Databases · Junior · Question 4 of 75
How do you model a many-to-many relationship, such as students and courses?
- 1By adding a
student_idcolumn tocoursesand acourse_idcolumn tostudents - 2By storing a comma-separated list of course IDs in the
studentstable - ✓Correct answer: With a junction table, e.g.
enrollments(student_id, course_id), holding a foreign key to each side
Explanation
The junction table can also hold data about the relationship itself, like the enrollment date or a grade. A comma-separated list can't be indexed, joined or constrained properly.