In a school system I have 2 tables, one called Staff which holds records of all member of staff for a school, teachers, admin, cooks and cleaners etc. Then I have a second table called Course with a foreign key relating to Staff to state who is the Course leader, now I only want to allow teachers to be the Course Leader, i.e. a cook can’t be, but am not sure how to restrict this on the database level.
Note : I asked a more complicated wrong question here – Oracle Unique Constraint – Trigger to check value of property in new relation
In this scenario I would revise the data model. I would take the generic table staff and add under it tables for each STAFF_TYPE: CATERERS, TEACHERS, ADMIN, etc. Then it is a simple matter to enforce a foreign key between COURSES and TEACHERS without the need for triggers.
This is a standard solution to this kind of problem. As you further investigate the requirements you’ll find there will be similar foreign key issues with cooks and janitors. It is also likely that you will find there are attributes which teachers have that administrators lack. That’s why separate tables for each type are useful. At the same time they all have things in common. That’s why you need a table for the STAFF super type.
Of course, in proposing this answer I am echoing @JeffreyKemp’s suggestion in your previous question. Well, as @RobVanWijk has borrowed mine, why not? 🙂