I have a table called Student and it is used to save the relationship between Class and Student
Class |
Student (PK) |
Class 1 |
Peter |
Class 1 |
Harry |
Class 2 |
Sally |
Class 2 |
Tommy |
And there is another table called Teacher
Teacher(PK) |
John |
Emma |
Chris |
Finally, a many to many table is used for authorization and describe the responsibility of each teacher. For example, John can access the profile and Academic performance of every student of Class 1
Teacher (PK) |
Class(PK) |
John |
Class 1 |
Chris |
Class 2 |
Emma |
Class 2 |
The basic structure is work properly. However, Chris, who should be teacher of class 2 but he needs to access the profile of Peter from Class A because of special reason. Therefore, I try to update the table structure.
Teacher(PK) |
Class(PK) |
Student |
John |
Class 1 |
Null |
Chris |
Class 1 |
Peter |
Chris |
Class 2 |
Null |
Emma |
Class 2 |
Null |
Error is thrown because the key "Chris-Class1" is duplicated but I can't set student as part of primary key because nullable is not allow. There are a few methods that can be solved:
- Add an auto increment field can remove the primary key setting of teacher and class
Index(PK) |
Teacher |
Class |
Student |
1 |
John |
Class 1 |
Null |
2 |
Chris |
Class 1 |
Peter |
3 |
Chris |
Class 2 |
Null |
4 |
Emma |
Class 2 |
Null |
- Or separate it to two tables
Teacher (PK) |
Class(PK) |
John |
Class 1 |
Chris |
Class 2 |
Emma |
Class 2 |
Teacher(PK) |
Class(PK) |
Student(PK) |
Chris |
Class 1 |
Peter |
I would like to ask which approach should be better to handle this case?