开发者

Having a foreign key as the primary key in a child table

开发者 https://www.devze.com 2023-02-07 15:45 出处:网络
In a One to Many association between a Parent T开发者_开发技巧able and Child Table. Is it ok to make the Foreign key on the Child table as the Primary key on the same child table.

In a One to Many association between a Parent T开发者_开发技巧able and Child Table.

Is it ok to make the Foreign key on the Child table as the Primary key on the same child table.

Just working my way through DBMS and would appreciate expert views on these type of designs.

What are the Pros and Cons?


If you mean one parent row maps to many child rows, then you won't be able to do that. Primary key values come with unique key constraints; if you need multiple child rows to reference the same parent, setting the foreign key reference as the primary key will disallow that, because you won't be able to have more than one row with the same foreign key reference.


That would create a 1:0-1 relationship, not a 1:0-n, since defining the foreign key that relates the child to the parent as the child's primary key would prevent you from having more than one child with the same parent key.

0

精彩评论

暂无评论...
验证码 换一张
取 消