skip to content

from requirements to tables

entities, relationships, one-to-many and many-to-many. drawing the shop before creating it.

address
everything.design.er_modeling
part
part 7, schema design
chapter
chapter 1 of 13
reading time
7 min

before any CREATE TABLE: find the entities (the nouns that have their own identity), their attributes, and the relationships between them, each with a cardinality: one-to-one, one-to-many, many-to-many.

then the translation is mechanical. an entity is a table. one-to-many is a foreign key on the many side. many-to-many is a junction table with two foreign keys. one-to-one is a foreign key with UNIQUE on it.

the picture

you know how a schema designed while typing CREATE TABLE ends up with a products column on orders, then product2, then a comma-separated list? the shape of the data was decided by whichever table got written first.

think of an architect's sketch before the bricks (the diagram before the DDL). boxes for the rooms, lines for the doors, and a note on each door saying how many people go through it. moving a wall on paper is free; moving it after the concrete sets is a migration.

here are the shop's requirements, as a product person would write them:

customers place orders. an order has one or more lines; each line is some quantity of one product. a customer can exist before ordering anything. a product can exist before anyone buys it.

the nouns are the candidate entities: customer, order, line, product. "quantity" is an attribute, it has no life of its own. the verbs are relationships: places, has, is of. and the little words carry the cardinality: one or more, before ordering anything (zero is allowed).

placescustomersidpknameemailuniquecityordersidpkcustomer_idfkstatusordered_atorder_itemsorder_idpk fkline_nopkproduct_idfkqtyproductsidpkskuuniquenameprice
foreign key → the key it points at
drawn by hand · from datasets/shop/schema.sql
the shop, drawn before it's created. read each line from both ends: one customer places zero or more orders (circle = zero allowed, foot = many), and each order belongs to exactly one customer (two bars). order_items is where the many-to-many between orders and products lives.

from boxes and lines to tables

hover a stage.

the whole method, left to right. each step is a question you answer from the requirements, not from the database.

the cardinality decides where the foreign key lives:

relationshipexamplebecomes
one-to-manya customer places many ordersorders.customer_id REFERENCES customers, on the many side
many-to-manyan order contains many products, a product is in many ordersa junction table, order_items (order_id, product_id, ...)
one-to-onea customer has at most one loyalty cardcards.customer_id REFERENCES customers UNIQUE

and the zero-or-not on each end decides NOT NULL. "every order belongs to a customer" is customer_id NOT NULL. "an employee may have a manager" is a nullable manager_id.

predict the output
sql
-- a customer places many orders; an order has one customer.
-- where does the foreign key go?

many-to-many and the junction table

neither side of a many-to-many can hold the reference, because both sides have many. so the relationship becomes a table of its own, one row per pair. the school dataset's enrollments is one: a student takes many courses, a course has many students.

studentsidpknameemailuniqueenrollmentsstudent_idpk fkcourse_codepk fktermpkgradecoursescodepktitlecredits
foreign key → the key it points at
drawn by hand · from datasets/school/schema.sql
a many-to-many, resolved. enrollments has a foreign key to each side, and a composite primary key. the grade is an attribute of the relationship (this student, in this course, this term), so it lives on the junction table, not on either entity.

walking a many-to-many is two joins through the junction. Priya Nair's courses:

sqldb=school
SELECT c.code, c.title, e.term, e.grade
FROM students AS s
JOIN enrollments AS e ON e.student_id = s.id
JOIN courses AS c ON c.code = e.course_code
WHERE s.name = 'Priya Nair'
ORDER BY e.term, c.code;
output, from postgres 18.6
SELECT 7
codetexttitletexttermtextgradechar
CS101Intro to Programming2025-fallC
CS301Databases2026-fallnull
CS350Operating Systems2026-fallnull
CS401Database Internals2026-fallnull
MATH301Linear Algebra2026-fallnull
CS201Data Structures2026-springB
PHYS201Electromagnetism2026-springB
(7 rows)

and the other direction, a course's students, is the same two joins read from the other end. no list stored anywhere, no column per course: adding the hundredth course to a student's history is one more row.

one-to-one, and when it's really one table

a true one-to-one is rare. usually it's an optional extension of an entity: a customer may have a loyalty card, and the card's columns would be NULL for most customers. a foreign key plus UNIQUE says "at most one":

sqlempty db
CREATE TABLE customers (id int PRIMARY KEY, name text NOT NULL);
CREATE TABLE loyalty_cards (
  customer_id int PRIMARY KEY REFERENCES customers,
  points      int NOT NULL DEFAULT 0
);
INSERT INTO customers VALUES (42, 'Ada Lovelace');
INSERT INTO loyalty_cards VALUES (42, 120);
INSERT INTO loyalty_cards VALUES (42, 0);
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE TABLE
#3 · INSERT 0 1
#4 · INSERT 0 1
#5 · ERROR 23505
ERROR 23505: duplicate key value violates unique constraint "loyalty_cards_pkey"
DETAIL: Key (customer_id)=(42) already exists.

here the foreign key is the primary key, which gives both: the card can't exist without a customer, and a customer can't have two. if every customer always has exactly one card, and you always read them together, it's one table with a few more columns.

a few modeling calls that come up every time

  • an attribute or an entity? if you'll filter, group or constrain on its parts, or several things share it, it's an entity. an address you only print is a column; addresses with a country you report on, shared by billing and shipping, are a table.
  • an attribute of what? a price that changes over time is an attribute of the product now, and of the order line then. both columns are right, because they're different facts (denormalizing on purpose).
  • relationships to yourself. an employee's manager is another employee: a foreign key from the table to itself, employees.manager_id REFERENCES employees. that's a tree, and storing trees is about querying it.
  • one relationship or several? an order has a billing and a shipping address: two foreign keys to the same table, each named for its role.

keylines

  • nouns are entities (tables), properties are attributes (columns), verbs are relationships.
  • one-to-many: a foreign key on the many side.
  • many-to-many: a junction table with two foreign keys, and the relationship's own attributes.
  • one-to-one: a foreign key with UNIQUE, often as the primary key.
  • zero or one on an end decides NULL or NOT NULL.