Foreign Key and Referential Integrity

Context: FIT2094_MOC · an FK is a Primary Key appearing in another relation · must match a full PK or be NULL · how relationships are implemented relationally

Quick Revision

  • 🎯 Objective: a PK appearing in another relation ➔ every FK matches a full PK or is NULL.
  • 📦 Core Components: FK ➔ referential integrity ➔ implements relationships.
  • ⚡ Key Constraint: must match the full PK (composite too) or be NULL; NULL FK = optional participation.

📝 Core

1. The Foreign Key

  • Definition ➔ an attribute set that exists as a Primary Key in the same or another relation.
  • Implementsrelationships via PK–FK pairing.

2. Referential Integrity

  • Rule ➔ every FK value matches a full PK in the referenced relation, or is NULL.
  • Composite ➔ must match the entire composite PK; partial match invalid.
  • NULL FK ➔ optional participation (no related parent).

3. On-Delete Actions

  • RESTRICT (default) ➔ block deleting a parent still referenced by a child; the rule applied when no ON DELETE clause is given.
  • CASCADE ➔ deleting the parent auto-deletes referencing child rows.
  • SET NULL ➔ deleting the parent nulls the child’s FK (only if the FK column allows NULL).
  • Chosen at design time ➔ read participation from the data model — mandatory FK ⟹ RESTRICT (SET NULL would orphan it; CASCADE would destroy history).

⚙️ Core Implementation

🔹 EMP–DEPT

⚖️ Core Decision Matrix

FK stateValid?Meaning
equals a full PKrelated parent exists
NULLoptional participation
partial composite matchviolates referential integrity
dangling (no PK)rejected by RDBMS

When It Flips: a conceptual relationship becomes a concrete FK column (Conceptual vs Logical Model); a NULL FK is the circle (min 0) of Crow's Foot. Deletes of a referenced PK are restricted or cascaded.

📊 Exam Execution Trace

Manual Execution Trace

Validating EMP.deptno:

Step / StateEMP.deptnoValid?
0 (Init)
110 (exists in DEPT)
2NULL (president)
399 (no such DEPT)

Applied Exercise

Problem: Can EMP.deptno be NULL? Can it be a department not in DEPT? Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: NULL allowed; an unmatched value is rejected.

🧠 Active Recall