Cardinality & Relationships — Database

Cardinality answers "how many rows in table B does one row in table A connect to, and the other way round?" Ask it in both directions, with real data. The…

Sunset Coffee's order #1 holds Milk Coffee and Toast. Milk Coffee is also in order #3. You met the foreign key in the What Is Database Design lesson: a column that points at an id in another table. But to connect orders and products, where does the foreign key go? In orders, in products, or somewhere else?

That depends on cardinality. Cardinality answers this question: how many rows in table B may one row in table A connect to, and the other way round? There are only three answers, and you already know all of them from daily life:

Cardinality decides where the foreign key goes, so guessing the cardinality wrong means building the wrong tables.

One question, two directions

Always ask about cardinality in both directions, with real example data. At Sunset Coffee, the three cardinalities appear in three pairs of tables:

Pair One A has how many B? One B belongs to how many A? Cardinality
customers and profiles one (a profile with an address and a birth date) one 1:1 one-to-one
customers and orders many (Budi ordered twice) one (each order has 1 owner) 1:N one-to-many
orders and products many (Milk Coffee and Toast) many (Milk Coffee is in #1 and #3) M:N many-to-many

Other pairs you'll meet often: user and profile (1:1), user and blog post (1:N), an order and its item rows (1:N), student and course (M:N), tag and article (M:N). Student and course are M:N because one student takes many courses, and one course is taken by many students. M:N is sometimes written N:M or N:N, meaning the same.

One-to-one (1:1)

One customer has at most one profile. The foreign key customer_id goes in profiles, plus UNIQUE so one customer can't have a second profile. This code makes the table, then tries to give Budi (customer 1) two profiles:

CREATE TABLE profiles (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL UNIQUE REFERENCES customers(id),
  address TEXT NOT NULL
);

INSERT INTO profiles (id, customer_id, address) VALUES (1, 1, '5 Jasmine Street');
INSERT INTO profiles (id, customer_id, address) VALUES (3, 1, '9 Rose Street');
-- Error: UNIQUE constraint failed: profiles.customer_id

One-to-many (1:N)

One customer has many orders, and each order belongs to one customer. The foreign key goes on the many side, orders.customer_id, without UNIQUE:

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers(id),
  ordered_at TEXT NOT NULL
);

Budi ordered twice, so two orders rows both hold customer_id = 1, and that's fine. But order #1 can't also belong to Siti. The customer_id cell in that row fits one number, and a second row with id = 1 is rejected (UNIQUE constraint failed: orders.id) because id is the primary key. If you add the UNIQUE from the 1:1 example to customer_id here, it's Budi's second order that gets rejected: the model is too strict.

Many-to-many: it needs a linking table

Orders and products can't be connected with one plain foreign key:

So the foreign key goes somewhere else: a new table between the two. This table is called a linking table (a junction table, or a pivot table in Laravel), and it holds pairs of foreign keys from both sides. This code makes order_items, then records the products in orders #1 and #3:

CREATE TABLE order_items (
  order_id INTEGER NOT NULL REFERENCES orders(id),
  product_id INTEGER NOT NULL REFERENCES products(id),
  qty INTEGER NOT NULL,
  PRIMARY KEY (order_id, product_id)
);

INSERT INTO order_items (order_id, product_id, qty) VALUES (1, 1, 2);  -- #1: 2 Milk Coffee
INSERT INTO order_items (order_id, product_id, qty) VALUES (1, 4, 1);  -- #1: 1 Toast
INSERT INTO order_items (order_id, product_id, qty) VALUES (3, 1, 1);  -- #3: 1 Milk Coffee

Where the foreign key goes

Cardinality Foreign key Guard
1:1 in one of the tables, usually the optional one (profiles.customer_id) UNIQUE
1:N on the many side (orders.customer_id) a plain foreign key
M:N in the linking table, both of them (order_items) a combined PRIMARY KEY

📌 A relationship to itself

Sometimes a table points at itself. This is called self-referencing. For example employees and their managers: a manager_id column in the employees table points at employees.id, because a manager is an employee too. The cardinality is still 1:N (one manager, many employees under that manager), both sides are just the same table.

⚠️ Too strict, too loose

TL;DR: Cardinality answers "how many rows in table B does one row in table A connect to, and the other way round?" Ask it in both directions, with real data. The answer decides where the foreign key (a column that points at an id in another table) goes. 1:1 or one-to-one (customer and profile, user and profile): a foreign key plus UNIQUE, so its values can't repeat. 1:N or one-to-many (customer and orders, user and posts, an order and its items): a foreign key on the many side. M:N or many-to-many (orders and products, student and course, tag and article): a plain foreign key alone isn't enough. It needs a linking table (a new table between the two) like order_items, with two foreign keys and a primary key that combines those two columns.

⚡ One A has how many B?

Guess first: cardinality is how many rows on one side connect to the other side, written 1:1 (one to one), 1:N (one to many) or M:N (many to many). One user has one profile, and each profile belongs to one user. Which cardinality is this?

1:1 (one-to-one). Asked in both directions, the answer is one both times.

Meaning: For two tables A and B, cardinality is always asked in both directions: one A has how many B, and one B belongs to how many A. One and one is 1:1. Many and one is 1:N: one customer has many orders, each order belongs to one customer. Many and many is M:N: one order holds many products, and one product is in many orders.

In the exercise, return "1:1", "1:N" or "M:N" exactly: capitals, a colon, no spaces.

⚡ UNIQUE guards 1:1

Write:

CREATE TABLE profiles (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL UNIQUE REFERENCES customers(id),
  address TEXT NOT NULL
);

Meaning: The foreign key customer_id (a column that points at a customer's id) goes in profiles. UNIQUE rejects repeated values, so one customer can't have a second profile. In 1:N, the foreign key goes on the many side (orders.customer_id) without UNIQUE.

⚡ M:N needs a linking table

Write:

CREATE TABLE order_items (
  order_id INTEGER NOT NULL REFERENCES orders(id),
  product_id INTEGER NOT NULL REFERENCES products(id),
  qty INTEGER NOT NULL,
  PRIMARY KEY (order_id, product_id)
);
Guess first: orders and products are M:N. Why not just put order_id in products?

One order_id cell fits one order number, so each product could only be recorded in one order. Recording that product in another order with UPDATE quietly moves it out of the first order, with no error.

Meaning: order_items is a linking table between orders and products. One of its rows is one product in one order, with its quantity (qty). The combined primary key (order_id, product_id) rejects the same pair going in twice.

What you will learn