Where should I store a foreign key?

后端 未结 4 644
猫巷女王i
猫巷女王i 2021-01-31 09:52

If I have a relationship between two tables (both tables have their own primary keys) what should guide my decision as to which table should store the foreign key? I understand

4条回答
  •  误落风尘
    2021-01-31 10:24

    Which table is the child in the relationship?
    Answer that, and you know which table needs the foreign key column, referencing the parent's [typically] primary key. That's for a one-to-many relationship...

    A many-to-many would require you to add a third table, using the keys from both of the two tables as it's primary key.

提交回复
热议问题