Question

can we make pair of values unique in Junction Table? two tables (Students and classrooms) have many to many relationship. I have created a junction table which contains Studentid and Classroom id only. But i do not want same pair to be enetered twice. Forexample (student id:33 Classroom id:5). Please help!!!

Was it helpful?

Solution

Add a unique index on both columns:

ALTER TABLE `junction` ADD UNIQUE (
`student_id` ,
`classroom_id`
)

If you try to enter the same combination twice an error will occur on mysql side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top