Quiz 2
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: XYX \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 XYX \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 IDnameID \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?
TrivialXYX \rightarrow Y where YXY \subseteq XAAA \rightarrow A , ABAAB \rightarrow AYes
Non-trivialYXY \nsubseteq XIDnameID \rightarrow nameNo (depends on data)
Completely non-trivialXY=X \cap Y = \emptysetIDnameID \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 YXY \subseteq X , then XYX \rightarrow YABAAB \rightarrow A
2AugmentationIf XYX \rightarrow Y , then XZYZXZ \rightarrow YZIf ABA \rightarrow B , then ACBCAC \rightarrow BC
3TransitivityIf XYX \rightarrow Y and YZY \rightarrow Z , then XZX \rightarrow ZIf ABA \rightarrow B and BCB \rightarrow C , then ACA \rightarrow C

Derived Rules

RuleDerivationExample
UnionIf XYX \rightarrow Y and XZX \rightarrow Z , then XYZX \rightarrow YZABA \rightarrow B and ACA \rightarrow CABCA \rightarrow BC
DecompositionIf XYZX \rightarrow YZ , then XYX \rightarrow Y and XZX \rightarrow ZABCA \rightarrow BCABA \rightarrow B and ACA \rightarrow C
PseudotransitivityIf XYX \rightarrow Y and WYZWY \rightarrow Z , then WXZWX \rightarrow ZABA \rightarrow B and CBDCB \rightarrow DCADCA \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={AB,BC}F = \{A \rightarrow B, B \rightarrow C\}, find F+F^+:
  1. Start with F: ABA \rightarrow B, BCB \rightarrow C
  2. By transitivity: ACA \rightarrow C
  3. By reflexivity: AAA \rightarrow A, BBB \rightarrow B, CCC \rightarrow C, ABAAB \rightarrow A, etc.
  4. By augmentation: ABBAB \rightarrow B, ABBCAB \rightarrow BC, ACBCAC \rightarrow BC, etc.
  5. By union/decomposition: ABCA \rightarrow BC (from ABA \rightarrow B and ACA \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={ABC,CDE,BD}F = \{A \rightarrow BC, CD \rightarrow E, B \rightarrow D\} Compute A+A^+:
  1. result = {A}
  2. ABCA \rightarrow BC: A ⊆ result? Yes → result = {A, B, C}
  3. BDB \rightarrow D: B ⊆ result? Yes → result = {A, B, C, D}
  4. CDECD \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={AB,BCD,DE}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}
  • ABA \rightarrow B: A ⊆ result → result = {A, B, C}
  • BCDBC \rightarrow D: BC ⊆ result → result = {A, B, C, D}
  • DED \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,BRA^+ = {A, B} \neq R.) Result: Candidate key = {A,C}\{A, C\}

Checking if an FD Holds

To check if XYX \rightarrow Y holds in FF:
  1. Compute X+X^+ using F
  2. If YX+Y \subseteq X^+, then XYX \rightarrow Y holds Example: Does ABEAB \rightarrow E hold in F={AB,BCD,DE}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: BCDBC \rightarrow D needs C, which isn't in result
    • {A,B}+=A,B\{A, B\}^+ = {A, B}
  4. E ∉ {A, B}, so ABEAB \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 XYX \rightarrow Y in F1F_1, check if it holds in F2F_2 (using attribute closure with F2F_2)
  2. For each FD XYX \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={AB,BC}F_1 = \{A \rightarrow B, B \rightarrow C\} F2={AB,AC}F_2 = \{A \rightarrow B, A \rightarrow C\} Check ACA \rightarrow C in F1F_1: A+A^+ using F1F_1 = {A, B, C}. C ⊆ A+A^+ ✓ Check BCB \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={ABCD,BCD,BD,DA}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}
    • ABCDA \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={ABCD,DA}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}
  • ABCDA \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={ABCD,DA}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}
    • DAD \rightarrow A → {D, A}
    • ABCDA \rightarrow BCD → {A, B, C, D} = R
    • So D is a candidate key.
  • Compute A+A^+:
    • result = {A}
    • ABCDA \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={ABCD,BCDE,BD,DA}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}ABCDA \rightarrow BCDYes (A⊆{A,G})B, C, D
3{A, B, C, D, G}BCDEBC \rightarrow DEYes (BC⊆result)E
4{A, B, C, D, E, G}BDB \rightarrow DYes, but D already present
5{A, B, C, D, E, G}DAD \rightarrow AYes, but A already present
6{A,B,C,D,E,G}=R\{A, B, C, D, E, G\} = RDoneAll 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={ABCD,CE,EB,DG}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}
  • ABCDAB \rightarrow CD: needs B (not in result) ✗
  • CEC \rightarrow E: needs C ✗
  • EBE \rightarrow B: needs E ✗
  • DGD \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}ABCDAB \rightarrow CDC, D
3{A, B, C, D}CEC \rightarrow EE
4{A, B, C, D, E}EBE \rightarrow BB (already)
5{A, B, C, D, E}DGD \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}CEC \rightarrow EE
3{A, C, E}EBE \rightarrow BB
4{A, B, C, E}ABCDAB \rightarrow CDD
5{A, B, C, D, E}DGD \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}DGD \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}EBE \rightarrow BB
3{A, B, E}ABCDAB \rightarrow CDC, D
4{A, B, C, D, E}DGD \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: =262+262+262263263263+264= 2^{6-2} + 2^{6-2} + 2^{6-2} - 2^{6-3} - 2^{6-3} - 2^{6-3} + 2^{6-4} =16+16+16888+4=28= 16 + 16 + 16 - 8 - 8 - 8 + 4 = 28 superkeys

14.12 Practice: Attribute Closure Walkthrough

Problem: Given F={AB,ACD,BC,DE}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. ABA \rightarrow B → {A, B}
  3. BCB \rightarrow C → {A, B, C}
  4. ACDAC \rightarrow D → {A, B, C, D}
  5. DED \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. BCB \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. ABA \rightarrow B → {A, B, C}
  16. BCB \rightarrow C → {A, B, C} (C already there)
  17. ACDAC \rightarrow D → {A, B, C, D}
  18. DED \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 YXYX \rightarrow Y
Trivial FDY is subset of XYXY \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 XYX \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, studentidstudentnamestudent_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 YXY \subseteq X, then XYX \rightarrow Y
  2. Augmentation: If XYX \rightarrow Y, then XZYZXZ \rightarrow YZ
  3. Transitivity: If XYX \rightarrow Y and YZY \rightarrow Z, then XZX \rightarrow Z
These are sound and complete — every implied FD can be derived using these rules.

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

Answer
  1. result = {A}
  2. ABA \rightarrow B: A ⊆ result → result = {A, B}
  3. BCB \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={AB,AC,BD}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}
  • ABA \rightarrow B → {A, B}
  • ACA \rightarrow C → {A, B, C}
  • BDB \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={AB,AC}F_1 = \{A \rightarrow B, A \rightarrow C\} and F2={ABC}F_2 = \{A \rightarrow BC\} are equivalent.

Answer
Check each FD from F1F_1 against F2F_2:
  • ABA \rightarrow B: Using F2F_2, compute A+A^+ = {A, B, C}. B ⊆ A+A^+
  • ACA \rightarrow C: Using F2F_2, compute A+A^+ = {A, B, C}. C ⊆ A+A^+
Check each FD from F2F_2 against F1F_1:
  • ABCA \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 ABCAB \rightarrow C hold in F={AB,BC}F = \{A \rightarrow B, B \rightarrow C\}?

Answer
Compute {A,B}+\{A, B\}^+:
  • result = {A, B}
  • ABA \rightarrow B: A ⊆ result (B already there)
  • BCB \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 ABCAB \rightarrow C holds in F.
(Note: This is expected because ABA \rightarrow B and BCB \rightarrow C gives ACA \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 XYX \rightarrow Y is one where YXY \subseteq X — the RHS is a subset of the LHS. Trivial FDs are always true.
Examples:
  1. AAA \rightarrow A
  2. ABAAB \rightarrow A
  3. ABCABABC \rightarrow AB
Non-trivial example: IDnameID \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={AB,BCD,DE}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}
  • ABA \rightarrow B → {A, B, C}
  • BCDBC \rightarrow D → {A, B, C, D}
  • DED \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
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.