Relational Theory – Unique Relationship Attribute for Every Pair of Entities

relational-theoryunique-constraint

I have entities "Student" and "Course" and they are connected with relationship many-to-many called "Exam". The relationship "Exam" has an atribute "Grade". For this ER model, I have made a relational schema:

Student(StudentID, StudentName, DoB)
Course(CourseID, CourseName)
Exam(StudentID, CourseID, Grade)

Can someone explain how would you put in a relational schema the fact that a student can't get multiple grades for a single course? For example, we can't have records like (Student1, Course1, Grade1) and (Student1, Course1, Grade2).

Best Answer

In a relational database every table should have a Primary Key.

A Primary Key can be one column, like StudentId, or it can be a Compound Key, like (StudentID, CourseID), which is exactly what you should use for Exam.

Then Exam's Primary Key guarantees that "a student can't get multiple grades for a single course", because that would require multiple rows with the same (StudentID, CourseID) values.