简体   繁体   中英

composite foreign key referencing one or more columns

I have a table (tableA) that joins 3 other tables with primary keys 'vehicle','engine','transmission' I would like to be able to assign parts to one or more of these eg 'only this vehicle' or 'only this vehicle with this engine' or 'any vehicle with this engine'

My plan is to have a parts table (tableB) with also primary keys 'vehicle','engine','transmission' and I'd like to be able to insert for example:

  • 4844, null, null to assign a part to only 'vehicle' or
  • 4844, 240, null to assign a part to 'only this vehicle with this engine'.

Is there some way I can enforce integrity at the database level so that.

  1. the fields that are filled in in tableB must reference fields in tableA.
  2. at least one of the fields must be filled in.
  3. the option exists not to insert data into them all?
  1. read up on data model patterns. there are several good books (hay, fowler, silverston, blaha) and hay covers this kinda stuff well

  2. if you want to do this sort of stuff, please use a real database like postgres that has check constraints. for your questions 2 and 3, this is easily solved with checks.

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