I have a table in my database (TableA) that has a column (TableA.Column1) that only allows values from certain rows of another table (TableB.Column2). This is very similar to a normal foreign key relation except for the fact that only certain rows from TableB.Column2 are allowed. For example, I may only allow rows from TableB.Column2 where TableB.Column3 > 100;
Is there a way to express this type of referential integrity in a database? I’ve tried adding a where clause to a foreign key and I’ve tried adding a subquery to a Check constraint. I’ve not gotten either to work.
Does anyone have any advice?
The answer may differ depending on the database system you are using. But an option is certainly to use a trigger.