I have a question regarding enforce a constraint to my table.
I have a table called workson and a table called staff, each staff has specific title(supervisor,authorizer,manager,…). I need to ensure that the supervisor and the authorizer cannot be the same staff on workson table.The cardinality between them is many to many. I am not sure how to do it.
Can you please advise me to solve this problem?
Thanks
Consider the following query
When the data in the table satisifes your constraint the above query will be the empty set i.e.
The problem is, few SQL products allow a subquery in a
CHECKconstraint.The usual workaround is to implment the same logic in procedural code e.g. using a trigger or forcing users to update data via a stored procedures (by removing update privileges on the base tables) that ensure the constraint is never violated. You have to take care to serialize updates properly without making the whole thing run like glue.
Happily, there is an excellent book on the subject:
Applied Mathematics for Database Professionals By Lex de Haan, Toon Koppelaars
They give full details on how to implement such a strategy in Oracle which can be ported to other SQL products.