Start here
Name the things, name the links, then count the links.
A data model is a plan for how data is stored. In chapter 1 you saw why a plan helps. This chapter gives you three words that every plan uses.
- Entity: a kind of thing the business keeps data about, such as a customer.
- Relationship: a link between two entities, such as a customer places an order.
- Cardinality: how many rows on one side can link to one row on the other side.
A picture of entities and relationships is an entity-relationship diagram, or ERD. Step through the three moves below to see one grow.
The chapter in three moves
A shop sells to its customers.
What things does this shop keep data about?
- customer_id
- name
- country
- order_id
- status
- amount
Two entities, one box each. Each line in a box is one attribute. The underlined line is the identifier.
- Name the entities and their attributes
- Name the relationship as a verb, in both directions
- State the count rule, and spot a many-to-many
The shop data
Most examples in this chapter use the shop from the SQL chapters. Here are its tables.
| customer_id | name | country |
|---|---|---|
| 1 | Asha | IN |
| 2 | Ben | GB |
| 3 | Carla | US |
| 4 | Dev | NULL |
| 5 | Omar | IN |
| order_id | customer_id | status | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | paid | 40.00 | 2026-01-03 |
| 102 | 1 | paid | 25.00 | 2026-01-10 |
| 103 | 2 | refunded | 60.00 | 2026-01-11 |
| 104 | 3 | paid | 15.50 | 2026-01-15 |
| 105 | 3 | pending | NULL | 2026-01-20 |
| 106 | 4 | paid | 80.00 | 2026-01-21 |
| product_id | name |
|---|---|
| 1 | Pen |
| 2 | Notebook |
| 3 | Bag |
| 4 | Lamp |
| 5 | Chair |
| order_id | product_id | quantity |
|---|---|---|
| 101 | 1 | 2 |
| 101 | 2 | 1 |
| 102 | 3 | 1 |
| 103 | 4 | 1 |
| 104 | 1 | 3 |
| 106 | 5 | 1 |
| 106 | 4 | 1 |
One change from the SQL chapters: each product now has its own row in products. The table order_items stores the product_id, not the product name.
Two facts matter most in this chapter.
- Omar is a customer, but he has no orders.
- Order 105 exists, but it has no items.
