| 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 exactly one row in Table A.
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 PRIMARY KEY,
Name VARCHAR(100) NOT NULL
);
-- Child (the "many" side)
CREATE TABLE "Order" (
OrderID INT 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)
ON DELETE RESTRICT -- choose CASCADE/SET NULL/RESTRICT per policy
);
-- Performance best practice:
CREATE INDEX ix_order_customerid ON "Order"(CustomerID);
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 shows the relationship from A’s perspective: one A points to many Bs. The second shows B’s perspective: each B points back to exactly one A. Together they illustrate the two sides of the same 1:N relationship.
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.