Learning path

Full curriculum

Full curriculum

Unit content

Database constraints and normalization

A database schema should make invalid states difficult to represent. Constraints enforce local rules, while normalization organizes relations to reduce unnecessary duplication and update anomalies.

Common constraints

A column can be required with NOT NULL, restricted to unique values with UNIQUE, or checked by a condition with CHECK.

Primary-key and foreign-key constraints express identity and referential integrity.

These rules belong in the database when they describe invariants that must hold regardless of which application code performs the write.

Update anomalies

Suppose a table repeats an author's email in every row containing one of that author's books. Changing the email then requires updating several rows consistently.

If one copy is missed, the database contains contradictory representations of the same fact.

Functional dependencies

A functional dependency expresses that one set of attributes determines another. If author_id uniquely determines author_email, storing that email repeatedly beside unrelated book facts mixes dependencies with different meanings.

Normalization

Normalization decomposes relations so that facts with different dependencies are represented separately and connected through keys.

The common normal forms provide increasingly strict criteria for avoiding particular redundancy patterns. The goal is not to maximize the number of tables, but to give each stored fact one clear source of truth.

Denormalization can be useful for performance in some systems, but it deliberately reintroduces duplication and therefore requires a plan for keeping duplicated data consistent.