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

谁说胖子不能爱 提交于 2019-12-10 22:45:26

问题


In a One to Many association between a Parent Table 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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4857935/having-a-foreign-key-as-the-primary-key-in-a-child-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!