Integrity Constraints, Functional Dependencies and Normal Forms
Domain, key, entity and referential integrity constraints with cascading actions, update anomalies, functional dependencies and Armstrong's axioms, attribute closure and finding all candidate keys, minimal covers, the normal forms 1NF, 2NF, 3NF and BCNF (and 4NF), lossless-join and dependency-preserving decomposition, and the 3NF synthesis and BCNF decomposition algorithms.
Integrity constraints
Constraints are rules the DBMS enforces on every insert, update and delete.
| Constraint | Meaning | SQL |
|---|---|---|
| Domain | values come from the attribute's type/range | types, CHECK (age >= 18) |
| Not null | value required | NOT NULL |
| Key (uniqueness) | no two tuples share the key | PRIMARY KEY, UNIQUE |
| Entity integrity | primary key attributes are never NULL | implied by PRIMARY KEY |
| Referential integrity | every foreign key value matches an existing referenced key (or is NULL) | FOREIGN KEY ... REFERENCES |
| General assertions / triggers | arbitrary conditions or actions | CHECK, CREATE ASSERTION, triggers |
Referential actions when a referenced row is deleted or its key updated: RESTRICT/NO ACTION (reject), CASCADE (propagate), SET NULL, SET DEFAULT. Inserting a referencing row with a non-existent key is always rejected.
Why normalise? Anomalies
A badly designed table EmpDept(eid, ename, dept, dept_head) repeats dept_head for every employee of a department:
- Update anomaly: changing a department head requires updating many rows (or the data becomes inconsistent).
- Insertion anomaly: a new department cannot be recorded until it has an employee.
- Deletion anomaly: deleting the last employee of a department loses the department's head.
Normalisation removes redundancy by decomposing relations using functional dependencies.
Functional dependencies
means: any two tuples that agree on also agree on . It is trivial if .
Armstrong's axioms (sound and complete):
- Reflexivity: .
- Augmentation: .
- Transitivity: .
Derived rules: union (), decomposition (), pseudo-transitivity ().
Attribute closure — the workhorse
= all attributes functionally determined by :
def closure(X, F): # F: list of (lhs, rhs) attribute strings
result = set(X)
changed = True
while changed:
changed = False
for lhs, rhs in F:
if set(lhs) <= result and not set(rhs) <= result:
result |= set(rhs)
changed = True
return result
Uses: is a superkey iff contains all attributes; follows from iff .
Finding candidate keys
Prime attribute: belongs to some candidate key.
Minimal (canonical) cover
An equivalent set of FDs with single attributes on the right, no extraneous attributes on the left, and no redundant FDs. Steps: split right-hand sides; remove extraneous left attributes (is in ?); drop FDs implied by the others.
Normal forms
| Form | Requirement | Removes |
|---|---|---|
| 1NF | atomic values, no repeating groups | multivalued cells |
| 2NF | 1NF + no partial dependency of a non-prime attribute on part of a candidate key | redundancy from composite keys |
| 3NF | for every non-trivial : is a superkey or is prime | transitive dependencies |
| BCNF | for every non-trivial : is a superkey | all FD-based redundancy |
| 4NF | BCNF + every non-trivial multivalued dependency has a superkey | independent multivalued facts |
BCNF ⊂ 3NF ⊂ 2NF ⊂ 1NF. Shortcuts: if every candidate key is a single attribute, a 1NF relation is automatically in 2NF; if every attribute is prime, the relation is automatically in 3NF; any relation with exactly two attributes is in BCNF.
Decomposition
Replacing by (projections) must satisfy:
Lossless join — joining the pieces gives back exactly (no spurious tuples). Binary test: or (the common attributes form a superkey of one side).
Dependency preservation — every FD can be checked within a single piece: .
| Target | Lossless? | Dependency preserving? |
|---|---|---|
| 3NF (synthesis from a minimal cover, plus a key relation if needed) | always | always |
| BCNF (repeatedly split on a violating FD into and ) | always | not always |
Quick revision
Practice questions
GATE-style questions: 3 one-mark and 8 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.
Relation has functional dependencies . The number of candidate keys of is ______.
Relation has functional dependencies . The number of candidate keys of is ______.
with functional dependencies is in which highest normal form?
A relation is in Boyce–Codd normal form (BCNF) if, for every non-trivial functional dependency that holds on it,
with is decomposed into and . Which statements are true?
One or more options are correct.
For with , the number of attributes in the closure is ______.
has functional dependencies . Which statement is correct?
Which of the following are among Armstrong's axioms (the three basic inference rules)?
One or more options are correct.
Table Emp has a foreign key dept referencing Dept(dname) declared with ON DELETE CASCADE. When a row of Dept is deleted,
Relation has as its only candidate key. The number of superkeys of is ______.
Relation has functional dependencies . The number of candidate keys of is ______.