Many-to-Many relationships needing an associate table

前端 未结 6 479
萌比男神i
萌比男神i 2021-01-25 10:18

In many database design tutorials/articles, they always bring up the fact that if two tables share a many-to-many relationship, then a third table should be created to act as an

6条回答
  •  终归单人心
    2021-01-25 10:58

    Let's understand it with an example. If you have two tables one for apples and other for persons and they have a many to many relationship; meaning one person can have multiple apple and one apple can be eaten by multiple person by sharing.

    So if we add a foreign key to apple table in the person table then this gives the relationship that one apple can be eaten by multiple person but we have not specified the relationship for the other way around. See this table

    Similarly, if we add a foreign key to person table in the apple table then this gives the relationship that one person can eat multiple apples but not the other relationship along with it.See this table

    So here comes the association table. Association table are used for many to many relationship between two objects. They consists of at least 2 foreign keys, each of which reference one of the two objects and the primary key in an Association table is made up of at least the 2 foreign keys. See this table

    Reference: https://openclassrooms.com/en/courses/2071486-retrieve-data-using-sql/5758019-create-an-association-table

提交回复
热议问题