My task is to build a simple database to hold information on IT infrastructure at my work.
I know very little about databases, but it’s not critical, it’s only going to be used by my team. I can’t go into the specifics as they’re confidential and also somewhat technical, so I’ll use a simple analogy.
Say I need to build a database holding information on various animals in an animal shelter. For the purposes of the analogy, each animal could easily have all its information held in a single table without redundancy. The fields for this table could be, say,
ANIMAL_ID, ANIMAL_NAME, SPECIES, NUMBER_LEGS, NUMBER_TOES... (bear with me, here).
Now, lets say this shelter only keeps two species, dogs and birds. Obviously you can limit NUMBER_LEGS to accept values from 0-4 only. However,
in the case of birds, they would never have more than two legs.
Similarly, a bird with only one leg (the other having been bitten off by a dog) would only have a maximum of 3 toes rather than the usual six (whereas a dog might have anything from 1 to twenty toes).
I plan to have a limit on this at the application (or even presentation) layer of the program that will access this data. It will take the form of a decision tree
(eg. choose animal, then choose number of legs, then number toes, etc). However, I am wondering if:
A) If it is typical to enforce these constraints at the data level, so that it is impossible for the database to hold, say,
a bird with three legs, or a three-legged dog with 20 toes.
B) If A is true, how would I go about doing this in a generic relational database?
(Note that although number of toes and number of claws are both numerical fields, this will not always be true in the real thing. I might need a set of a certain attribute (say ANIMAL_COLOUR), where possible values are BLACK, BLUE, RED, PURPLE, BRINDLE etc. But then a DOG cannot have the COLOUR value PURPLE; meanwhile a BIRD can be PURPLE or BLUE but not BRINDLE.)
They are known as check constraints. An example for Oracle is: