如何为组合主键创建外参考键 [英] how to create foreign reference key for composition primary key
问题描述
假设我们有以下两个表
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)
)
其中通过合并Bank_ID和Bank_Card生成Customer_ID的位置.如何设置外键Customer_ID以引用Bank_ID和Bank_Card
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
推荐答案
所需的是约束,但它不是FK(外键)约束.(FK约束表示列列表的值在其他地方显示为PK /UNIQUE.)要声明性地执行它,可以在Bank
中添加冗余的生成(计算/计算)列Customer_ID
,并在Customer
中添加FK.要执行它而不添加多余的列,您需要触发器.但是智能密钥是个坏主意.
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.
这篇关于如何为组合主键创建外参考键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!