A word about subtypes. The proper way to implement constraints for subtypes would be to use assertions (CREATE ASSERTION
), but it is still not available in major DBs. I am using FKs
instead, and as all other substitute methods it is not perfect. People argue a lot, on SO and SE-DBA, what is better. I would encourage you to check other methods too.
-- Theme THM, of theme-type THM_TYP exists.
--
theme {THM, THM_TYP, -- other_common_attributes}
PK {THM}
SK {THM, THM_TYP}
CHECK THM_TYP in ('F', 'P')
-- Free theme THM (of theme-type 'F') exists.
--
free_theme {THM, THM_TYP,
-- other attributes specific to free}
PK {THM}
FK {THM, THM_TYP} REFERENCES theme {THM, THM_TYP}
CHECK (THM_TYP = 'F')
-- Paid theme THM (of theme-type 'P') exists.
--
paid_theme {THM, THM_TYP,
-- other attributes specific to paid}
PK {THM}
FK {THM, THM_TYP} REFERENCES theme {THM, THM_TYP}
CHECK (THM_TYP = 'P')
Note:
All attributes (columns) NOT NULL
PK = Primary Key
AK = Alternate Key (Unique)
SK = Proper Superkey (Unique)
FK = Foreign Key
Following formal normalization rules will not get you to this solution. Both, free_theme and paid_theme tables have the FD {} --> {THM_TYP}
; in other words attribute THM_TYP
does not depend on the PK {THM}
, hence these tables are not in 2NF.
In this case FK
and CHECK
constraints prevent anomalies and logical errors -- which is the objective of the normalization in the first place.
If you have a problem with tables not being in 2NF, here is a way to think about this:
- Make sure that free_theme and paid_theme tables are in high NF (5NF) without the
THM_TYP
attribute.
- Add
THM_TYP
in order to deal with the problem, and understand the compromise.
- Remember that the root cause of the problem is the lack of the required cross table constraint (assertion) in current SQL implementations.