| Lesson 6 | One-to-many relationships |
| Objective | Define and model one-to-many (1:N) relationships. |
Think in terms of tables: one row in Table A can relate to many rows in Table B, while each row in Table B relates back to at most one row in Table A. Business rules determine whether that parent is required or optional.
Suppose customer 101 (Ava) has orders 5001, 5002 and 5003, customer 102 (Ben) has no orders yet, and customer 103 (Cara) has order 5004. The relationship is one-to-many because several orders can reference the same customer. In this example, every order must reference exactly one customer.
Customer (one) ||----------o{ SalesOrder (zero or many)
This text sketch represents Crow's Foot endpoints: || means exactly one, and o{ means zero or many. Read it in both directions: one customer can have zero or many orders; one order belongs to exactly one customer. See the Crow's Foot symbols and reading rules.
These describe the same relationship from opposite directions. Customer to SalesOrder is 1:N; SalesOrder to Customer is N:1. It does not become one-to-one when read from an individual order. A one-to-one relationship also limits each customer to at most one related row.
Ask two questions about the pair of entities A and B:
Examples:
Customer → Order (one customer, many orders; each order belongs to exactly one customer),
Department → Employee (one department, many employees; each employee belongs to one department),
Author → Book (one author, many books; each book has one primary author) — for co-authors, see M:N in a later lesson.
1:N is implemented by placing a foreign key on the “many” side:
-- Parent (the "one" side)
CREATE TABLE Customer (
CustomerID INT NOT NULL PRIMARY KEY,
Name VARCHAR(100) NOT NULL
);
-- Child (the "many" side)
CREATE TABLE SalesOrder (
OrderID INT NOT NULL PRIMARY KEY,
CustomerID INT NOT NULL, -- each order must belong to exactly one customer
OrderDate DATE NOT NULL,
CONSTRAINT fk_order_customer
FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerID)
);
-- Performance best practice:
CREATE INDEX ix_order_customerid ON SalesOrder(CustomerID);
Supply identifiers explicitly. SalesOrder.CustomerID is deliberately not unique: different orders must be able to reference the same customer. A foreign key prevents nonexistent customer references; NOT NULL makes the reference mandatory. Neither constraint requires every customer to have an order.
This example omits an explicit delete action. Adapt deletion policy and syntax to your DBMS; in this schema, deleting a referenced customer is prevented while orders still reference it.
NOT NULL to require participation (every B must have an A). Allow NULL for optional participation.ON DELETE action that matches business rules (e.g., CASCADE to remove children with the parent, RESTRICT/NO ACTION to prevent deletion if children exist).Phone1, Phone2, Phone3). Use a child table with one row per phone number.The first diagram follows a customer to their orders. The second follows each order back to its customer. Both use the join condition SalesOrder.CustomerID = Customer.CustomerID. The match does not depend on visual row position or on OrderID equaling CustomerID.
| SalesOrder.OrderID (PK) | SalesOrder.CustomerID (FK) | Referenced customer |
|---|---|---|
| 5001 | 101 | Ava |
| 5002 | 101 | Ava |
| 5003 | 101 | Ava |
| 5004 | 103 | Cara |
OrderID identifies an individual order. CustomerID may repeat in SalesOrder, allowing several orders for one customer. Its foreign key and NOT NULL constraint require each order to reference an existing customer. No order row is required for Ben; optional participation on the customer side is represented by the absence of an order, not a null customer identifier in an order.
If your answers match, you have a one-to-many.
One-to-many is the most common relationship in relational design. Model it by placing a foreign key on the “many” side, choose optionality and delete actions that mirror the business rules, and index the FK. The next lesson explores one-to-many relationships further.