One-to-Many
ELI5 — The Vibe Check
One-to-Many means one row in Table A can relate to many rows in Table B. One user can have many orders. One post can have many comments. It is the most common relationship in databases and is implemented with a foreign key on the 'many' side.
Real Talk
A one-to-many relationship means that one record in the parent table can be referenced by multiple records in the child table. The child table holds the foreign key pointing to the parent's primary key. It is the most fundamental relationship in relational database design.
Show Me The Code
-- One user, many orders
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(id), -- many side
total DECIMAL
);
When You'll Hear This
"A user has a one-to-many relationship with orders." / "Put the foreign key on the 'many' side."
Related Terms
ERD (Entity Relationship Diagram)
An ERD is a visual map of your database — boxes for tables, lines showing how they connect.
Foreign Key
A foreign key is how you link two tables together. If an 'orders' table has a 'user_id' column pointing to the 'users' table, that is a foreign key.
Junction Table
A junction table (also called a join table) is the middle table you create to represent a many-to-many relationship.
Many-to-Many
Many-to-Many means rows on both sides can relate to many rows on the other side. Students can enroll in many courses, and courses can have many students.
One-to-One
One-to-One is a relationship where one row in Table A corresponds to exactly one row in Table B. Like a user and their profile — one user, one profile.