Registry Synced

14 - Functional Dependencies

3402 words
17 min read

Reading compass

Now · 🎯 Learning Objectives

14 - Functional Dependencies

🎯 Learning Objectives

After reading this topic, you will be able to:
  • Define functional dependencies and explain why they matter
  • Apply Armstrong's axioms to derive implied FDs
  • Compute closure of a set of FDs (F⁺)
  • Compute attribute closure (α⁺) to find keys
  • Check equivalence of two FD sets
  • Find superkeys and candidate keys using attribute closure

📋 Prerequisites

📖 Core Content

14.1 Intuition: What Data Relationships Tell Us

A functional dependency (FD) describes a relationship between attributes in a relation. It says: If I know the value of attribute(s) X, I can determine the value of attribute(s) Y. Real-world examples:
  • {Aadhaar_number} → {name, DOB} — If I know your Aadhaar number, I know your name and date of birth
  • {student_id} → {student_name} — If I know your student ID, I know your name
  • {zip_code} → {city} — If I know the zip code, I know the city Formally: X→YX \rightarrow Y (read as "X determines Y" or "Y is functionally dependent on X")
Why This Matters: FDs are the foundation of normalization — the process of designing schemas that avoid data redundancy and anomalies. Understanding FDs is critical for good database design.

14.2 Formal Definition

Given relation RR, a functional dependency X→YX \rightarrow Y holds iff: For any two tuples t1t_1 and t2t_2 in RR:
if t1[X]=t2[X] then t1[Y]=t2[Y]\text{if } t_1[X] = t_2[X] \text{ then } t_1[Y] = t_2[Y]
In plain English: "Whenever two rows have the same X value, they must also have the same Y value." Example: In instructor(ID, name, dept_name, salary), the FD ID→nameID \rightarrow name means: if two rows have the same ID, they MUST have the same name.

14.3 Trivial vs. Non-Trivial FDs

TypeDefinitionExampleAlways True?
TrivialX→YX \rightarrow Y where Y⊆XY \subseteq XA→AA \rightarrow A , AB→AAB \rightarrow AYes
Non-trivialY⊈XY \nsubseteq XID→nameID \rightarrow nameNo (depends on data)
Completely non-trivialX∩Y=∅X \cap Y = \emptysetID→nameID \rightarrow nameNo
Trivial FDs are always true — they convey no useful information.

14.4 Armstrong's Axioms

Armstrong's axioms are a set of inference rules for deriving new FDs from existing ones. They're sound (every derived FD is true) and complete (every implied FD can be derived).
RuleNameFormal DefinitionExample
1ReflexivityIf Y⊆XY \subseteq X , then X→YX \rightarrow YAB→AAB \rightarrow A
2AugmentationIf X→YX \rightarrow Y , then XZ→YZXZ \rightarrow YZIf A→BA \rightarrow B , then AC→BCAC \rightarrow BC
3TransitivityIf X→YX \rightarrow Y and Y→ZY \rightarrow Z , then X→ZX \rightarrow ZIf A→BA \rightarrow B and B→CB \rightarrow C , then A→CA \rightarrow C

Derived Rules

RuleDerivationExample
UnionIf X→YX \rightarrow Y and X→ZX \rightarrow Z , then X→YZX \rightarrow YZA→BA \rightarrow B and A→CA \rightarrow C → A→BCA \rightarrow BC
DecompositionIf X→YZX \rightarrow YZ , then X→YX \rightarrow Y and X→ZX \rightarrow ZA→BCA \rightarrow BC → A→BA \rightarrow B and A→CA \rightarrow C
PseudotransitivityIf X→YX \rightarrow Y and WY→ZWY \rightarrow Z , then WX→ZWX \rightarrow ZA→BA \rightarrow B and CB→DCB \rightarrow D → CA→DCA \rightarrow D

14.5 Closure of FD Sets (F⁺)

The closure of a set of FDs FF, denoted F+F^+, is the set of ALL FDs that can be derived from FF using Armstrong's axioms. Example: Given F={A→B,B→C}F = \{A \rightarrow B, B \rightarrow C\}, find F+F^+:
  1. Start with F: A→BA \rightarrow B, B→CB \rightarrow C
  2. By transitivity: A→CA \rightarrow C
  3. By reflexivity: A→AA \rightarrow A, B→BB \rightarrow B, C→CC \rightarrow C, AB→AAB \rightarrow A, etc.
  4. By augmentation: AB→BAB \rightarrow B, AB→BCAB \rightarrow BC, AC→BCAC \rightarrow BC, etc.
  5. By union/decomposition: A→BCA \rightarrow BC (from A→BA \rightarrow B and A→CA \rightarrow C)
  6. Continue until no new FDs can be derived F+F^+ can be very large — up to 2n2^n FDs for n attributes. That's why we use attribute closure instead.

14.6 Attribute Closure (α⁺)

Instead of computing F+F^+ (all FDs), we compute the attribute closure of a set of attributes α\alpha: the set of ALL attributes that can be determined by α\alpha. Algorithm:
pseudo
attribute_closure(α, F):
    result = α
    while result changes:
        for each FD X → Y in F:
            if X ⊆ result:
                result = result ∪ Y
    return result

Worked Example: Attribute Closure

Given R(A,B,C,D,E)R(A, B, C, D, E) and F={A→BC,CD→E,B→D}F = \{A \rightarrow BC, CD \rightarrow E, B \rightarrow D\} Compute A+A^+:
  1. result = {A}
  2. A→BCA \rightarrow BC: A ⊆ result? Yes → result = {A, B, C}
  3. B→DB \rightarrow D: B ⊆ result? Yes → result = {A, B, C, D}
  4. CD→ECD \rightarrow E: CD ⊆ result? Yes → result = {A, B, C, D, E}
  5. No more changes. A+=A,B,C,D,EA^+ = {A, B, C, D, E} (all attributes!) Since A+A^+ contains all attributes, AA is a superkey.

14.7 Using Attribute Closure

Finding Candidate Keys

Method:
  1. Find attributes that never appear on the RHS of any FD — they MUST be in every candidate key
  2. Compute closure of these mandatory attributes
  3. If closure covers all attributes, they form a candidate key
  4. If not, add one attribute at a time and recompute Example: R(A,B,C,D,E)R(A, B, C, D, E) with F={A→B,BC→D,D→E}F = \{A \rightarrow B, BC \rightarrow D, D \rightarrow E\} Step 1: Find mandatory attributes:
  • Attributes not on RHS: A,CA, C (A appears only on LHS; C appears on LHS but never on RHS) Step 2: Compute {A,C}+\{A, C\}^+:
  • result = {A, C}
  • A→BA \rightarrow B: A ⊆ result → result = {A, B, C}
  • BC→DBC \rightarrow D: BC ⊆ result → result = {A, B, C, D}
  • D→ED \rightarrow E: D ⊆ result → result = {A, B, C, D, E}
  • {A,C}+\{A, C\}^+ = ALL attributes! Step 3: {A,C}\{A, C\} is a candidate key (can we remove A? No, C+=CC^+ = {C}. Can we remove C? No, A+=A,B≠RA^+ = {A, B} \neq R.) Result: Candidate key = {A,C}\{A, C\}

Checking if an FD Holds

To check if X→YX \rightarrow Y holds in FF:
  1. Compute X+X^+ using F
  2. If Y⊆X+Y \subseteq X^+, then X→YX \rightarrow Y holds Example: Does AB→EAB \rightarrow E hold in F={A→B,BC→D,D→E}F = \{A \rightarrow B, BC \rightarrow D, D \rightarrow E\}?
  3. Compute {A,B}+\{A, B\}^+:
    • result = {A, B}
    • No FD applies (A→B applies but B is already there)
    • Wait: BC→DBC \rightarrow D needs C, which isn't in result
    • {A,B}+=A,B\{A, B\}^+ = {A, B}
  4. E ∉ {A, B}, so AB→EAB \rightarrow E does NOT hold.

14.8 Equivalence of FD Sets

Two FD sets F1F_1 and F2F_2 are equivalent if F1+=F2+F_1^+ = F_2^+ (they have the same closure). Algorithm to check equivalence:
  1. For each FD X→YX \rightarrow Y in F1F_1, check if it holds in F2F_2 (using attribute closure with F2F_2)
  2. For each FD X→YX \rightarrow Y in F2F_2, check if it holds in F1F_1
  3. If all hold, F1F_1 and F2F_2 are equivalent Example: F1={A→B,B→C}F_1 = \{A \rightarrow B, B \rightarrow C\} F2={A→B,A→C}F_2 = \{A \rightarrow B, A \rightarrow C\} Check A→CA \rightarrow C in F1F_1: A+A^+ using F1F_1 = {A, B, C}. C ⊆ A+A^+ ✓ Check B→CB \rightarrow C in F2F_2: B+B^+ using F2F_2 = {B}. C ∉ B+B^+ ✗ F1F_1 and F2F_2 are NOT equivalent.

14.9 Worked Examples

Example 1: Finding Superkeys from Attribute Closure

Given R(A,B,C,D,G)R(A, B, C, D, G) and F={A→BCD,BC→D,B→D,D→A}F = \{A \rightarrow BCD, BC \rightarrow D, B \rightarrow D, D \rightarrow A\} Prove AGAG is a superkey:
  • Compute {A,G}+\{A, G\}^+:
    • result = {A, G}
    • A→BCDA \rightarrow BCD: A ⊆ result → result = {A, B, C, D, G}
    • All attributes covered! {A,G}+\{A, G\}^+ = R
  • Therefore, AGAG is a superkey.

Example 2: First way to find candidate keys

R(A,B,C,D)R(A, B, C, D) with F={A→BCD,D→A}F = \{A \rightarrow BCD, D \rightarrow A\} Step 1: Attributes not on RHS: C appears on RHS of none → C is mandatory. Actually C doesn't appear on LHS of any FD, and isn't on RHS... Let me check:
  • A→BCD means A determines B,C,D
  • D→A means D determines A
  • C is never on RHS and never on LHS So C must be in every candidate key. Step 2: Compute {C}+\{C\}^+: just {C}. Not all attributes. Step 3: Try {C,A}+\{C, A\}^+:
  • result = {C, A}
  • A→BCDA \rightarrow BCD → result = {A, B, C, D} = R
  • {C,A}\{C, A\} is a superkey. Is it minimal? If we remove C, A+=A,B,C,D=RA^+ = {A, B, C, D} = R, so A alone is sufficient. If we remove A, C+=CC^+ = {C}. Wait, let me re-examine. A is on LHS, not on RHS... Actually A is never on RHS. So both A and C are mandatory? Let me check again: F={A→BCD,D→A}F = \{A \rightarrow BCD, D \rightarrow A\} Attributes on RHS: B, C, D, A (from D→A) Attributes NOT on RHS: (none — A appears on RHS via D→A) Let me compute all candidate keys:
  • Compute D+D^+:
    • result = {D}
    • D→AD \rightarrow A → {D, A}
    • A→BCDA \rightarrow BCD → {A, B, C, D} = R
    • So D is a candidate key.
  • Compute A+A^+:
    • result = {A}
    • A→BCDA \rightarrow BCD → {A, B, C, D} = R
    • So A is a candidate key. Candidate keys: {A} and {D}.

14.10 Tracing Table: Systematic Attribute Closure

The attribute closure algorithm is best understood with a tracing table. Let's work through a complex example. Given: R(A,B,C,D,E,G)R(A, B, C, D, E, G) and F={A→BCD,BC→DE,B→D,D→A}F = \{A \rightarrow BCD, BC \rightarrow DE, B \rightarrow D, D \rightarrow A\} Compute AG+AG^+:
StepResultFD CheckedFD Applies?New Attributes
1{A, G}———
2{A, G}A→BCDA \rightarrow BCDYes (A⊆{A,G})B, C, D
3{A, B, C, D, G}BC→DEBC \rightarrow DEYes (BC⊆result)E
4{A, B, C, D, E, G}B→DB \rightarrow DYes, but D already present—
5{A, B, C, D, E, G}D→AD \rightarrow AYes, but A already present—
6{A,B,C,D,E,G}=R\{A, B, C, D, E, G\} = RDone—All attributes!
Result: AG+=RAG^+ = R, therefore AGAG is a superkey.

14.11 Complete Example: Finding All Candidate Keys

Given: R(A,B,C,D,E,G)R(A, B, C, D, E, G) and F={AB→CD,C→E,E→B,D→G}F = \{AB \rightarrow CD, C \rightarrow E, E \rightarrow B, D \rightarrow G\} Step 1: Identify mandatory attributes — those that never appear on RHS.
AttributeAppears on RHS?Mandatory in CK?
ANo (only in AB→CD LHS)Yes
BYes (E→B)No
CYes (AB→CD)No
DYes (AB→CD)No
EYes (C→E)No
GYes (D→G)No
A MUST be in every candidate key. Step 2: Compute {A}+\{A\}^+:
  • result = {A}
  • AB→CDAB \rightarrow CD: needs B (not in result) ✗
  • C→EC \rightarrow E: needs C ✗
  • E→BE \rightarrow B: needs E ✗
  • D→GD \rightarrow G: needs D ✗
  • {A}+={A}\{A\}^+ = \{A\} — not a superkey. Step 3: Add one attribute at a time. Try {A,B}\{A, B\}:
StepResultFDNew
1{A, B}——
2{A, B}AB→CDAB \rightarrow CDC, D
3{A, B, C, D}C→EC \rightarrow EE
4{A, B, C, D, E}E→BE \rightarrow BB (already)
5{A, B, C, D, E}D→GD \rightarrow GG
6{A, B, C, D, E, G} = RDone✓
{A,B}+=R\{A, B\}^+ = R. CK found: {A,B}\{A, B\}. Try {A,C}\{A, C\}:
StepResultFDNew
1{A, C}——
2{A, C}C→EC \rightarrow EE
3{A, C, E}E→BE \rightarrow BB
4{A, B, C, E}AB→CDAB \rightarrow CDD
5{A, B, C, D, E}D→GD \rightarrow GG
6{A, B, C, D, E, G} = RDone✓
{A,C}+=R\{A, C\}^+ = R. CK found: {A,C}\{A, C\}. Try {A,D}\{A, D\}:
StepResultFDNew
1{A, D}——
2{A, D}D→GD \rightarrow GG
3{A, D, G}No other FD applies—
{A,D}+={A,D,G}≠R\{A, D\}^+ = \{A, D, G\} \neq R. Not a superkey. Try {A,E}\{A, E\}:
StepResultFDNew
1{A, E}——
2{A, E}E→BE \rightarrow BB
3{A, B, E}AB→CDAB \rightarrow CDC, D
4{A, B, C, D, E}D→GD \rightarrow GG
5{A, B, C, D, E, G} = RDone✓
{A,E}+=R\{A, E\}^+ = R. CK found: {A,E}\{A, E\}. Try {A,G}\{A, G\}:
StepResultFDNew
1{A, G}——
2{A, G}No FD has A or G alone on LHS—
{A,G}+={A,G}≠R\{A, G\}^+ = \{A, G\} \neq R. Not a superkey. Candidate keys: {A,B}\{A, B\}, {A,C}\{A, C\}, {A,E}\{A, E\} Superkey count: With 6 attributes, three CKs of size 2, all sharing A: =26−2+26−2+26−2−26−3−26−3−26−3+26−4= 2^{6-2} + 2^{6-2} + 2^{6-2} - 2^{6-3} - 2^{6-3} - 2^{6-3} + 2^{6-4} =16+16+16−8−8−8+4=28= 16 + 16 + 16 - 8 - 8 - 8 + 4 = 28 superkeys

14.12 Practice: Attribute Closure Walkthrough

Problem: Given F={A→B,AC→D,B→C,D→E}F = \{A \rightarrow B, AC \rightarrow D, B \rightarrow C, D \rightarrow E\}, compute {A}+\{A\}^+, {B}+\{B\}^+, {C}+\{C\}^+, and {A,C}+\{A, C\}^+. {A}+\{A\}^+ :
  1. result = {A}
  2. A→BA \rightarrow B → {A, B}
  3. B→CB \rightarrow C → {A, B, C}
  4. AC→DAC \rightarrow D → {A, B, C, D}
  5. D→ED \rightarrow E → {A, B, C, D, E}
  6. Done. {A}+={A,B,C,D,E}\{A\}^+ = \{A, B, C, D, E\} {B}+\{B\}^+ :
  7. result = {B}
  8. B→CB \rightarrow C → {B, C}
  9. No other FD applies (A not in result)
  10. {B}+={B,C}\{B\}^+ = \{B, C\} {C}+\{C\}^+ :
  11. result = {C}
  12. No FD has C alone on LHS (AC→D needs A)
  13. {C}+={C}\{C\}^+ = \{C\} {A,C}+\{A, C\}^+ :
  14. result = {A, C}
  15. A→BA \rightarrow B → {A, B, C}
  16. B→CB \rightarrow C → {A, B, C} (C already there)
  17. AC→DAC \rightarrow D → {A, B, C, D}
  18. D→ED \rightarrow E → {A, B, C, D, E}
  19. Done. {A,C}+={A,B,C,D,E}\{A, C\}^+ = \{A, B, C, D, E\} Check: Is {A,C}\{A, C\} a candidate key? {A,C}+=R\{A, C\}^+ = R. Is it minimal?
  • Remove A: {C}+={C}≠R\{C\}^+ = \{C\} \neq R ✓ A needed
  • Remove C: {A}+={A,B,C,D,E}=R\{A\}^+ = \{A, B, C, D, E\} = R! So A alone is a candidate key.
  • Final CK: {A}\{A\} only.
ConceptDescriptionFormula
Functional DependencyX determines YX→YX \rightarrow Y
Trivial FDY is subset of XY⊆XY \subseteq X
Attribute ClosureAll attributes determined by αα+\alpha^+
Closure of FAll FDs derivable from FF+F^+
Candidate KeyMinimal superkeyAny α where α+=R\alpha^+ = R and no proper subset has closure = R

⚠️ Common Pitfalls

Pitfall 1: Thinking FDs are "One-to-One Mappings"

The Mistake: "X → Y means X maps to a unique Y like a function." Why It's Wrong: That's actually correct for our purposes, but students confuse it with "every X maps to exactly one Y" (which is always true in relations — each attribute has one value per tuple). The FD says: same X → same Y across different tuples. Correct: Think of it as: "If two tuples have the same X, they MUST have the same Y."

Pitfall 2: Confusing Attribute Closure with FD Closure

The Mistake: Using F⁺ and α⁺ interchangeably. Why It's Wrong:
  • F+F^+ = all FDs derivable from F (a set of FDs)
  • α+\alpha^+ = all attributes derivable from α (a set of attributes) Memory Aid: F+F^+ has a superscript + on the FD set; α+\alpha^+ has it on the attribute set.

Pitfall 3: Assuming Transitivity Works Backwards

The Mistake: "A → B and A → C implies B → C." Why It's Wrong: Transitivity requires a chain: X → Y and Y → Z gives X → Z. A → B and A → C mean both B and C are determined by A, but there's no relationship between B and C. Correct: From A → B and A → C, we can use UNION to get A → BC, then DECOMPOSITION to get back to A → B and A → C. But we cannot derive B → C.

📝 Practice Questions

Q1. What is a functional dependency? Give a real-world example.

Answer
A functional dependency X→YX \rightarrow Y means: if two tuples have the same value for attribute(s) X, they must have the same value for attribute(s) Y.
Real-world example: In a student table, studentid→studentnamestudent_id \rightarrow student_name — any two rows with the same student_id must have the same student_name.

Q2. List Armstrong's three basic axioms.

Answer
  1. Reflexivity: If Y⊆XY \subseteq X, then X→YX \rightarrow Y
  2. Augmentation: If X→YX \rightarrow Y, then XZ→YZXZ \rightarrow YZ
  3. Transitivity: If X→YX \rightarrow Y and Y→ZY \rightarrow Z, then X→ZX \rightarrow Z
These are sound and complete — every implied FD can be derived using these rules.

Q3. Given F={A→B,B→C}F = \{A \rightarrow B, B \rightarrow C\}, compute {A}+\{A\}^+.

Answer
  1. result = {A}
  2. A→BA \rightarrow B: A ⊆ result → result = {A, B}
  3. B→CB \rightarrow C: B ⊆ result → result = {A, B, C}
  4. No more changes. {A}+={A,B,C}\{A\}^+ = \{A, B, C\}

Q4. Given R(A,B,C,D)R(A, B, C, D) and F={A→B,A→C,B→D}F = \{A \rightarrow B, A \rightarrow C, B \rightarrow D\}, find all candidate keys.

Answer
Step 1: Attributes not on RHS of any FD: A (appears only on LHS of FDs). So A is mandatory.
Step 2: Compute {A}+\{A\}^+:
  • result = {A}
  • A→BA \rightarrow B → {A, B}
  • A→CA \rightarrow C → {A, B, C}
  • B→DB \rightarrow D → {A, B, C, D} = R
Step 3: {A}+\{A\}^+ covers all attributes. Since A is a single attribute, it's minimal.
Candidate key: {A}

Q5. Check if F1={A→B,A→C}F_1 = \{A \rightarrow B, A \rightarrow C\} and F2={A→BC}F_2 = \{A \rightarrow BC\} are equivalent.

Answer
Check each FD from F1F_1 against F2F_2:
  • A→BA \rightarrow B: Using F2F_2, compute A+A^+ = {A, B, C}. B ⊆ A+A^+ ✓
  • A→CA \rightarrow C: Using F2F_2, compute A+A^+ = {A, B, C}. C ⊆ A+A^+ ✓
Check each FD from F2F_2 against F1F_1:
  • A→BCA \rightarrow BC: Using F1F_1, compute A+A^+ = {A, B, C}. BC ⊆ A+A^+ ✓
All FDs hold in the other set. Therefore, F1F_1 and F2F_2 are equivalent.

Q6. Does AB→CAB \rightarrow C hold in F={A→B,B→C}F = \{A \rightarrow B, B \rightarrow C\}?

Answer
Compute {A,B}+\{A, B\}^+:
  • result = {A, B}
  • A→BA \rightarrow B: A ⊆ result (B already there)
  • B→CB \rightarrow C: B ⊆ result → result = {A, B, C}
  • {A,B}+={A,B,C}\{A, B\}^+ = \{A, B, C\}
C ⊆ {A,B}+\{A, B\}^+? Yes! So AB→CAB \rightarrow C holds in F.
(Note: This is expected because A→BA \rightarrow B and B→CB \rightarrow C gives A→CA \rightarrow C by transitivity, so with A alone we can determine C.)

Q7. What is a trivial FD? Give three examples.

Answer
A trivial FD X→YX \rightarrow Y is one where Y⊆XY \subseteq X — the RHS is a subset of the LHS. Trivial FDs are always true.
Examples:
  1. A→AA \rightarrow A
  2. AB→AAB \rightarrow A
  3. ABC→ABABC \rightarrow AB
Non-trivial example: ID→nameID \rightarrow name (name is not a subset of ID)

Q8. Given R(A,B,C,D,E)R(A, B, C, D, E) with F={A→B,BC→D,D→E}F = \{A \rightarrow B, BC \rightarrow D, D \rightarrow E\}, find all candidate keys.

Answer
Step 1: Attributes not on RHS: A, C. (A appears on LHS only; C appears on LHS only; B appears on RHS of A→B; D appears on RHS of BC→D; E appears on RHS of D→E)
So A and C must be in every candidate key.
Step 2: Compute {A,C}+\{A, C\}^+:
  • result = {A, C}
  • A→BA \rightarrow B → {A, B, C}
  • BC→DBC \rightarrow D → {A, B, C, D}
  • D→ED \rightarrow E → {A, B, C, D, E} = R
Step 3: {A,C}+\{A, C\}^+ = R. Is it minimal?
  • Remove A: {C}+={C}≠R\{C\}^+ = \{C\} \neq R
  • Remove C: {A}+={A,B}≠R\{A\}^+ = \{A, B\} \neq R (no BC → D without C)
Candidate key: {A, C}

🔗 Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.