Search code examples
mysqlsqlmany-to-many

many to many relationship sql


enter image description here

What can i add to these three tables to allow a "family" to have many students and a family to have many parents?

i am struggling to get my head around what i could add which would allow me to do this.

Thanks


Solution

  • You need to add a foreign key in student and parent that maps back to family_id, Remove parent_id and student_id in the Family table. If you need to find all member of family 22

    select * from family f 
    join parent p on f.family_id=p.family_id
    join student s on s.family_id=f.family_id
    where f.family_id=22