Let’s say you’re operating a zoo, and you have this simple constraint:
You can put many animals of the same type into a single cage; or distribute them among many cages; but you cannot mix animals of different types within a single cage.
This rule prevents, for example, assigning a zebra to live in the same cage as a lion. Simple, right?
How do you enforce it? Any ideas yet? Keep reading: I will present a solution that uses a generalization of the standard UNIQUE constraint.
(Don’t dismiss the problem too quickly. As with most simple-sounding problems, it’s a fairly general problem with many applications.)