Difference between one-to-one and one-to-many relationship in database

后端 未结 8 1476
执念已碎
执念已碎 2021-02-01 06:13

This is probably a basic(dumb) question but when having a one-to-one relationship in a database the other table has a foreign key ID(in this example). And in a one-to-many relat

8条回答
  •  故里飘歌
    2021-02-01 07:01

    Given table A and B if

    1. A and B have a strict 1 to 1 relationship
    2. For every B instance, there will always be an A instance

    The best approach is to make the primary key of B also a foreign key referencing A. This is also called "Table per Type Inheritance" and the "is a" relationship. There are other ways to enforce a unique foreign key, but using the primary key makes the relationship clear in the schema and in ER diagrams.

    Of course there are always other scenarios, and if your design doesn't meet both of the criteria above, you'll have to use another approach.

提交回复
热议问题