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).
from boxes and lines to tables
hover a stage.
the cardinality decides where the foreign key lives:
| relationship | example | becomes |
|---|---|---|
| one-to-many | a customer places many orders | orders.customer_id REFERENCES customers, on the many side |
| many-to-many | an order contains many products, a product is in many orders | a junction table, order_items (order_id, product_id, ...) |
| one-to-one | a customer has at most one loyalty card | cards.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.
-- 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.
walking a many-to-many is two joins through the junction. Priya Nair's courses:
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;| codetext | titletext | termtext | gradechar |
|---|---|---|---|
| CS101 | Intro to Programming | 2025-fall | C |
| CS301 | Databases | 2026-fall | null |
| CS350 | Operating Systems | 2026-fall | null |
| CS401 | Database Internals | 2026-fall | null |
| MATH301 | Linear Algebra | 2026-fall | null |
| CS201 | Data Structures | 2026-spring | B |
| PHYS201 | Electromagnetism | 2026-spring | B |
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":
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);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.