Pregunta

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

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top