简体   繁体   中英

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. Then we have a payment device which holds cardID and we declared that it is a foreign key. 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 ...

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...

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

The FK on the PaymentDevice table can now reference the PK in the new Card table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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