I have a relationship/linking table which is intended to hold relationships against more than one base table. that is, we have this general idea of Tags
which can be applied to several different entities, let's say Company
, Building
, and Customer
. Each of these entities has a unique Id
column that's an identity col. I have logically assigned the 3 base entities an enumeration value, so Company
is 1
, Building 2
, and Customer is 3
.
What I've done, then, is generically associate tags based on a logical compound key of entity type and entity id.
EntityType EntityId TagId
-----------------------------
1 99 22
I want to enforce that if entity type is 1, representing a company record, that there is a record in the company table with that id (99, in the above example). Of course I can't add a typical foreign key to the company table's Id column because not all of the linking table records refer company records. I need something that checks the correct base table based on the EntityType value.
I've searched but I can't find any examples for this, though I have to believe it's not uncommon. TIA.