Foreign key declaration can be replaced by its equivalent check constraint True or False?

StackOverflow https://stackoverflow.com/questions/22115371

  •  18-10-2022
  •  | 
  •  

Question

I wanna know that a foreign key declaration can be replaced by its equivalent check constraint.. If yes why? I tried to google it but couldn't find the answer Word of advice please...

Was it helpful?

Solution 2

Yes you can replace foreign key with check constraint but remember to use Enums

OTHER TIPS

It depends on what you mean by replaced.

At the conceptual level, you can consider a foreign key constraint to be roughly a set of valid values. And, at the conceptual level, you can consider an equivalent check constraint to be roughly a set of valid values.

If that's all you mean by replaced, then you can replace a foreign key constraint with an equivalent check constraint.

But the set of valid values for a foreign key constraint can be changed with SQL DML: SQL insert, update, delete statements. The set of valid values for an equivalent check constraint cannot be changed that way. You have to use SQL DDL to change a check constraint.

Also, different users might have different privileges on the referenced table of a foreign key constraint. Some might have only insert privileges, some might have insert and update privileges, etc. You can't control changes to a check constraint that way. A user that's allowed to change the check constraint can change it in any way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top