postgresql add tag
toraritte
Found the example below in a book teaching logical database design, but I'm stuck on how one would implement this in PostreSQL:

[![enter image description here][1]][1]

The diagram reflects the following business rule:

> An instructor must teach one class, but no more than eight classes.

This is what I infer from the diagram:

+ There is a 1:N (one-to-many) relationship between the `INSTRUCTORS` and `INSTRUCTOR CLASSES` tables.

+ Every record (row) in the `INSTRUCTORS` table has to have at least one corresponding record (row) in the `INSTRUCTOR CLASSES` table - and vice versa.

+ `(R)` represents the `RESTRICT` deletion rule. It appears on both sides of the relationship, so in order to delete a record (row) from the `INSTRUCTORS` table there shouldn't be any record (row) in `INSTRUCTOR CLASSES` table references it - and vice versa.

The last item is what makes my head scratch: 

1. As far as I know, deletion rules / constraints can only be specified on foreign key fields. So how would that work the other way around? Or this can only be implemented outside of PostgreSQL and in the application wrapper itself?

   Given the business rule example, it makes sense: If one deletes **all** assignment for an instructor, then it will violate the rule. (Not saying that the rule is completely sensible though...)

2. If it is possible to implement this, then how would one delete a record (row) from the `INSTRUCTORS` table? (Or the last assignment for an instructor in the `INSTRUCTOR CLASSES` table.) Via transactions?

3. Should I even bother worrying about this?

   To me, it seems like a very contrived example: Yes, every instructor should teach at least one class, but if there is a new hire for example, they may not have an assigned class yet.



  [1]: https://i.stack.imgur.com/C0Ft7.png

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.