functional dependencies
what determines what. the bit of math that decides your keys and your normal forms.
- address
- everything.design.functional_dependencies
- part
- part 7, schema design
- chapter
- chapter 5 of 13
- reading time
- 8 min
a functional dependency X → Y says: any two rows that agree on X agree on Y. course code determines course title; a student's id determines their name.
it's a rule about every row the table could ever hold, so data can only disprove one (one counterexample), never prove it. the closure X⁺ is everything X determines, and X is a candidate key when X⁺ is every column and no smaller set manages it. keys and normal forms are both built from this.
the picture
you know how you can look at a spreadsheet and feel that some columns repeat? every row with CS101 says "Intro to Programming", every row with Hannah Okoye says B-101. that feeling has a name and a test.
think of a phone book: the number determines the person who answers (number → person), but a name doesn't determine a number, because there are two Alex Kims. X → Y is a promise in one direction. it says nothing about Y → X.
- 1SELECT course_code, course_title, count(*) AS rows,
- 2 count(*) OVER (PARTITION BY course_code) = 1 AS agrees
- 3FROM enrollments_flat GROUP BY course_code, course_title ORDER BY course_code, course_title;
| course_codetext | course_titletext | rowsint8 | agreesbool |
|---|---|---|---|
| CS101 | Intro to Programming | 2 | t |
| CS201 | Data Structures | 4 | t |
| CS301 | Databases | 3 | t |
| CS350 | Operating Systems | 4 | t |
| CS401 | Database Internals | 2 | t |
| HIST101 | History of Computing | 2 | t |
| MATH101 | Calculus I | 2 | t |
| MATH201 | Discrete Math | 3 | t |
| MATH301 | Linear Algebra | 2 | t |
| PHYS101 | Mechanics | 3 | t |
| PHYS201 | Electromagnetism | 3 | t |
every course code has one title in these 30 rows. it's a rule, too: courses are keyed by code.
agrees is false, and the row dead, when the same X shows up with a second Y. the third and fourth are the same claim on 30 rows and on all 40 students.look at the last two. in enrollments_flat every student name happens to come with one email, so the data agrees with student_name → student_email. across all students it breaks: two different people called Alex Kim. a dependency comes from what the columns mean (an email identifies a person, a name doesn't). the data can only catch you being wrong.
checking one in SQL
"rows that agree on X agree on Y" turns into a query almost word for word: group by X, and look for a group with more than one distinct Y.
SELECT instructor_name, count(DISTINCT instructor_office) AS offices,
string_agg(DISTINCT instructor_office, ', ' ORDER BY instructor_office) AS which
FROM enrollments_flat
GROUP BY instructor_name
HAVING count(DISTINCT instructor_office) > 1
ORDER BY instructor_name;| instructor_nametext | officesint8 | whichtext |
|---|---|---|
| Hannah Okoye | 2 | B-101, B-199 |
one row back is a violation. here are the rows behind it:
SELECT student_name, course_code, instructor_name, instructor_office
FROM enrollments_flat
WHERE instructor_name = 'Hannah Okoye'
ORDER BY student_name, course_code;| student_nametext | course_codetext | instructor_nametext | instructor_officetext |
|---|---|---|---|
| Emma Dubois | CS101 | Hannah Okoye | B-199 |
| Kenji Watanabe | CS301 | Hannah Okoye | B-101 |
| Noah Schmidt | CS301 | Hannah Okoye | B-101 |
| Priya Nair | CS101 | Hannah Okoye | B-101 |
| Priya Nair | CS301 | Hannah Okoye | B-101 |
| student_nametext | course_codetext | instructor_nametext | instructor_officetext |
|---|---|---|---|
| Emma Dubois | CS101 | Hannah Okoye | B-199 |
| Kenji Watanabe | CS301 | Hannah Okoye | B-101 |
| Noah Schmidt | CS301 | Hannah Okoye | B-101 |
| Priya Nair | CS101 | Hannah Okoye | B-101 |
| Priya Nair | CS301 | Hannah Okoye | B-101 |
the B-199 row is the update anomaly the dataset plants on purpose. zero rows means "not disproved yet". for a dependency with several columns on either side, group by all of X and count distinct rows of Y: count(DISTINCT (y1, y2)).
SELECT course_code
FROM enrollments_flat
GROUP BY course_code
HAVING count(DISTINCT course_title) > 1;the one postgres knows about
postgres doesn't track your dependencies, with one exception: a primary key determines every other column of its table. that's why you can GROUP BY a key and still select the other columns:
SELECT s.id, s.name, s.email, count(*) AS enrollments
FROM students AS s JOIN enrollments AS e ON e.student_id = s.id
GROUP BY s.id
ORDER BY enrollments DESC, s.id
LIMIT 3;| idint4 | nametext | emailtext | enrollmentsint8 |
|---|---|---|---|
| 18 | Clara Novak | clara.novak@uni.example | 8 |
| 26 | Leila Karimi | leila.karimi@uni.example | 8 |
| 5 | Emma Dubois | emma.dubois@uni.example | 7 |
group by the name instead, and postgres won't assume name → email, rightly:
SELECT s.name, s.email, count(*) AS enrollments
FROM students AS s JOIN enrollments AS e ON e.student_id = s.id
GROUP BY s.name;the standard calls this functional dependence and lets a database infer far more (from UNIQUE NOT NULL columns, from equalities in WHERE). postgres only uses primary keys of the grouped tables. (GROUP BY has the rest.)
closure: everything X drags along
dependencies combine. if order_id → email and email → address, then order_id → address. three rules, Armstrong's axioms, generate every dependency that follows from a set:
- reflexivity
- if Y is a subset of X, then X → Y. trivially, (a, b) → a.
- augmentation
- if X → Y, then XZ → YZ. add the same columns to both sides.
- transitivity
- if X → Y and Y → Z, then X → Z.
in practice you don't apply axioms by hand, you compute a closure: start with X, and keep adding the right side of any dependency whose left side you already have, until nothing changes. what you end with, X⁺, is everything X determines. if X⁺ is every column, X is a superkey; if it is and no proper subset of X is, X is a candidate key.
- order_id, sku → qty
- sku → product, list_price
- order_id → ordered_on, email
- email → customer, address
start with {order_id}. a set always determines itself.
try orders_wide from order_id alone: you get the order's date, the customer and the address, but never the product or the qty, so it isn't a key. add sku and everything arrives. tutoring has two candidate keys, (student, subject) and (student, tutor), and that overlap is exactly the BCNF corner case. enrollments_flat needs the term too: the same student can take the same course twice.
from dependencies to keys and forms
once you have the dependencies, the rest of design is mechanical:
- the candidate keys are the minimal sets whose closure is everything. pick one as the primary key, put UNIQUE on the others (keys).
- a dependency whose left side isn't a key is redundancy waiting to happen. its right side gets repeated once per row with the same left side.
- splitting the table so that left side becomes a key of its own table removes the redundancy, and keeps the join lossless. that's every step of normal forms.
keylines
- X → Y: rows that agree on X agree on Y. one direction only.
- data can disprove a dependency, never prove it. GROUP BY X HAVING count(DISTINCT Y) > 1 finds the counterexamples.
- the closure X⁺ is everything X determines. X⁺ = all columns means X is a superkey.
- a candidate key is a minimal superkey; a table can have several.
- postgres knows one dependency: a primary key determines its row, so GROUP BY the key works.