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

后端 未结 8 1319
执念已碎
执念已碎 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:08

    Im having trouble understanding what the actual question is.

    Your analysis is for the most part correct, in that if you have a 2 tables, and table2 has a foreign key to table one, it could be either a one-to-one or a many-to-one.

    Your sentence "And in a one-to-many relationship the table contains many foreign keys."

    The table of the 'many' side still contains one column that is a foreign key, its just that more than one row can have the same foreign key value (many rows point to one parent).

    Also note that you can put the foreign key on the parent table, to the child, instead of the other way around. In this way, you can prevent one-to-many if you want to do that. Also note that in this way, more than one parent can share a child, which might or might not be what you want.

提交回复
热议问题