سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top