how to create foreign reference key for composition primary key

你离开我真会死。 提交于 2019-12-14 02:33:07

问题


Suppose we have the following two tables

Create table Bank (
   Bank_ID Numeric Not Null,
   Bank_Card Numeric Not Null,
   Primary Key(Bank_ID, Bank_Card)
)

Create table Customer (
   Customer_ID Numeric Not Null,
   Name varchar(30) Not Null,
   Primary key(Customer_ID)
)

Where Customer_ID is generated by concatenating Bank_ID and Bank_Card. How can I set foreign key Customer_ID to reference Bank_ID and Bank_Card


回答1:


What you want is a constraint, but it's not a FK (foreign key) constraint.(A FK constraint says that values for a column list appear elsewhere as PK/UNIQUE.) To enforce it declaratively you could add redundant generated (computed/calculated) column Customer_ID to Bank and a FK to it in Customer. To enforce it without adding redundant columns you need triggers. But smart keys are a bad idea.



来源:https://stackoverflow.com/questions/45392537/how-to-create-foreign-reference-key-for-composition-primary-key

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