basics03Chapter 3 of 6
Relationships and cardinality
Tables are linked, but how many rows belong to how many? That number is the cardinality, and it shapes how a database is built.
Three kinds
In the diagram
A customer has no, one or many orders; each order belongs to exactly one customer.
In the data
One customer can have many orders, but each order belongs to exactly one customer. This is the most common kind. The foreign key always sits on the many side: orders.customer_id.
Optional or mandatory
A relationship has a maximum (one or many) and a minimum (zero or one). Can a customer exist without orders? Yes, Deniz has none yet: the minimum is zero, orders are optional. Can an order exist without a customer? No, customer_id must be filled in: the minimum is one, the customer is mandatory.
Crow's foot, piece by piece
Crow's foot notation draws both halves at the end of the line. Right at the table sits the maximum: a bar for one, a fork (the crow's foot) for many. Further out sits the minimum: a bar for one, a circle for zero. Build an end yourself:
0..n
zero, one or many
A customer has any number of orders, also none.
| Symbol | Min..max | Meaning |
|---|---|---|
| 1..1 | exactly oneEach order has exactly one customer. | |
| 0..1 | zero or oneA customer has at most one card. | |
| 1..n | one or manyMade-up rule: only people who ordered count as customers. | |
| 0..n | zero, one or manyA customer has any number of orders, also none. |
The short form 1 / n only says the maximum. It cannot show whether zero is allowed; for that you need the circle and the bar.
Quick check
Which cardinality fits each pair? You get an answer right away.
- 01
- 02
- 03
- 04
0 of 4 right