I have a table tree
:
+------+-------+ | id | name | +------+-------+ | 0 | tree1 | | 1 | tree2 | | 2 | tree3 | | 3 | tree4 | +------+-------+
A table pen
:
+------+------+ | id | name | +------+------+ | 0 | pen1 | | 1 | pen2 | | 2 | pen3 | | 3 | pen4 | +------+------+
And a third table task
that “attach” a task to either a tree or a pen:
+------+------+-------+ | type | id | name | +------+------+-------+ | 0 | 1 | foo | | 0 | 2 | bar | | 1 | 1 | fee | | 1 | 2 | beer | +------+------+-------+
When type
is 0, it means id
reference a tree
. When type
is 1, it reference a pen
(and so on with many different tables).
How can I do this and ensure referential integrity?