Skip to content

Many-to-Many

Medium — good to knowDatabase

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.