Say I have the following tables which implement a many to many relationship between parts and objects, where the link table has a unique key pair (object_id,part_id) such that there are no duplicate rows:
Part table:
part_id | part_name | part_zone --------+-----------+---------- 0 | part1.0 | top 1 | part2 | bottom 2 | part1.1 | top 3 | part3 | top
Object table:
object_id | object_name ---- ----+----------- 0 | object1 1 | object2 2 | object3 3 | object4
Link table:
object_id | part_id ------ --+--------- 0 | 0 0 | 1 1 | 1 1 | 2
There is a further constraint in that there should only be one of each part_zone in an object. So if the part_zone column were to be moved to the link table and the key changed to the unique pair (object_id,part_zone) then this would be satisfied. I don’t want to do this however since part_zone should be in the part table. In my head I’m looking to keep the tables as are they are, but use the (object_id,part_zone) unique key where part_zone is pulled from the part table via a join on part_id.
Hopefully my intention is clear, but I’m not too sure this is necessarily the best implementation or I’m using the right terminology for what I’m after.
Thanks in advance to anyone who can offer any insight.
You could add
part_zoneintoLink, with a cascading compound foreign key constraint referencing(part_zone, part_id)in theParttable; that way, you can define aUNIQUEconstraint on(object_id, part_zone)as suggested whilst still being able to maintainpart_zonefrom theParttable:You could even define triggers on
INSERTandUPDATEto populate the values inLinkso that you need not do it manually: