One-to-Many Relationship in ERD Design
How to model a one-to-many (1:N) relationship in an Entity-Relationship Diagram. Covers foreign key placement, crow's foot notation, and real-world examples.
Detailed Explanation
The Most Common Relationship
The one-to-many (1:N) relationship is the most frequently used relationship type in relational databases. It means that a single record in one table can be associated with multiple records in another table, but each record in the second table is associated with exactly one record in the first.
How It Works
Consider users and posts. One user can write many posts, but each post is written by exactly one user.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
The foreign key (user_id) lives in the "many" side table (posts). This is the fundamental rule: the foreign key always goes on the "many" side.
Identifying One-to-Many Relationships
Ask yourself: "Can one X have many Ys?" If the answer is yes, and "Can one Y belong to only one X?" is also yes, then it's a one-to-many relationship.
| "One" side | "Many" side | Foreign key in |
|---|---|---|
departments |
employees |
employees.department_id |
categories |
products |
products.category_id |
authors |
books |
books.author_id |
customers |
orders |
orders.customer_id |
Crow's Foot Representation
In the ERD editor, a one-to-many relationship displays a single line on the "one" side and a crow's foot (three-pronged fork) on the "many" side. The label "one : many" appears on the connecting line.
Cascading Behavior
When designing one-to-many relationships, consider what happens when the "one" side record is deleted. Common strategies:
- CASCADE: Delete all related records (e.g., deleting a user deletes all their posts)
- SET NULL: Set the foreign key to NULL (e.g., keep the post but remove the author)
- RESTRICT: Prevent deletion if related records exist
Use Case
You are designing a blog system, e-commerce platform, or any application where a parent record owns multiple child records. Understanding one-to-many relationships is foundational to relational database design and is the first relationship type to master.