From ER Diagram to Tables
Apply the mapping rules that turn entities and relationships into tables, place foreign keys correctly, and choose what happens on update and delete.
Welcome back!
Step 1 of 14Learn
Mapping entities and attributes
With a finished ER diagram, building the logical schema is mostly mechanical. A few rules cover nearly every case.
Rule 1: each strong entity becomes a table. Its identifier becomes the primary key, and its simple attributes become columns. Customer becomes customer(customer_id, name, email). Composite attributes are flattened into their parts: the address becomes street, city, postal_code and country.
Rule 2: each multivalued attribute becomes its own table, with a foreign key back to the owner and a primary key made of the owner's key plus the value. A customer's phone numbers become customer_phone(customer_id, phone), with primary key (customer_id, phone), so the same number can't be listed twice for one customer.
Rule 3: derived attributes are usually left out. An order's total is computed from its lines when needed.
Rule 4: weak entities become tables whose primary key includes the owner's key. An order line is identified by its order plus a line number: order_line(order_id, line_no, ...).
Naming conventions help a schema stay readable. Pick one style and use it everywhere: singular or plural table names, snake_case columns, and foreign keys named after the table they point to, like customer_id.
Step 2 of 14Practice · Choose one
A Customer entity has a multivalued attribute "phone numbers." How is it implemented?
Step 3 of 14Flashcards
Mapping rules for entities and attributes
Tap a card to flip it.
Step 4 of 14Learn
Foreign keys and the "many" side
A foreign key (FK) is a column whose values must match the primary key of another table (or be NULL, if allowed). It is how relationships appear in tables.
Rule 5: in a one-to-many relationship, put the foreign key on the "many" side. Each order has exactly one customer, so the order table gets a customer_id column:
customer(customer_id, name, email) orders(order_id, order_date, customer_id → customer)
Why not the other way? Putting order ids in the customer table would require a list in one cell, because one customer has many orders. On the many side, each row needs only one value.
Participation decides nullability. If every order must have a customer, declare customer_id NOT NULL. If an employee may have no department, the department_id column allows NULL.
Rule 6: a many-to-many relationship becomes a junction table holding two foreign keys, one to each side. Its primary key is usually the pair: book_author(book_id, author_id). Relationship attributes go here too: order_line(order_id, book_id, quantity, unit_price).
Rule 7: a one-to-one relationship puts a foreign key on either side, declared UNIQUE. Usually choose the side where participation is optional, to avoid NULLs: a parking_space table with a UNIQUE employee_id.
Step 5 of 14Practice · Spot the mistake
A student mapped the bookstore ER diagram to tables. Tap the two lines that break the mapping rules.
Step 6 of 14Learn
Referential integrity in action
Declaring a foreign key turns on referential integrity: the database guarantees that every foreign key value points to a row that actually exists. You can't insert an order for customer 999 if there is no customer 999. That one rule eliminates a whole class of "orphan" rows that silently corrupt reports.
The interesting question is what happens when the referenced row changes. Suppose someone tries to delete customer 12, who has orders. The foreign key can be declared with an action:
- RESTRICT / NO ACTION (the default): refuse the delete while orders exist. Safe and the usual choice for important data.
- CASCADE: delete the customer's orders automatically too. Convenient for data that truly belongs to its parent, like order lines when an order is deleted, but dangerous elsewhere. One click could wipe out years of sales history.
- SET NULL: keep the orders but set their customer_id to NULL. Only possible if the column allows NULL.
- SET DEFAULT: set the foreign key to its default value.
The same choices exist for ON UPDATE, used when a primary key value changes. With stable surrogate keys that almost never happens, which is one more argument for them. Many real systems avoid deleting customers at all and instead mark them inactive, keeping history intact.
Step 7 of 14Practice · Match the pairs
Match each foreign key action to what happens when the parent row is deleted.
Tap an answer, then tap the place where it belongs (or drag it there).
- ON DELETE RESTRICT
- ON DELETE CASCADE
- ON DELETE SET NULL
Step 8 of 14Compare
Foreign key actions when a parent row is deleted
| Aspect | Child rows | Good fit |
|---|---|---|
| RESTRICT / NO ACTION | Delete is refused while children exist | Customers with orders, products with sales |
| CASCADE | Deleted along with the parent | Order lines of a deleted order |
| SET NULL | Foreign key set to NULL | Optional links, e.g. an employee’s mentor leaves |
| SET DEFAULT | Foreign key set to its default | Reassign to a placeholder category |
Step 9 of 14Note
Step 10 of 14Learn
A complete mapping
Here is the whole bookstore model as tables. Primary keys are in bold; arrows show foreign keys.
| Table | Columns |
|---|---|
| publisher | publisher_id, name, country |
| book | book_id, isbn (unique), title, price, pub_year, publisher_id → publisher |
| author | author_id, name |
| book_author | book_id → book, author_id → author, byline_position |
| customer | customer_id, name, email (unique), street, city, postal_code |
| orders | order_id, order_date, customer_id → customer |
| order_line | order_id → orders, book_id → book, quantity, unit_price |
Seven tables for what started as one spreadsheet. Each fact now lives in exactly one place: Ana's address is in one customer row; a book's current price is in one book row. Two details are worth noticing. The order table is called orders because ORDER is a reserved word in SQL. And order_line stores unit_price even though book has a price: the book's price can change tomorrow, but the price Ana actually paid last March must not. That isn't redundancy; it's a different fact, the historical price at the time of sale.
Step 11 of 14Practice · Work it out
In the complete bookstore schema (publisher, book, author, book_author, customer, orders, order_line), how many foreign key columns are there in total?
Step 12 of 14Practice · True or false
Storing unit_price in order_line, when book already has a price column, is redundancy that normalization should remove.
Step 13 of 14Practice · Chart
Rows per table after a year of trading
Tables on the "many" side of relationships grow fastest, because every sale adds rows to them while the reference tables barely change.
Tap the table that grows the most as the shop sells.
Tap the bar that answers the question, then press Check.
Step 14 of 14Practice · Choose one
In a one-to-many relationship where each employee belongs to exactly one department, where does the foreign key go, and how should it be declared?
Lesson complete!