How do you model a many-to-many relationship?

SQL keys and table design · Freshers and experienced

Model answer

Create a junction table with foreign keys to both tables, for example enrolments(student_id, course_id), and make the pair the primary key (or add a unique constraint) so a pair cannot repeat. Extra columns, like enrolled_on, go in the junction table.

Learn this topic

Keys and Table Design: Why Data Is Split Into Tables in the course SQL for Beginners: Learn Databases Step by Step.

Sign in to add your own answer.

More questions