What is the name for the technique of using a set of foreign keys in a table where all but one are NULL for a given row?
In other words, each row needs a foreign key to one (and only one) of n different possible tables so you actually have all the necessary foreign keys but all but one are NULL.
(users of Django may recognize this as an alternative to using a Generic Foreign Key)
A table can have many foreign keys. A foreign key is nullable if any part is nullable. A foreign key value is null if any part is null.
Foreign keys allow key values that are all NULL , even if there are no matching PRIMARY or UNIQUE keys. By default (without any NOT NULL or CHECK clauses), the FOREIGN KEY constraint enforces the match none rule for composite foreign keys in the ANSI/ISO standard.
A foreign key may not be null when it is part of a composite primary key in the child table.
@MartinSmith: Well, the question is "Does a foreign key constraint automatically disallow nulls?" and the answer is "No", because "A FOREIGN KEY constraint can contain null values".
The term for the design you're describing is Exclusive Arc.
Instead, I prefer to make one foreign key that references a common super-table of your n different parent tables.
See my other answers for polymorphic associations:
It would have been easier with a example, but a common way to untangle this is simply to find a common super-type for those tables. Suppose that we have tables Book, Article, Magazine
and now a table has to have foreign key to these tables. Using a common super-type Publication
resolves this. See here for the model and the similar question/answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With