🗃️One-to-Many Relationship
Also known as One-to-Many, 1:N Relationship, Table Relationship, Parent-Child Table
A link between two tables. One row on one side goes with many rows on the other.
Think of it like
It is like a mum and her kids. One mum can have many kids, but each kid has one mum.
Example
Lucy's blog has authors and posts. Each post stores an author_id, so one author can have many posts.
Why it matters
It is the most common link between tables. Customers and orders, posts and comments, and teams and players all work this way.
Definition
Example code
sql
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
total NUMERIC
);Common mistake
Key takeaways
- 1One side, many side. One customer, many orders.
- 2Key on the many side. Each order holds a customer_id.
- 3Join to read it. A join brings both tables together.