Sub topic 2
Integrity Constraints, Foreign Keys, and Functional Dependencies
This chapter assumes you know relations (tables), tuples (rows), attributes (columns), set union and subsets, and basic CREATE TABLE and SELECT statements.
Integrity Constraints: Domain, Entity, and Referential
Domain integrity requires each value to belong to its attribute's domain, enforced by types and CHECK rules. Entity integrity requires the primary key to be unique and never NULL. Referential integrity requires every non-null foreign key value to match an existing key in the referenced table.
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
salary NUMERIC(10,2) CHECK (salary > 0),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);Foreign Keys and Referential Actions
A referential action decides what happens to referencing rows when the referenced row changes. CASCADE propagates the change, SET NULL clears the reference, and RESTRICT (or NO ACTION) rejects the change. In the schema above, deleting a department sets its employees' dept_id to NULL, and updating a department key cascades to the employees.
Functional Dependencies and Armstrong's Axioms
A functional dependency (FD) X → Y holds when any two tuples that agree on X also agree on Y. Armstrong's axioms derive all implied FDs:
- Reflexivity: if Y ⊆ X, then X → Y.
- Augmentation: if X → Y, then XZ → YZ.
- Transitivity: if X → Y and Y → Z, then X → Z.
Computing Attribute Closure
The closure X⁺ is the set of attributes determined by X. Start with X and repeatedly add the right side of any FD whose left side is already inside the result.
def closure(attrs, fds):
result = set(attrs)
changed = True
while changed:
changed = False
for lhs, rhs in fds:
if lhs <= result and not rhs <= result:
result |= rhs
changed = True
return result
fds = [({'A'}, {'B'}), ({'B'}, {'C'}), ({'C', 'D'}, {'E'})]
print(sorted(closure({'A', 'D'}, fds))) # ['A', 'B', 'C', 'D', 'E']Finding Candidate Keys from FDs
A superkey has a closure equal to all attributes. A candidate key is a minimal superkey. Attributes that never appear on the right side of an FD must belong to every key.
| Attribute | Appears on a right side? | In every key? |
|---|---|---|
| A | No | Yes |
| B | Yes | No |
| C | Yes | No |
| D | No | Yes |
| E | Yes | No |
Since A⁺ and D⁺ together give all attributes, {A, D} is the only candidate key.
Redundancy and Update Anomalies
Suppose Enrollment(student, course, teacher, dept) has FDs course → teacher and teacher → dept. Each teacher's department repeats in every enrollment row. This causes three anomalies:
- Insertion: a teacher cannot be recorded until a student enrolls.
- Update: changing a teacher's department requires editing many rows.
- Deletion: removing the last student of a course also erases the teacher's department.
Exercise
In R(A, B, C, D, E) with FDs A → B, B → C, and CD → E, is {B} a candidate key?
Show answer
No. B⁺ = {B, C}. It lacks A, D, and E, so B is not a superkey.