Mysql – How to model (a many to many) to many relationship

database-designMySQL

Hello i am facing the following scenario. I have a student table with the following structure.

Student
id(PK), matriculation number, first_name, last_name, email

A student can have more than one Majors(BSc mechanical engineering, MSc mechanical engineering, etc…)

Major
id(PK), major

At the same time a major may be assigned to more than one students, therefore the relationship between student and major is many to many. I implemented it using the following mapping table.

MajorStudent
id(PK), student_id(FK), major_id(FK)

Every student however can sit for many exams. But for every exam it is important to know his Major. That is he can sit for the same exam as BSc mechanical engineering and as MSc mechanical engineering, but this should count as two different exams. Meanwhile every exam is assigned a grade, which can have one of four values. My exam and grade tables looks like this.

Exam
id(PK), major_student_id(FK), grade_id(FK)

Grade
id(PK), grade(value1, value2, value3, value4)

What i am trying to get at the end is the following

View
student_id | first_name | last_name |... | major      | exam_id | grade
1            test1       testtest1        BSc M.engin   1         value1
1            test1       testtest1        MSc M.engin   2         value3

Do you think my desing is correct. And in general how would you go about modeling a many to many to many relationship? (If it helps i am using MySQL and cakephp to implement it)

Best Answer

The tables student and major are good.

However following improvements can/should be made.

  • Merge the FKs in MajorStudent to a PK, getting rid of the id
  • Save a StudentFK and MajorFK rather than a MajorStudentFK in Exam, well one can discuss about this though but it'll be faster and in my consideration NOT redundant
  • Grade is not normalized. All attributes are supposed to be atomic and grade is not. Either make 4 columns (bad) or use the following

    Exam(id[PK], studentId[FK], MajorId[FK]) Grades(id[PK], examId[FK], value)

I don't understand why one exam comes with four grades though.