DBMS & SQL chapters
  1. 01Database Concepts & Relational Architecture
  2. 02Comprehensive SQL Engine & Joins
  3. 03Functional Dependencies & Normalization
  4. 04Transactions & Concurrency Control
  5. 05Indexing, B+ Trees & NoSQL Systems

Chapter 3 · Database Systems

Functional Dependencies & Normalization

5 topics in 1 parts

Full lessonPython Programming · Functions

Key ideas

External View 1 (Web User Portal) External View 2 (Finance & Billing) External View 3 (Analytics & BI) ↕ LOGICAL DATA INDEPENDENCE ↕ CONCEPTUAL / LOGICAL SCHEMA LEVEL Entities, Relational Tables, Attributes, Constraints, Foreign Keys & Business Rules ↕ PHYSICAL DATA INDEPENDENCE ↕ INTERNAL / PHYSICAL STORAGE LEVEL B+ Tree Indexes, Hashing, Page Blocks, Compression, Record Offsets & RAID Storage
Functional Dependencies & Normalization
Evaluation ParameterStandard Relational PatternProduction Optimization Impact
Storage Engine LayerDisk page blocks & buffer pool cacheMinimizes random disk I/O via sequential prefetching
Index UtilizationB+ Tree logarithmic search $O(\log N)$Avoids full table scans across millions of rows
Concurrency & LocksRow-level shared (S) and exclusive (X) locksPrevents race conditions while maximizing read throughput
Failure RecoveryWrite-Ahead Logging (WAL) / Undo-RedoGuarantees durability and instant crash recovery

Conceptual Foundation & Core Theory

In the Relational Model, Relational Keys are foundational constraints used to uniquely identify individual tuples (rows) within a relation (table) and enforce Referential Integrity across distinct relations.

1. The Hierarchy of Keys

  • Super Key (SK): Any set of one or more attributes that collectively identify each row uniquely in a table. A super key may contain extraneous or redundant attributes (e.g., {Roll_No, Phone_No} is a valid Super Key even though Roll_No alone is already unique).
  • Candidate Key (CK): A minimal Super Key with no redundant attributes. If any attribute is removed from a candidate key, it ceases to be unique. A table can possess multiple candidate keys (e.g., Roll_No, Registration_No, Email).
  • Primary Key (PK): The single Candidate Key chosen by the Database Administrator to uniquely identify tuples in the table. By standard SQL rules, a Primary Key column strictly prohibits NULL values and must contain distinct values across all rows.
  • Alternate (Secondary) Key: Any candidate key that was not chosen as the Primary Key.
  • Foreign Key (FK): An attribute (or set of attributes) in one table that references the Primary Key (or unique key) of another table, establishing a parent-child relationship and preventing orphan rows.

Executable Code Blueprint & Implementation (DBMS & SQL)

-- Relational Keys Implementation in SQL DDL
CREATE TABLE Departments (
    dept_id INT PRIMARY KEY,              -- Primary Key
    dept_name VARCHAR(50) NOT NULL UNIQUE -- Alternate Key
);

CREATE TABLE Students (
    student_id INT,                      -- Candidate Key 1
    roll_no VARCHAR(20),                 -- Candidate Key 2
    email VARCHAR(100),                  -- Candidate Key 3
    phone VARCHAR(15),
    dept_id INT,                         -- Foreign Key referencing Departments
    
    -- Designate Primary Key
    CONSTRAINT pk_student PRIMARY KEY (student_id),
    
    -- Designate Alternate Keys with UNIQUE constraints
    CONSTRAINT uq_roll UNIQUE (roll_no),
    CONSTRAINT uq_email UNIQUE (email),
    
    -- Foreign Key Constraint enforcing Referential Integrity
    CONSTRAINT fk_student_dept FOREIGN KEY (dept_id) 
        REFERENCES Departments(dept_id) 
        ON DELETE CASCADE ON UPDATE CASCADE
);
SUPER KEY (Superset of attributes uniquely identifying a tuple) CANDIDATE KEY (Minimal Super Key with NO redundant attributes) PRIMARY KEY Selected by DBA (NOT NULL + UNIQUE) e.g. Student_ID (Roll_No) ALTERNATE / SECONDARY Unchosen Candidate Keys e.g. Passport_No, Email
Functional Dependencies & Normalization

Relational Key Types Comparison Matrix

Key TypeDefinitionAllows NULL?Multiplicity in TableMinimality Requirement
Super KeyAny attribute combination guaranteeing unique rowsDependent on attributesUnlimited possible subsetsNo (can contain redundant columns)
Candidate KeyMinimal super key with zero redundant attributesStrictly UNIQUE (rarely null)One or more per tableYes (Strictly minimal)
Primary KeyThe designated unique identifier chosen by DBASTRICTLY NO (NOT NULL enforced)Exactly ONE per tableYes (Must be a Candidate Key)
Foreign KeyReferences Primary Key of another relationYes (unless defined NOT NULL)Zero, one, or multipleMatches referenced parent key

Relational Design Interview Problem: Minimal Super Key Derivation

Question: Given a relation $R(A, B, C, D)$ with functional dependencies $A \to B, B \to C, C \to D$. Find all candidate keys. Answer: Compute attribute closure of $A$: $A^+ = \{A, B, C, D\}$. Since $A^+$ includes all attributes of $R$, $A$ is a Super Key. Since $A$ is a single attribute, no subset can be removed, making $A$ minimal. Therefore, $A$ is the sole Candidate Key.
INNER JOIN Matching in BOTH LEFT JOIN ALL Left + Matched Right FULL OUTER JOIN ALL Left + ALL Right
Functional Dependencies & Normalization
Normal FormPre-requisite ConditionAnomaly / Flaw EliminatedMathematical Rule
1NFRelational Table FormatMulti-valued & nested listsAll domain attribute values must be atomic
2NFMust be in 1NFPartial dependencies on composite keysNon-prime attributes fully functionally dependent on whole key
3NFMust be in 2NFTransitive dependencies ($A \to B \to C$)In $X \to Y$, either $X$ is a superkey or $Y$ is a prime attribute
BCNFMust be in 3NFAll functional dependency anomaliesIn every non-trivial $X \to Y$, $X$ MUST be a superkey

Database Interview Problem: Finding the Highest Normal Form

Problem: Given $R(A, B, C, D)$ with functional dependencies $AB \to CD, C \to D$. The candidate key is $AB$. Identify the highest normal form. Answer: Attributes are: prime = $\{A, B\}$, non-prime = $\{C, D\}$. In dependency $C \to D$, $C$ is NOT a superkey, and $D$ is a non-prime attribute depending on non-prime attribute $C$. This is a transitive dependency. Furthermore, $C$ does not contain $A$ or $B$, so it is not partial. Hence, $R$ is in 2NF, but fails 3NF.

Checkpoint

1. What distinguishes a Candidate Key from a general Super Key?

2. A relational table is in 2NF. Which condition is strictly required for it to also satisfy 3NF?

All 5 topics in Functional Dependencies & Normalization

  1. Normalization

    • Functional Dependencies
    • Candidate Keys Algorithm
    • 1NF & 2NF Rules
    • 3NF vs BCNF
    • Lossless Decomposition