Sub topic 8
Modeling One-to-Many and Many-to-Many Relationships
This chapter assumes you understand basic tables and can read short CREATE TABLE and SELECT statements. It also assumes you know that a primary key identifies a row and that a foreign key stores a key from another table.
One-to-many relationships
A one-to-many relationship means one row in one table can relate to many rows in another table. A customer can place many orders, but each order belongs to one customer.
Put the primary key on the “one” side. Store that key as a foreign key on the “many” side. This avoids copying the customer’s name and email into every order.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date DATE NOT NULL,
total DECIMAL(10, 2) NOT NULL CHECK (total >= 0)
);
INSERT INTO customers (customer_id, name)
VALUES (1, 'Ada Lovelace');
INSERT INTO orders (order_id, customer_id, order_date, total)
VALUES
(100, 1, '2026-01-15', 59.90),
(101, 1, '2026-01-20', 24.50);
SELECT c.name, o.order_id, o.order_date, o.total
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id;The customer_id value 1 appears once in customers and twice in orders. The foreign key connects those rows without duplicating the customer record.
The relationship’s cardinality describes how many rows can participate.
| Relationship | Example | Foreign key location |
|---|---|---|
| One-to-many | One customer, many orders | On the many-side table |
| One-to-one | One user, one profile | On one table with a `UNIQUE` foreign key |
| Many-to-many | Many orders, many products | In a junction table |
Many-to-many relationships
A many-to-many relationship means each row on either side can relate to many rows on the other side. An order can contain many products, and a product can appear in many orders.
A relational table should not store a variable-length list such as product_ids = '4,7,9'. Instead, create a junction table, also called an associative table. Each row in the junction table represents one relationship.
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL REFERENCES orders (order_id),
product_id INTEGER NOT NULL REFERENCES products (product_id),
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);
INSERT INTO products (product_id, name)
VALUES
(10, 'Notebook'),
(11, 'Pen');
INSERT INTO order_items (order_id, product_id, quantity)
VALUES
(100, 10, 2),
(100, 11, 3);
SELECT o.order_id, p.name, oi.quantity
FROM order_items AS oi
JOIN orders AS o ON o.order_id = oi.order_id
JOIN products AS p ON p.product_id = oi.product_id
ORDER BY o.order_id, p.name;The composite primary key, (order_id, product_id), prevents the same product from appearing twice in one order. If the application needs separate lines for the same product, use a separate order_item_id key instead.
Common modeling errors
- Putting a comma-separated list of IDs in one column makes validation and joins difficult.
- Omitting foreign keys allows relationships to point to rows that do not exist.
- Forgetting a uniqueness rule can create duplicate relationships.
- Copying parent attributes into child rows creates update anomalies. A changed customer email might then require many updates.
Exercise
What table and key structure represents students enrolling in many courses when each course has many students?
Show answer
Create students and courses tables with primary keys, then create an enrollments junction table containing studentid and courseid as foreign keys. Use PRIMARY KEY (studentid, courseid) unless duplicate enrollments are allowed.