Many-to-Many
ELI5 — The Vibe Check
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. You cannot model this with just two tables — you need a third 'junction' table in between.
Real Talk
A many-to-many relationship means that multiple records in Table A can relate to multiple records in Table B. Since relational databases cannot directly represent this, it requires a junction table (also called a join table or bridge table) that holds foreign keys referencing both tables.
Show Me The Code
-- Students and Courses: many-to-many
CREATE TABLE enrollments (
student_id INT REFERENCES students(id),
course_id INT REFERENCES courses(id),
PRIMARY KEY (student_id, course_id)
);
When You'll Hear This
"Tags and posts have a many-to-many relationship — one post has many tags, one tag belongs to many posts." / "Use a junction table to implement many-to-many."
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.
One-to-Many
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.
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.