Functional Dependency
Functional Dependency
Level 6 — Schema Design & Normalization The mathematical relationship between columns in a table where the value of one column (or set of columns) uniquely determines the value of another column, written as .
1. Prerequisites
- Normalization — The schema cleanup process that uses dependencies to split tables.
2. Term Category
Core Concept (Attribute Determination Relationship): Functional Dependency () describes a relationship where the value of column set uniquely determines the value of column set .
3. Explanation
Environment Context
- Universal Standard (A fundamental mathematical concept in relational algebra used to define database normalization rules).
(1) Design Motivation — "Why did we design this?"
Before you can apply the rules of database normalization (1NF, 2NF, 3NF), you need a precise mathematical language to describe how columns relate to each other.
If you inspect a student database, you observe that:
- If you know a student's ID (
student_id), you can find their exact email address. There is only one email address per student ID. - If you know a student's name (
student_name), you cannot be certain of their email, because two different students named "John Smith" will have different emails.
We designed the concept of Functional Dependency to describe these rules.
We write: (Read: "X functionally determines Y", or "Y is functionally dependent on X").
This means that if two rows in a table have the same value for column , they must have the exact same value for column .
By identifying these mathematical dependencies in your data, you can logically determine where table schemas are broken, and how to split them up to prevent anomalies.
(2) Keys and Functional Dependencies
By definition, the Primary Key of a table functionally determines every other column in that table. If id is the primary key of a users table:
(3) Types of Dependencies (Prerequisites for Normalization)
1. Full Functional Dependency
An attribute is fully dependent on a composite set of attributes if it depends on the entirety of , and cannot be determined by any smaller subset of . (Critical for 2NF).
2. Transitive Dependency
A transitive dependency occurs when column determines column , and column determines column . Indirectly, column determines column through : (Critical for 3NF).
(4) Reality Metaphor
Imagine a university student portal:
- Locker ID Combo Code: If you look up Locker
45(X), the database shows a single, unique lock combination code (Y). Locker45will never have two different combination codes. - Locker ID does not functionally determine Student Name: Student Alice might rent locker
45in the morning and locker80in the afternoon. Knowing Locker45does not determine a single student name because different students rent it over the semesters.
(5) Dependency Mapping Examples
Assume we have an unnormalized table:
enrollments (student_id, course_id, student_name, grade)
The functional dependencies are:
student_idstudent_name- (Knowing the ID uniquely determines the name. This is a partial dependency because it only depends on part of the primary key).
(student_id, course_id)grade- (A grade belongs to a specific student in a specific course. You need both columns to determine the grade. This is a full dependency).
4. Common Mistakes & Pitfalls
Mistake 1: Confusing functional dependencies with database index settings
The mistake: Thinking you have to run a SQL command to "activate" a functional dependency.
Why it's wrong: Functional dependencies are real-world business facts, not database configurations. They represent how your business data operates. The database engine does not know that zip_code determines city_name until you normalize the schema and write constraints to enforce it.
Fix: Analyze your business rules, write down your functional dependencies on paper, and use them to design tables that match normal forms.
Mistake 2: Assuming Non-Key Columns Depend on the Whole Primary Key in Composite Keys
The mistake: Including store_location in table (store_id, product_id, store_location).
Why it's wrong: store_location depends ONLY on store_id, NOT on product_id! This partial functional dependency violates Second Normal Form (2NF).
Incorrect:
// Including store_location in (store_id, product_id) table
Fix:
Move store_location to separate stores table: stores (store_id, store_location)
Mistake 3: Ignoring Transitive Functional Dependencies ($A
ightarrow B ightarrow C$)
The mistake: Storing zip_code and city_name in users table when zip_code functionally determines city_name.
Why it's wrong: Transitive dependencies () violate 3NF, creating update anomalies when city names change. Move zip lookup to a zip_codes table.
Incorrect:
// Storing zip_code and city_name in users table
Fix:
Move zip mapping to zip_codes table: zip_codes (zip_code, city_name)
5. Practice Exercises
Exercise 1: Identifying Functional Dependencies in Schema Data
Scenario:
Analyze functional dependencies () in a course_registrations table: student_id -> student_name, course_id -> course_title.
Requirements:
- Identify primary key determinants and partial dependencies.
Answer
Implementation
Functional Dependency Analysis:
- Composite Primary Key: (student_id, course_id)
- Dependency 1: (student_id, course_id) -> grade (Full Dependency)
- Dependency 2: student_id -> student_name (Partial Dependency: depends ONLY on part of PK!)
- Dependency 3: course_id -> course_title (Partial Dependency: depends ONLY on part of PK!)
Technical Explanation
- A Functional Dependency means given value , value is uniquely determined.
- Partial dependencies (where non-key columns depend on part of a composite primary key) violate 2NF.
- Theoretical foundation for database normalization algorithms.
Exercise 2: Resolving Partial Dependencies to Achieve 2NF
Scenario:
Decompose course_registrations to eliminate partial dependencies and achieve Second Normal Form (2NF).
Requirements:
- Create
students,courses, andenrollmentstables.
Answer
Implementation
CREATE TABLE students (
id INTEGER PRIMARY KEY,
student_name TEXT NOT NULL
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
course_title TEXT NOT NULL
);
CREATE TABLE enrollments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
grade TEXT,
PRIMARY KEY (student_id, course_id)
);
Technical Explanation
- Decomposing tables separates independent functional dependencies into dedicated tables.
- Guarantees all non-key attributes in
enrollments(grade) depend on the FULL composite primary key. - Achieves 2NF compliance.
Exercise 3: Identifying Transitive Dependencies for 3NF
Scenario:
Identify transitive dependency zip_code -> city in table addresses(id, street, zip_code, city).
Requirements:
- Explain transitive dependency .
Answer
Implementation
Transitive Dependency Analysis:
- Primary Key: id
- Dependency 1: id -> zip_code
- Dependency 2: zip_code -> city
- Transitive Link: id -> city via zip_code (Violates 3NF!)
Solution: Move (zip_code, city) into a separate 'zip_codes' lookup table.
Technical Explanation
- Transitive dependencies occur when a non-key column depends on another non-key column.
- Violates 3NF and causes update anomalies if a city name changes.
- Core concept of 3NF schema decomposition.
6. Related Terms
- Normalization — The parent organization process.
- First Normal Form (1NF) — The atomic standard.
7. Key Takeaways
- Functional Dependency describes how column values determine other column values.
- Written as (X uniquely determines the value in Y).
- Primary keys functionally determine all other columns in their table.
- Full dependency requires all columns of a composite key to determine a value.
- Transitive dependency maps indirect relationships ().
- Use dependency mapping to find and fix database schema anomalies.