Normalization and deliberate denormalization
Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.
What you will be able to do
#- Identify update, insert and delete anomalies in an unnormalized table.
- Apply first, second and third normal form to a schema.
- Decide when denormalization is worth its consistency cost.
Overview
#Normalization removes redundancy so one fact lives in one place. Each normal form removes a class of anomaly.
The three anomalies
- Update anomaly: a repeated value must be changed in many rows, and a partial update leaves inconsistency.
- Insert anomaly: you cannot record a fact without inventing unrelated data.
- Delete anomaly: deleting one row destroys an unrelated fact.
Normal forms
- 1NF: atomic columns, no repeating groups.
- 2NF: 1NF plus no partial dependency on part of a composite key.
- 3NF: 2NF plus no transitive dependency of a non-key column on another non-key column.
orders(order_id, customer_id, customer_name, customer_email)
customer_name and customer_email depend on customer_id, not on order_id. Move them to a customers table and reference customer_id.
Deliberate denormalization
Sometimes you copy a value to avoid a join on a hot read path, for example storing comment_count on the article. That is a cache, not a source of truth: it must be updated in the same transaction or rebuilt, and it can drift.
Common pitfalls
- Denormalizing before measuring, then owning two sources of truth.
- Storing derived totals that no process keeps consistent.
- Confusing a read model with the write model.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Explain
Name the anomaly in this table and the normal form it violates.
starter.textenrollments(student_id, course_id, student_name, course_title)Reveal the reference answer
reference.textstudent_name depends on student_id and course_title depends on course_id, both non-key attributes of the composite key (student_id, course_id). That is a partial dependency, so it violates second normal form.Authored expected output: Identifies a partial dependency on part of the composite key, violating 2NF.
Splitting into students and courses tables removes the redundancy and the update anomaly.
Exercise 2Predict
You store comment_count on articles to avoid a count join. A comment is deleted directly by another service. What is now wrong?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.txtarticles.comment_count -- denormalized DELETE FROM comments WHERE id = 9;
Common misconceptions
#- Normalization is always correct: a measured read bottleneck can justify controlled denormalization.
- Denormalized fields stay correct automatically: they require explicit maintenance and can drift.
Follow-up practice
#- backend-databases-transactions
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.