Section 9 · 9 min read
Integrity constraints and normal forms
Keys and integrity constraints, functional dependencies, attribute closure, minimal cover, finding candidate keys, the 1NF to BCNF tests, and lossless and dependency-preserving decomposition.
On this page
Integrity constraints
- Domain constraint: each value comes from the attribute's domain (type, range, CHECK).
- Key constraint: no two tuples agree on a candidate key.
- Entity integrity: primary key attributes cannot be NULL.
- Referential integrity: a foreign key value must either be NULL or match the primary key of some tuple in the referenced relation. When a referenced tuple is deleted, the action can be RESTRICT/NO ACTION (reject), CASCADE (delete referencing tuples, recursively), SET NULL or SET DEFAULT.
Inserting into the referencing table and deleting from the referenced table are the operations that can violate referential integrity. Deleting from the referencing table or inserting into the referenced table never can.
Keys
- Superkey: a set of attributes whose closure is the whole relation.
- Candidate key: a minimal superkey (no proper subset is a superkey).
- Primary key: the candidate key chosen by the designer.
- Prime attribute: an attribute that belongs to at least one candidate key. All others are non-prime.
Counting superkeys of with attributes:
- One candidate key of size : superkeys.
- Two candidate keys , : (inclusion-exclusion).
Functional dependencies
holds if any two tuples that agree on also agree on . An FD is a statement about every legal instance, so a single instance can disprove an FD but cannot prove one.
Armstrong's axioms
- Reflexivity: if then .
- Augmentation: if then .
- Transitivity: if and then .
Derived rules: union (, give ), decomposition ( gives ), pseudotransitivity (, give ). Note that does not imply .
Attribute closure
is every attribute determined by . Start with ; repeatedly, for any FD with , add . Stop when nothing changes. is in exactly when .
Minimal (canonical) cover
A minimal cover of is equivalent to and has:
- A single attribute on every right side.
- No extraneous attribute on any left side: for , is extraneous if (computed with the full set).
- No redundant FD: is redundant if computed without that FD.
Do the steps in this order. A set can have more than one minimal cover.
Finding candidate keys
- Attributes that appear on no right side must be in every key. Call this set (the core).
- Attributes that appear only on right sides are in no key.
- If is everything, is the only candidate key.
- Otherwise try adding the remaining attributes to , smallest sets first, and keep minimal sets whose closure is everything.
- Shortcut: once is a key, if some FD has , then is a superkey; reduce it to find another key.
Normal forms
| Form | Condition |
|---|---|
| 1NF | All attribute values are atomic (no repeating groups, no sets) |
| 2NF | 1NF, and no non-prime attribute depends on a proper subset of any candidate key |
| 3NF | For every non-trivial : is a superkey or is prime |
| BCNF | For every non-trivial : is a superkey |
Each form implies the ones above it: BCNF 3NF 2NF 1NF.
Quick facts:
- A relation whose candidate keys are all single attributes is automatically in 2NF.
- A relation where every attribute is prime is automatically in 3NF.
- Any relation with exactly two attributes is in BCNF.
- To find the highest normal form, compute all keys first, then test BCNF, then 3NF, then 2NF, using every FD (including ones in that matter, such as those derived on a projection).
Decomposition
Decompose into to remove redundancy. Two properties matter.
Lossless join
Joining the parts gives back exactly , with no spurious tuples. For two parts the test is:
that is, the common attributes form a superkey of at least one side. For three or more parts, either merge two at a time using this test or use the tableau (chase) method: one row per part, distinguished symbols in that part's columns, then apply FDs to equate symbols; the decomposition is lossless if some row becomes fully distinguished.
Dependency preservation
Let be the FDs of that use only attributes of . The decomposition preserves dependencies if . Practical check: for each in , compute using only the projected FDs, and see if it contains .
What can always be achieved
- 3NF synthesis (from a minimal cover, one table per FD group, plus a key table if needed) is always lossless and dependency-preserving.
- BCNF decomposition is always lossless but may lose dependencies.
Worked examples
Worked example 1: candidate keys
with .
- Never on a right side: C and D. Core , and , not a key.
- : gives B, gives E. Key.
- : , then . Key.
- : , then . Key.
Any 4-attribute set containing C and D includes one of these, so the candidate keys are ACD, BCD, CDE. Every attribute is prime, so R is at least in 3NF. It is not in BCNF because has a left side that is not a superkey.
Worked example 2: minimal cover
.
- Right sides are already single attributes.
- Left sides: in , contains C, so B is extraneous: . In , likewise D is extraneous: again, a duplicate.
- Redundancy in : without , lacks C, so keep it. The others are needed for the same reason.
Minimal cover: .
Worked example 3: highest normal form
, .
Keys: B is never on a right side. . is everything; : , then , everything. , . Candidate keys: AB and BC. Prime: A, B, C. Non-prime: D, E.
- BCNF: fails (C is not a superkey).
- 3NF: is fine (A is prime), but has D not a superkey and E non-prime. Fails.
- 2NF: do D or E depend on a proper subset of a key? , , . No. Passes.
Highest normal form: 2NF.
Worked example 4: BCNF can lose a dependency
, . Keys: AB and AC. All attributes are prime, so R is in 3NF. violates BCNF.
Decompose on : and . and , so C is a key of : lossless. But cannot be checked inside either table: has no A, has no B. With only available, . The dependency is lost. This is the standard reason 3NF is sometimes preferred over BCNF.
Worked example 5: lossless test for three tables
, , decomposition .
Join and first: common A, and makes A a key of AB. Lossless, giving ABE. Join ABE with BCD: common B, and , so B is a key of BCD. Lossless. Whole decomposition is lossless.
By contrast, under is lossy: the common attribute C determines nothing ().
Common traps
- Concluding an FD holds because it holds on one sample instance. An instance can only refute an FD.
- Splitting left sides: does not give .
- Forgetting a candidate key. Always use the core attributes and the replacement trick to find all keys before testing normal forms.
- In 2NF, checking partial dependencies of prime attributes. 2NF only restricts non-prime attributes.
- Mixing up 3NF and BCNF: 3NF allows with non-superkey X when A is prime; BCNF does not.
- Assuming BCNF decomposition preserves dependencies. It is only guaranteed lossless.
- Testing losslessness for three parts by checking pairs that share nothing. Pick an order where each join has a key on one side, or use the chase.
- Removing redundant FDs before removing extraneous left attributes when computing a minimal cover.
Quick revision
- by repeated firing of FDs; iff .
- Attributes never on a right side are in every key; attributes only on right sides are in none.
- Superkeys with one key of size k in n attributes: .
- 2NF: no partial dependency of a non-prime attribute. 3NF: LHS superkey or RHS prime. BCNF: LHS superkey.
- Two-attribute relation: always BCNF. All attributes prime: at least 3NF. All keys single-attribute: at least 2NF.
- Lossless binary split: common attributes are a key of one side.
- 3NF synthesis: lossless and dependency-preserving. BCNF: lossless, may lose dependencies.
- Minimal cover: singleton RHS, drop extraneous LHS attributes, drop redundant FDs.