简体   繁体   English

两个主键引用一个外键

[英]Two primary key reference to a foreign key

In our database system, we have student and personnel fields. 在我们的数据库系统中,我们有学生和人事领域。 They have PersonnelCardId and StudentCardId and we made them primary key. 它们具有PersonnelCardId和StudentCardId,我们将它们设置为主键。 Then we have a payment device which holds cardID and we declared that it is a foreign key. 然后,我们有一个持有cardID的付款设备,并声明它是外键。 And we couldn't insert a row in that case and after a research we learned that we can't reference two primary key to a foreign key. 在这种情况下,我们无法插入行,经过研究,我们得知我们无法将两个主键引用到外键。 So how can we fix this situation? 那么我们如何解决这种情况呢? Thanks. 谢谢。

As you've stated, a foreign key can't reference two different tables at the same time. 如您所述,外键不能同时引用两个不同的表。 With your current schema, the cardID foreign key can only reference either PersonnelCardId or StudentCardId ... 使用当前架构, cardID外键只能引用PersonnelCardIdStudentCardId ...

If I understand correctly, your schema looks something like: 如果我理解正确,则您的架构如下所示:

PersonnelCard
----------------
PersonnelCardID - PK
PersonnelCardFields


StudentCard
----------------
StudentCardID - PK
StudentCardFields


PaymentDevice
----------------
PaymentDeviceID - PK
CardID - FK

To resolve this, you'll have to find a way to combine the PersonnelCard and StudentCard tables... 要解决此问题,您必须找到一种方法来组合PersonnelCardStudentCard表...

Card
----------------
CardID - PK
CardType - (Personnel or Student)
CardFields

The FK on the PaymentDevice table can now reference the PK in the new Card table. 现在, PaymentDevice表上的FK PaymentDeviceCard表中的PK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM