Database Relationships and Diagrams · Lesson 23 of 112

Database Cardinality and Optionality

What you will learn

  • What cardinality means
  • What optionality means
  • How to write "minimum and maximum" for each side
  • How NOT NULL makes a link required
  • Why the difference shows up in queries

1. Two questions about every relationship

For each side of a relationship, ask:

  1. Cardinality (maximum): can it link to one row or many?
  2. Optionality (minimum): must it link to at least one row (mandatory), or can it link to none (optional)?

Together they give four useful readings for one side:

Reading Minimum Maximum
exactly one 1 1
zero or one 0 1
one or many 1 many
zero or many 0 many

2. Reading a diagram

Take "a team has players":

teams  1 ----------o< players

Read each end:

  • At the players end, o< means zero or many: a team may have no players yet, or many.
  • At the teams end, 1 means a player is linked to one team. Whether a player may also have no team is the optionality question, and a plain 1 does not answer it.

Here is the convention used in this lesson (tools draw these a little differently, so check the legend of the tool you use):

Mark Meaning
1 exactly one (mandatory)
o1 zero or one (optional)
< one or many
o< zero or many

A team can exist with no players (zero or many). Can a player exist without a team? That is a business decision, and the next section shows how it becomes code.

3. How the rules become code

A foreign key column controls the child's side:

  • Required parent: NOT NULL on the foreign key. Every child must have a parent.
  • Optional parent: allow NULL. A child may have no parent.

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It adds small tables of its own to your browser's copy; run the examples from top to bottom, and press Reset data to start again.

CREATE TABLE teams (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE players (id INTEGER PRIMARY KEY, name TEXT, team_id INTEGER REFERENCES teams (id));
CREATE TABLE kit_orders (id INTEGER PRIMARY KEY, item TEXT, team_id INTEGER NOT NULL REFERENCES teams (id));
INSERT INTO teams VALUES (1, 'Tigers');
INSERT INTO players VALUES (1, 'Sam', NULL);
SELECT * FROM players;
id  name  team_id
--  ----  -------
 1  Sam   NULL
(1 row)

A kit order without a team is refused (required link):

INSERT INTO kit_orders VALUES (1, 'Kit', NULL);
IntegrityError: NOT NULL constraint failed: kit_orders.team_id
CREATE TABLE teams (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE players (id INTEGER PRIMARY KEY, name TEXT, team_id INTEGER REFERENCES teams (id));
CREATE TABLE kit_orders (id INTEGER PRIMARY KEY, item TEXT, team_id INTEGER NOT NULL REFERENCES teams (id));
INSERT INTO teams VALUES (1, 'Tigers');
INSERT INTO players VALUES (1, 'Sam', NULL);
SELECT * FROM players;
id  name  team_id
--  ----  -------
 1  Sam   NULL
(1 row)

A kit order without a team is refused (required link):

INSERT INTO kit_orders VALUES (1, 'Kit', NULL);
IntegrityError: NOT NULL constraint failed: kit_orders.team_id
CREATE TABLE teams (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE players (id INTEGER PRIMARY KEY, name TEXT, team_id INTEGER REFERENCES teams (id));
CREATE TABLE kit_orders (id INTEGER PRIMARY KEY, item TEXT, team_id INTEGER NOT NULL REFERENCES teams (id));
INSERT INTO teams VALUES (1, 'Tigers');
INSERT INTO players VALUES (1, 'Sam', NULL);
SELECT * FROM players;
id  name  team_id
--  ----  -------
 1  Sam   NULL
(1 row)

A kit order without a team is refused (required link):

INSERT INTO kit_orders VALUES (1, 'Kit', NULL);
IntegrityError: NOT NULL constraint failed: kit_orders.team_id

A player without a team is accepted (optional link). A kit order without a team is refused (required link).

The Shop has both kinds in one table. orders.customer_id is NOT NULL (every order has a customer), while orders.employee_id allows NULL (online orders have no employee). Count them:

SELECT COUNT(*) AS orders, COUNT(employee_id) AS with_employee, COUNT(*) - COUNT(employee_id) AS without_employee
FROM orders;
orders  with_employee  without_employee
------  -------------  ----------------
  2600           2279               321
(1 row)

The difference shows when you join. Add Rina in team 1; Sam has no team:

INSERT INTO players VALUES (2, 'Rina', 1);
SELECT * FROM players ORDER BY id;
id  name  team_id
--  ----  -------
 1  Sam      NULL
 2  Rina        1
(2 rows)
SELECT p.name, t.name AS team FROM players p LEFT JOIN teams t ON t.id = p.team_id ORDER BY p.id;
name  team
----  ------
Sam   NULL
Rina  Tigers
(2 rows)
SELECT p.name, t.name AS team FROM players p JOIN teams t ON t.id = p.team_id ORDER BY p.id;
name  team
----  ------
Rina  Tigers
(1 row)
INSERT INTO players VALUES (2, 'Rina', 1);
SELECT * FROM players ORDER BY id;
id  name  team_id
--  ----  -------
 1  Sam      NULL
 2  Rina        1
(2 rows)
SELECT p.name, t.name AS team FROM players p LEFT JOIN teams t ON t.id = p.team_id ORDER BY p.id;
name  team
----  ------
Sam   NULL
Rina  Tigers
(2 rows)
SELECT p.name, t.name AS team FROM players p JOIN teams t ON t.id = p.team_id ORDER BY p.id;
name  team
----  ------
Rina  Tigers
(1 row)
INSERT INTO players VALUES (2, 'Rina', 1);
SELECT * FROM players ORDER BY id;
id  name  team_id
--  ----  -------
 1  Sam      NULL
 2  Rina        1
(2 rows)
SELECT p.name, t.name AS team FROM players p LEFT JOIN teams t ON t.id = p.team_id ORDER BY p.id;
name  team
----  ------
Sam   NULL
Rina  Tigers
(2 rows)
SELECT p.name, t.name AS team FROM players p JOIN teams t ON t.id = p.team_id ORDER BY p.id;
name  team
----  ------
Rina  Tigers
(1 row)

A plain JOIN drops Sam because he has no team. A LEFT JOIN keeps him. If a link is optional, remember this when you write queries.

Worked example: decide min and max

A library: "a loan is made by a member".

Question Answer Result
Can a loan have many members? no max 1
Can a loan exist with no member? no min 1, so member_id INTEGER NOT NULL REFERENCES members(id)
Can a member have many loans? yes max many
Can a member have no loans? yes (a new member) min 0

So members to loans is "one" to "zero or many". Names are made up.

5. Notations, and identifying relationships

Diagram styles differ. The most common one, crow's foot notation, draws a three-pronged "foot" on the many side (the < in our table) and a small circle for optional. If you learn the idea (cardinality plus optionality on each end), you can read any of them; check the legend each time.

One more distinction is common in textbooks. A relationship is identifying when the child cannot be identified without its parent: the foreign key is part of the child's primary key. It is non-identifying when the child has its own key and the foreign key is an ordinary column, which can be optional:

CREATE TABLE shipments (id INTEGER PRIMARY KEY, placed TEXT NOT NULL);
CREATE TABLE shipment_lines (
    shipment_id INTEGER NOT NULL REFERENCES shipments (id),
    line_no     INTEGER NOT NULL,
    item        TEXT NOT NULL,
    PRIMARY KEY (shipment_id, line_no)
);
CREATE TABLE helpers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE tickets (id INTEGER PRIMARY KEY, title TEXT NOT NULL, assigned_to INTEGER REFERENCES helpers (id));
INSERT INTO shipments VALUES (1, '2026-03-01');
INSERT INTO shipment_lines VALUES (1, 1, 'Notebook'), (1, 2, 'Pen');
INSERT INTO helpers VALUES (1, 'Rina');
INSERT INTO tickets VALUES (1, 'Fix lamp', 1), (2, 'Order pens', NULL);
(2 rows changed)

The same line number twice is refused:

INSERT INTO shipment_lines VALUES (1, 2, 'Mug');
IntegrityError: UNIQUE constraint failed: shipment_lines.shipment_id, shipment_lines.line_no
SELECT * FROM shipment_lines ORDER BY line_no;
SELECT * FROM tickets ORDER BY id;
shipment_id  line_no  item
-----------  -------  --------
          1        1  Notebook
          1        2  Pen
(2 rows)

id  title       assigned_to
--  ----------  -----------
 1  Fix lamp              1
 2  Order pens         NULL
(2 rows)
CREATE TABLE shipments (id INTEGER PRIMARY KEY, placed TEXT NOT NULL);
CREATE TABLE shipment_lines (
    shipment_id INTEGER NOT NULL REFERENCES shipments (id),
    line_no     INTEGER NOT NULL,
    item        TEXT NOT NULL,
    PRIMARY KEY (shipment_id, line_no)
);
CREATE TABLE helpers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE tickets (id INTEGER PRIMARY KEY, title TEXT NOT NULL, assigned_to INTEGER REFERENCES helpers (id));
INSERT INTO shipments VALUES (1, '2026-03-01');
INSERT INTO shipment_lines VALUES (1, 1, 'Notebook'), (1, 2, 'Pen');
INSERT INTO helpers VALUES (1, 'Rina');
INSERT INTO tickets VALUES (1, 'Fix lamp', 1), (2, 'Order pens', NULL);
(2 rows changed)

The same line number twice is refused:

INSERT INTO shipment_lines VALUES (1, 2, 'Mug');
IntegrityError: UNIQUE constraint failed: shipment_lines.shipment_id, shipment_lines.line_no
SELECT * FROM shipment_lines ORDER BY line_no;
SELECT * FROM tickets ORDER BY id;
shipment_id  line_no  item
-----------  -------  --------
          1        1  Notebook
          1        2  Pen
(2 rows)

id  title       assigned_to
--  ----------  -----------
 1  Fix lamp              1
 2  Order pens         NULL
(2 rows)
CREATE TABLE shipments (id INTEGER PRIMARY KEY, placed TEXT NOT NULL);
CREATE TABLE shipment_lines (
    shipment_id INTEGER NOT NULL REFERENCES shipments (id),
    line_no     INTEGER NOT NULL,
    item        TEXT NOT NULL,
    PRIMARY KEY (shipment_id, line_no)
);
CREATE TABLE helpers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE tickets (id INTEGER PRIMARY KEY, title TEXT NOT NULL, assigned_to INTEGER REFERENCES helpers (id));
INSERT INTO shipments VALUES (1, '2026-03-01');
INSERT INTO shipment_lines VALUES (1, 1, 'Notebook'), (1, 2, 'Pen');
INSERT INTO helpers VALUES (1, 'Rina');
INSERT INTO tickets VALUES (1, 'Fix lamp', 1), (2, 'Order pens', NULL);
(2 rows changed)

The same line number twice is refused:

INSERT INTO shipment_lines VALUES (1, 2, 'Mug');
IntegrityError: UNIQUE constraint failed: shipment_lines.shipment_id, shipment_lines.line_no
SELECT * FROM shipment_lines ORDER BY line_no;
SELECT * FROM tickets ORDER BY id;
shipment_id  line_no  item
-----------  -------  --------
          1        1  Notebook
          1        2  Pen
(2 rows)

id  title       assigned_to
--  ----------  -----------
 1  Fix lamp              1
 2  Order pens         NULL
(2 rows)

An order line is meaningless without its order: line 2 of shipment 1 is identified by the pair (shipment_id, line_no), so the relationship is identifying. A ticket exists on its own and may be unassigned (NULL), so it is non-identifying. Many designers skip the distinction and just use a surrogate id for every table (lesson 2.5), which is a fine choice; the words are still worth knowing.

Common mistakes

  • Mixing up which side the rule applies to. NOT NULL on the foreign key says the child must have a parent, nothing about whether a parent must have children.
  • Making every link mandatory, blocking valid cases like a player waiting for a team.
  • Forgetting LEFT JOIN for optional links.
  • Guessing instead of asking the business whether a link is required.

Key takeaways

  • Cardinality is the maximum (one or many); optionality is the minimum (zero or one).
  • Each side of a relationship has its own pair of answers.
  • NOT NULL on the foreign key makes the parent required.
  • A nullable foreign key makes it optional.
  • Use LEFT JOIN when a link can be missing.

Practice

1. What is the difference between cardinality and optionality?

Show answer

Cardinality is how many rows can link (maximum); optionality is whether at least one link is required (minimum).

2. How do you make a parent mandatory for a child?

Show answer

Declare the foreign key column NOT NULL.

3. What does "zero or many" mean?

Show answer

An optional link with no upper limit: a row may have none or any number.

4. Why was Sam missing from the plain JOIN result?

Show answer

His team_id is NULL, so no team row matches; a plain JOIN keeps only matching rows.

5. An invoice must have exactly one customer. Write the column.

Show answer

customer_id INTEGER NOT NULL REFERENCES customers(id) in invoices.

6. Why should you ask the business about the minimum?

Show answer

Whether a link is required is a rule of the real world, not something the data can reveal; the wrong guess either blocks valid data or allows bad data.