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.
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!!!
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.