Find a course

Module 3: NormalizationLesson 6 of 1214 min

Anomalies and Functional Dependencies

Identify insert, update and delete anomalies in a poorly designed table and describe the hidden rules behind them as functional dependencies.

Step 1 of 13Learn

Three ways a bad table bites

Here's a table a course coordinator built to track enrollments:

student_idstudent_namecourse_codecourse_titleinstructorinstructor_office
101MayaDB201DatabasesDr. LinB-214
101MayaST110StatisticsDr. OrtizC-105
102OmarDB201DatabasesDr. LinB-214
103JessDB201DatabasesDr. LinB-214

It looks tidy, but it causes three kinds of anomalies:

  • Update anomaly. Dr. Lin moves to office B-300. Her office appears in every DB201 enrollment row, potentially hundreds of them. Miss one and the database contradicts itself.
  • Insert anomaly. A new course, AI300, is approved, but no students have enrolled yet. You can't add it without inventing a fake student, because every row needs a student_id.
  • Delete anomaly. Omar and Jess drop DB201, and then Maya does too. When the last enrollment row is deleted, the facts that DB201 exists, is called Databases and is taught by Dr. Lin in B-214 vanish with it.

All three come from the same root cause: the table stores facts about different things (students, courses, instructors) in one place, so some facts are repeated in many rows and others exist only as a side effect of unrelated rows. Normalization is the systematic process of splitting such tables until each table describes one thing.

Step 2 of 13Practice · Match the pairs

Match each event in the enrollment table to the anomaly it illustrates.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Changing an instructor's office requires editing many rows
  2. A new course cannot be recorded until a student enrolls
  3. Removing the last enrollment erases the course's title

Step 3 of 13Compare

The three anomalies side by side

AspectWhat goes wrongIn the enrollment table
Update anomalyOne fact stored in many rows; miss one and data contradicts itselfDr. Lin’s office changed in some DB201 rows only
Insert anomalyCannot record a fact without an unrelated oneNew course AI300 needs a fake student
Delete anomalyRemoving one fact destroys anotherLast DB201 enrollment deleted, course facts vanish

Step 4 of 13Learn

Functional dependencies

To fix anomalies systematically, you need precise language for the rules hiding in the data. That language is the functional dependency (FD).

We write A → B ("A determines B") when each value of A is always associated with exactly one value of B. If you know A, you know B. In the enrollment table:

  • student_id → student_name (a student ID belongs to one name)
  • course_code → course_title, instructor (each course has one title and one instructor)
  • instructor → instructor_office (each instructor has one office)
  • (student_id, course_code) → everything else in the row

The left side is the determinant. Note the direction: student_id → student_name holds, but student_name → student_id does not, because two students can share a name.

FDs are statements about the meaning of the data, not about the rows that happen to be there today. If the current table has only one student named Maya, the data doesn't contradict "student_name → student_id," but the rule is still false, because a second Maya could enroll tomorrow. You find FDs by asking the business, not by staring at sample data. Once you have them, they tell you exactly which facts are being repeated and how to split the table.

Step 5 of 13Practice · True or false

If the current rows of a table never show two different titles for the same course_code, that proves course_code → course_title.

Step 6 of 13Learn

Keys and dependencies

Functional dependencies give a precise definition of a key. A candidate key is a minimal set of columns that determines every other column in the table. In the enrollment table, student_id alone isn't a key (Maya has two rows), and course_code alone isn't either. Together, (student_id, course_code) determines everything, and neither part alone does, so it is the candidate key.

Columns that belong to some candidate key are prime attributes; all others are non-prime. Here, student_id and course_code are prime, and student_name, course_title, instructor and instructor_office are non-prime.

Now look at what the non-prime columns depend on:

  • student_name depends on only part of the key (student_id). This is a partial dependency.
  • instructor_office depends on instructor, which depends on course_code. It depends on the key only through another non-key column. This is a transitive dependency.

These two patterns are exactly what the normal forms target. Second normal form removes partial dependencies; third normal form removes transitive dependencies. Each one you remove eliminates a family of anomalies.

Step 7 of 13Practice · Sort into groups

In the enrollment table with key (student_id, course_code), classify each dependency.

Tap an item, then tap its group (or drag it there).

Step 8 of 13Practice · Diagram

Who determines what in the enrollment table

Read each branch as an arrow: the parent determines the child. Columns hanging off only part of the key, or off a non-key column, signal trouble.

Which column depends on the key only through another non-key column?

Tap the shape that answers the question, then press Check.

Who determines what in the enrollment tableHierarchy diagram: (student_id, course_code), student_id, course_code, grade, student_name, course_title, instructor, instructor_office.(student_id,course_code)student_idcourse_codegradestudent_namecourse_titleinstructorinstructor_office
(student_id, course_code)
student_id
course_code
grade

Needs both parts of the key: a full dependency.

student_name

Depends on part of the key: partial dependency (breaks 2NF).

course_title
instructor
instructor_office

Step 9 of 13Note

Step 10 of 13Diagram

The normal forms build on each other

Each level includes every rule below it. Most practical designs aim for 3NF or BCNF.

The normal forms build on each otherPyramid diagram: 1NF: atomic values, a key, 2NF: the whole key, 3NF: nothing but the key, BCNF, 4NF and 5NF.1NF: atomic values, a key2NF: the whole key3NF: nothing but the keyBCNF4NF and5NF

Tap a shape with to see more.

1NF: atomic values, a key

One value per cell, no repeating groups.

2NF: the whole key

No column depends on just part of a composite key.

3NF: nothing but the key

No column depends on another non-key column.

BCNF

Every determinant is a superkey.

4NF and 5NF

Independent multivalued facts and join dependencies; rarely needed by name.

Step 11 of 13Learn

Finding dependencies in practice

Real projects don't come with a list of FDs. You uncover them with targeted questions, column by column:

  1. For each column, ask what determines it. "If I know the ISBN, do I know the title? The price? The publisher?" Yes to all three.
  2. Test the reverse. "If I know the title, do I know the ISBN?" No, because different editions share titles.
  3. Check composite cases. "Does the quantity depend on the order alone, or on the order and the book together?" Order and book together.
  4. Look for codes and names travelling together. Whenever a code column sits next to its description (course_code and course_title, zip and city, product_id and product_name), the description almost certainly depends on the code, not on the table's key. That's a sign of a table that should be split.
  5. Watch for repeated blocks. If the same three columns repeat identically across many rows, some determinant is hiding there.

Be careful with assumptions that seem obvious. Does a ZIP code determine the city? In the US, mostly, but some ZIP codes span more than one city name. Does an employee have exactly one phone? Not if the business records work and mobile numbers. Every dependency you declare becomes a design decision, so confirm the edge cases with the people who know the data.

Step 12 of 13Practice · Select all that apply

In a table book_sale(sale_id, isbn, title, publisher, publisher_city, sale_date, quantity), with sale_id as the key, which dependencies are likely to hold? Select all that apply.

Select all that apply.

Step 13 of 13Practice · Choose one

Which description of a candidate key is correct?