I am designing a database which needs to store some fairly complex relationships among many different types of entities.
In the simplest example, let us say I have three types of entities - "Student", "Tutor", and "Course", each with their own tables (student
, tutor
, course
). First of all, I need to represent simple many-to-many relationships between each possible pair of entity types:
student_course
- Each student can be enrolled in zero or more courses, and each course can have zero or more students. (ex: "Alex is enrolled in Biology").student_tutor
- Each student can be working with zero or more tutors, and each tutor can be assigned to zero or more students. (ex: "Emma is assigned to Alex").course_tutor
- Each tutor can be approved to teach zero or more courses, and each course can have zero or more approved tutors. (ex: "Emma knows how to tutor Physics").
Easy enough. However, I also need to represent some more advanced relationships, like:
- "Tutor John is working with students Alex and Maria on course Chemistry"
- "Tutor John is working with student Alex on course Algebra"
Notice that these are not captured by the binary many-to-many relationships I described earlier - John can be tutoring Maria, and Maria can be enrolled in History, but that doesn't mean that John is tutoring Maria in History.
The way I see it, I have two options:
Option 1 - A single table containing triplets
Table student_course_tutor
:
| student_id | course_id | tutor_id |
|------------|-----------|----------|
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 5 |
This is nice because it explicitly lays out the full relationship in a single row. However, I now need to maintain this table in addition to my simple relationship tables.
Option 2 - Meta-relationships
Table student_course
:
| id | student_id | course_id |
|----|------------|-----------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
Table student_course__tutor
:
| student_course_id | tutor_id |
|-------------------|-----------|
| 1 | 1 |
| 1 | 2 |
| 2 | 5 |
Since I was going to have a student_course
table anyway, adding this "meta-relationship" table is less redundant than representing the full triplet. However, it now becomes a lot more abstract and difficult to understand.
Which way is more correct?