Lets suppose I have a scenario with the following model: An Animal table which represents any animal, a Dog table and a Bird table, each one with a 1:1 relationship with the Animal table.
Animal
INTEGER id (PK)
STRING name
Bird
INTEGER id (PK FK referencing `Animal.id`)
Dog
INTEGER id (PK FK referencing `Animal.id`)
(I’m giving only the keys just to be clear)
How can I guarantee that a given row in the Animal table will have JUST one referenced row in either the Dog or the Bird table? The model itself allows it…
An animal can’t be a Dog and a Bird at the same time (not in mythology but that’s not the case 😛)
It would be better if this could be done just by playing with the model, without triggers…
Any tip would be appreciated 🙂
This may be what @Pranay meant, but the answer was incomplete. Add a column TYPE to all tables and then constrain it like this:
See David Portas’s blog fora good explanation of this.