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

Notation
Cardinality

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.

Anna Becker has orders 1 and 7, Ben Okafor has order 3, Chiara Rossi has order 2, Deniz Kaya has none.

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:

line-end
At least
At most

0..n

zero, one or many

A customer has any number of orders, also none.

The four ends
SymbolMin..maxMeaning
1..1exactly oneEach order has exactly one customer.
0..1zero or oneA customer has at most one card.
1..none or manyMade-up rule: only people who ordered count as customers.
0..nzero, 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.

quiz0 of 4 right
  1. 01
    Cardinality of customers and orders

  2. 02
    Cardinality of customers and products

  3. 03
    Cardinality of customers and customer cards

  4. 04
    Cardinality of products and orders

0 of 4 right