skip to content

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.

fd =
query.sql
  1. 1SELECT course_code, course_title, count(*) AS rows,
  2. 2 count(*) OVER (PARTITION BY course_code) = 1 AS agrees
  3. 3FROM enrollments_flat GROUP BY course_code, course_title ORDER BY course_code, course_title;
the result for course_code → course_title
course_codetextcourse_titletextrowsint8agreesbool
CS101Intro to Programming2t
CS201Data Structures4t
CS301Databases3t
CS350Operating Systems4t
CS401Database Internals2t
HIST101History of Computing2t
MATH101Calculus I2t
MATH201Discrete Math3t
MATH301Linear Algebra2t
PHYS101Mechanics3t
PHYS201Electromagnetism3t
(11 rows)

every course code has one title in these 30 rows. it's a rule, too: courses are keyed by code.

real output, postgres 18.6 (PGlite 0.5.12) · fd-check
pick a dependency. each row is one distinct (X, Y) pair in the school data, with how many rows carry it, and postgres's verdict: 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.

sqldb=school
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;
output, from postgres 18.6
SELECT 1
instructor_nametextofficesint8whichtext
Hannah Okoye2B-101, B-199
(1 row)

one row back is a violation. here are the rows behind it:

sqldb=school
SELECT student_name, course_code, instructor_name, instructor_office
FROM enrollments_flat
WHERE instructor_name = 'Hannah Okoye'
ORDER BY student_name, course_code;
output, from postgres 18.6
SELECT 5
student_nametextcourse_codetextinstructor_nametextinstructor_officetext
Emma DuboisCS101Hannah OkoyeB-199
Kenji WatanabeCS301Hannah OkoyeB-101
Noah SchmidtCS301Hannah OkoyeB-101
Priya NairCS101Hannah OkoyeB-101
Priya NairCS301Hannah OkoyeB-101
(5 rows)
student_nametextcourse_codetextinstructor_nametextinstructor_officetext
Emma DuboisCS101Hannah OkoyeB-199
Kenji WatanabeCS301Hannah OkoyeB-101
Noah SchmidtCS301Hannah OkoyeB-101
Priya NairCS101Hannah OkoyeB-101
Priya NairCS301Hannah OkoyeB-101
(5 rows)
Hannah Okoye's five rows in enrollments_flat, from the block above. four say B-101. one says B-199, and since instructor_name → instructor_office should hold, that row is the counterexample: an office someone updated in one place only.

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)).

predict the output
sql
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:

sqldb=school
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;
output, from postgres 18.6
SELECT 3
idint4nametextemailtextenrollmentsint8
18Clara Novakclara.novak@uni.example8
26Leila Karimileila.karimi@uni.example8
5Emma Duboisemma.dubois@uni.example7
(3 rows)

group by the name instead, and postgres won't assume name → email, rightly:

sqldb=school
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;
output, from postgres 18.6
ERROR 42803: column "s.email" must appear in the GROUP BY clause or be used in an aggregate function
1 │ SELECT s.name, s.email, count(*) AS enrollments
at line 1, column 16

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.

table
start
dependencies
  • order_id, sku → qty
  • sku → product, list_price
  • order_id → ordered_on, email
  • email → customer, address
closure of {order_id}, step 0 of 2
order_idskuqtyproductlist_priceordered_onemailcustomeraddress

start with {order_id}. a set always determines itself.

candidate keys of this table: {order_id, sku}
step 1/3
the textbook closure algorithm, run in this page
pick a table, pick starting attributes, and step. a dependency lights up hot when its left side is already inside, and its right side joins the closure. reach every attribute and the start set is a key; the line under it lists every candidate key.

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.