gpt4 book ai didi

sql - 两个主键引用一个外键

转载 作者:搜寻专家 更新时间:2023-10-30 21:51:59 25 4
gpt4 key购买 nike

在我们的数据库系统中,我们有学生和人员字段。他们有 PersonnelCardId 和 StudentCardId,我们将它们设为主键。然后我们有一个持有 cardID 的支付设备,我们声明它是一个外键。在那种情况下我们不能插入一行,经过研究我们了解到我们不能将两个主键引用到一个外键。那么我们如何解决这种情况呢?谢谢。

最佳答案

如您所述,一个外键不能同时引用两个不同的表。使用您当前的架构,cardID 外键只能引用 PersonnelCardIdStudentCardId...

如果我没理解错的话,你的架构看起来像这样:

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


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


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

要解决这个问题,您必须找到一种方法来组合 PersonnelCardStudentCard 表...

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

PaymentDevice 表中的 FK 现在可以引用新的 Card 表中的 PK。

关于sql - 两个主键引用一个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13993079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com