14 - Functional Dependencies
3402 words
17 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# 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 equivalenc...

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
- 03 - Relational Model โ Relations, attributes, keys
- Set theory basics (subsets, supersets)
๐ 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โ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 R, a functional dependency XโY holds iff:
For any two tuples t1โ and t2โ in R:
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โname means: if two rows have the same ID, they MUST have the same name.14.3 Trivial vs. Non-Trivial FDs
| Type | Definition | Example | Always True? |
|---|---|---|---|
| Trivial | XโY where YโX | AโA , ABโA | Yes |
| Non-trivial | YโX | IDโname | No (depends on data) |
| Completely non-trivial | XโฉY=โ | IDโname | No |
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).
| Rule | Name | Formal Definition | Example |
|---|---|---|---|
| 1 | Reflexivity | If YโX , then XโY | ABโA |
| 2 | Augmentation | If XโY , then XZโYZ | If AโB , then ACโBC |
| 3 | Transitivity | If XโY and YโZ , then XโZ | If AโB and BโC , then AโC |
Derived Rules
| Rule | Derivation | Example |
|---|---|---|
| Union | If XโY and XโZ , then XโYZ | AโB and AโC โ AโBC |
| Decomposition | If XโYZ , then XโY and XโZ | AโBC โ AโB and AโC |
| Pseudotransitivity | If XโY and WYโZ , then WXโZ | AโB and CBโD โ CAโD |
14.5 Closure of FD Sets (Fโบ)
The closure of a set of FDs F, denoted F+, is the set of ALL FDs that can be derived from F using Armstrong's axioms.
Example: Given F={AโB,BโC}, find F+:
- Start with F: AโB, BโC
- By transitivity: AโC
- By reflexivity: AโA, BโB, CโC, ABโA, etc.
- By augmentation: ABโB, ABโBC, ACโBC, etc.
- By union/decomposition: AโBC (from AโB and AโC)
- Continue until no new FDs can be derived F+ can be very large โ up to 2n FDs for n attributes. That's why we use attribute closure instead.
14.6 Attribute Closure (ฮฑโบ)
Instead of computing F+ (all FDs), we compute the attribute closure of a set of attributes ฮฑ: the set of ALL attributes that can be determined by ฮฑ.
Algorithm:
pseudoattribute_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) and F={AโBC,CDโE,BโD}
Compute A+:
- result = {A}
- AโBC: A โ result? Yes โ result = {A, B, C}
- BโD: B โ result? Yes โ result = {A, B, C, D}
- CDโE: CD โ result? Yes โ result = {A, B, C, D, E}
- No more changes. A+=A,B,C,D,E (all attributes!) Since A+ contains all attributes, A is a superkey.
14.7 Using Attribute Closure
Finding Candidate Keys
Method:
- Find attributes that never appear on the RHS of any FD โ they MUST be in every candidate key
- Compute closure of these mandatory attributes
- If closure covers all attributes, they form a candidate key
- If not, add one attribute at a time and recompute Example: R(A,B,C,D,E) with F={AโB,BCโD,DโE} Step 1: Find mandatory attributes:
- Attributes not on RHS: A,C (A appears only on LHS; C appears on LHS but never on RHS) Step 2: Compute {A,C}+:
- result = {A, C}
- AโB: A โ result โ result = {A, B, C}
- BCโD: BC โ result โ result = {A, B, C, D}
- DโE: D โ result โ result = {A, B, C, D, E}
- {A,C}+ = ALL attributes! Step 3: {A,C} is a candidate key (can we remove A? No, C+=C. Can we remove C? No, A+=A,B๎ =R.) Result: Candidate key = {A,C}
Checking if an FD Holds
To check if XโY holds in F:
- Compute X+ using F
- If YโX+, then XโY holds Example: Does ABโE hold in F={AโB,BCโD,DโE}?
- Compute {A,B}+:
- result = {A, B}
- No FD applies (AโB applies but B is already there)
- Wait: BCโD needs C, which isn't in result
- {A,B}+=A,B
- E โ {A, B}, so ABโE does NOT hold.
14.8 Equivalence of FD Sets
Two FD sets F1โ and F2โ are equivalent if F1+โ=F2+โ (they have the same closure).
Algorithm to check equivalence:
- For each FD XโY in F1โ, check if it holds in F2โ (using attribute closure with F2โ)
- For each FD XโY in F2โ, check if it holds in F1โ
- If all hold, F1โ and F2โ are equivalent Example: F1โ={AโB,BโC} F2โ={AโB,AโC} Check AโC in F1โ: A+ using F1โ = {A, B, C}. C โ A+ โ Check BโC in F2โ: B+ using F2โ = {B}. C โ B+ โ F1โ and F2โ are NOT equivalent.
14.9 Worked Examples
Example 1: Finding Superkeys from Attribute Closure
Given R(A,B,C,D,G) and F={AโBCD,BCโD,BโD,DโA}
Prove AG is a superkey:
- Compute {A,G}+:
- result = {A, G}
- AโBCD: A โ result โ result = {A, B, C, D, G}
- All attributes covered! {A,G}+ = R
- Therefore, AG is a superkey.
Example 2: First way to find candidate keys
R(A,B,C,D) with F={AโBCD,Dโ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}+: just {C}. Not all attributes. Step 3: Try {C,A}+:
- result = {C, A}
- AโBCD โ result = {A, B, C, D} = R
- {C,A} is a superkey. Is it minimal? If we remove C, A+=A,B,C,D=R, so A alone is sufficient. If we remove A, C+=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} 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+:
- result = {D}
- DโA โ {D, A}
- AโBCD โ {A, B, C, D} = R
- So D is a candidate key.
- Compute A+:
- result = {A}
- Aโ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) and F={AโBCD,BCโDE,BโD,DโA}
Compute AG+:
| Step | Result | FD Checked | FD Applies? | New Attributes |
|---|---|---|---|---|
| 1 | {A, G} | โ | โ | โ |
| 2 | {A, G} | AโBCD | Yes (Aโ{A,G}) | B, C, D |
| 3 | {A, B, C, D, G} | BCโDE | Yes (BCโresult) | E |
| 4 | {A, B, C, D, E, G} | BโD | Yes, but D already present | โ |
| 5 | {A, B, C, D, E, G} | DโA | Yes, but A already present | โ |
| 6 | {A,B,C,D,E,G}=R | Done | โ | All attributes! |
Result: AG+=R, therefore AG is a superkey.
14.11 Complete Example: Finding All Candidate Keys
Given: R(A,B,C,D,E,G) and F={ABโCD,CโE,EโB,DโG}
Step 1: Identify mandatory attributes โ those that never appear on RHS.
| Attribute | Appears on RHS? | Mandatory in CK? |
|---|---|---|
| A | No (only in ABโCD LHS) | Yes |
| B | Yes (EโB) | No |
| C | Yes (ABโCD) | No |
| D | Yes (ABโCD) | No |
| E | Yes (CโE) | No |
| G | Yes (DโG) | No |
A MUST be in every candidate key.
Step 2: Compute {A}+:
- result = {A}
- ABโCD: needs B (not in result) โ
- CโE: needs C โ
- EโB: needs E โ
- DโG: needs D โ
- {A}+={A} โ not a superkey. Step 3: Add one attribute at a time. Try {A,B}:
| Step | Result | FD | New |
|---|---|---|---|
| 1 | {A, B} | โ | โ |
| 2 | {A, B} | ABโCD | C, D |
| 3 | {A, B, C, D} | CโE | E |
| 4 | {A, B, C, D, E} | EโB | B (already) |
| 5 | {A, B, C, D, E} | DโG | G |
| 6 | {A, B, C, D, E, G} = R | Done | โ |
{A,B}+=R. CK found: {A,B}.
Try {A,C}:
| Step | Result | FD | New |
|---|---|---|---|
| 1 | {A, C} | โ | โ |
| 2 | {A, C} | CโE | E |
| 3 | {A, C, E} | EโB | B |
| 4 | {A, B, C, E} | ABโCD | D |
| 5 | {A, B, C, D, E} | DโG | G |
| 6 | {A, B, C, D, E, G} = R | Done | โ |
{A,C}+=R. CK found: {A,C}.
Try {A,D}:
| Step | Result | FD | New |
|---|---|---|---|
| 1 | {A, D} | โ | โ |
| 2 | {A, D} | DโG | G |
| 3 | {A, D, G} | No other FD applies | โ |
{A,D}+={A,D,G}๎ =R. Not a superkey.
Try {A,E}:
| Step | Result | FD | New |
|---|---|---|---|
| 1 | {A, E} | โ | โ |
| 2 | {A, E} | EโB | B |
| 3 | {A, B, E} | ABโCD | C, D |
| 4 | {A, B, C, D, E} | DโG | G |
| 5 | {A, B, C, D, E, G} = R | Done | โ |
{A,E}+=R. CK found: {A,E}.
Try {A,G}:
| Step | Result | FD | New |
|---|---|---|---|
| 1 | {A, G} | โ | โ |
| 2 | {A, G} | No FD has A or G alone on LHS | โ |
{A,G}+={A,G}๎ =R. Not a superkey.
Candidate keys: {A,B}, {A,C}, {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 =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}, compute {A}+, {B}+, {C}+, and {A,C}+.
{A}+ :
- result = {A}
- AโB โ {A, B}
- BโC โ {A, B, C}
- ACโD โ {A, B, C, D}
- DโE โ {A, B, C, D, E}
- Done. {A}+={A,B,C,D,E} {B}+ :
- result = {B}
- BโC โ {B, C}
- No other FD applies (A not in result)
- {B}+={B,C} {C}+ :
- result = {C}
- No FD has C alone on LHS (ACโD needs A)
- {C}+={C} {A,C}+ :
- result = {A, C}
- AโB โ {A, B, C}
- BโC โ {A, B, C} (C already there)
- ACโD โ {A, B, C, D}
- DโE โ {A, B, C, D, E}
- Done. {A,C}+={A,B,C,D,E} Check: Is {A,C} a candidate key? {A,C}+=R. Is it minimal?
- Remove A: {C}+={C}๎ =R โ A needed
- Remove C: {A}+={A,B,C,D,E}=R! So A alone is a candidate key.
- Final CK: {A} only.
| Concept | Description | Formula |
|---|---|---|
| Functional Dependency | X determines Y | XโY |
| Trivial FD | Y is subset of X | YโX |
| Attribute Closure | All attributes determined by ฮฑ | ฮฑ+ |
| Closure of F | All FDs derivable from F | F+ |
| Candidate Key | Minimal superkey | Any ฮฑ where ฮฑ+=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+ = all FDs derivable from F (a set of FDs)
- ฮฑ+ = all attributes derivable from ฮฑ (a set of attributes) Memory Aid: F+ has a superscript + on the FD set; ฮฑ+ 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.
AnswerA functional dependency Xโ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 astudenttable, studentiโdโstudentnโame โ any two rows with the same student_id must have the same student_name.
Q2. List Armstrong's three basic axioms.
Answer
- Reflexivity: If YโX, then XโY
- Augmentation: If XโY, then XZโYZ
- Transitivity: If XโY and YโZ, then XโZ
These are sound and complete โ every implied FD can be derived using these rules.
Q3. Given F={AโB,BโC}, compute {A}+.
Answer
- result = {A}
- AโB: A โ result โ result = {A, B}
- BโC: B โ result โ result = {A, B, C}
- No more changes. {A}+={A,B,C}
Q4. Given R(A,B,C,D) and F={AโB,AโC,BโD}, find all candidate keys.
AnswerStep 1: Attributes not on RHS of any FD: A (appears only on LHS of FDs). So A is mandatory.Step 2: Compute {A}+:
- result = {A}
- AโB โ {A, B}
- AโC โ {A, B, C}
- BโD โ {A, B, C, D} = R
Step 3: {A}+ covers all attributes. Since A is a single attribute, it's minimal.Candidate key: {A}
Q5. Check if F1โ={AโB,AโC} and F2โ={AโBC} are equivalent.
AnswerCheck each FD from F1โ against F2โ:
- AโB: Using F2โ, compute A+ = {A, B, C}. B โ A+ โ
- AโC: Using F2โ, compute A+ = {A, B, C}. C โ A+ โ
Check each FD from F2โ against F1โ:
- AโBC: Using F1โ, compute A+ = {A, B, C}. BC โ A+ โ
All FDs hold in the other set. Therefore, F1โ and F2โ are equivalent.
Q6. Does ABโC hold in F={AโB,BโC}?
AnswerCompute {A,B}+:
- result = {A, B}
- AโB: A โ result (B already there)
- BโC: B โ result โ result = {A, B, C}
- {A,B}+={A,B,C}
C โ {A,B}+? Yes! So ABโC holds in F.(Note: This is expected because AโB and BโC gives AโC by transitivity, so with A alone we can determine C.)
Q7. What is a trivial FD? Give three examples.
AnswerA trivial FD XโY is one where YโX โ the RHS is a subset of the LHS. Trivial FDs are always true.Examples:
- AโA
- ABโA
- ABCโAB
Non-trivial example: IDโname (name is not a subset of ID)
Q8. Given R(A,B,C,D,E) with F={AโB,BCโD,DโE}, find all candidate keys.
AnswerStep 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}+:
- result = {A, C}
- AโB โ {A, B, C}
- BCโD โ {A, B, C, D}
- DโE โ {A, B, C, D, E} = R
Step 3: {A,C}+ = R. Is it minimal?
- Remove A: {C}+={C}๎ =R
- Remove C: {A}+={A,B}๎ =R (no BC โ D without C)
Candidate key: {A, C}
๐ Cross-References
- Next Topic: 15 - Canonical Cover
- Previous Topic: 13 - ER-to-Relational Mapping
- Related: BSMA1001 (Maths 1) โ Set theory, relations
- Related: 16 - Normalization (using FDs to design good schemas)
- Textbook: Silberschatz, Korth, Sudarshan โ Chapter 8 (Relational Database Design) Join Discord Previous13 - ER-to-Relational MappingNext15 - Canonical Cover