Working with Normalization

1. Understanding Functional Dependencies

TypeNotationMeaning
Functional DependencyX → YX uniquely determines Y
Full FD{A,B} → CBoth A and B needed
Partial FDA → C (subset of PK)Violates 2NF
Transitive FDA → B → CViolates 3NF
Trivial FDX → Y where Y ⊆ XAlways holds
Multi-valued (MVD)X →→ YViolates 4NF

2. Applying First Normal Form

1NF RuleRequirement
Atomic ValuesNo lists, sets, or composite values in a cell
No Repeating GroupsNo phone1, phone2, phone3 columns
Unique RowsPrimary key defined
Same Type per ColumnConsistent data type in each column

Example: 1NF Conversion

-- BAD: multi-valued
-- customer(id, name, phones='555-1, 555-2')

-- 1NF: split into separate table
CREATE TABLE customer_phones (
  customer_id BIGINT REFERENCES customers(id),
  phone       VARCHAR(20),
  PRIMARY KEY (customer_id, phone)
);

3. Applying Second Normal Form

2NF RuleRequirement
Must be 1NFAlready satisfies 1NF
No Partial DependenciesNon-key attrs depend on entire PK, not part
Applies WhenPK is composite

Example: Removing Partial Dependency

-- BAD: product_name depends only on product_id, not (order_id, product_id)
-- order_items(order_id, product_id, qty, product_name)

-- 2NF: move product_name to products
CREATE TABLE products    (id BIGSERIAL PK, name TEXT);
CREATE TABLE order_items (
  order_id   BIGINT, product_id BIGINT, qty INT,
  PRIMARY KEY (order_id, product_id)
);

4. Applying Third Normal Form

3NF RuleRequirement
Must be 2NFAlready satisfies 2NF
No Transitive DependenciesNon-key attrs depend only on PK
TestFor each non-trivial FD X→A, X must be superkey OR A part of candidate key

Example: Removing Transitive Dependency

-- BAD: zip → city, state (transitive via zip)
-- customer(id, name, zip, city, state)

-- 3NF
CREATE TABLE zip_codes (zip CHAR(5) PRIMARY KEY, city TEXT, state CHAR(2));
CREATE TABLE customers (id BIGSERIAL PK, name TEXT, zip CHAR(5) REFERENCES zip_codes(zip));

5. Applying Boyce-Codd Normal Form (BCNF)

BCNF RuleRequirement
Must be 3NFStricter form of 3NF
Determinant is SuperkeyFor every non-trivial FD X→Y, X must be superkey
When MattersOverlapping candidate keys exist

6. Applying Fourth Normal Form

4NF RuleRequirement
Must be BCNFPlus eliminate MVDs
No Multi-valued DependenciesIndependent multi-valued facts in separate tables
Note: If student → courses and student → hobbies are independent, put them in separate tables — not a single (student, course, hobby) table.

7. Applying Fifth Normal Form

5NF RuleRequirement
Must be 4NFPlus no join dependencies that aren't implied by keys
DecomposableCannot lose info when splitting/rejoining tables
Rare in PracticeMostly theoretical concern

8. Identifying Normalization Anomalies

AnomalySymptomCaused By
Insert AnomalyCan't add data without unrelated dataMissing entity decomposition
Update AnomalySame fact in multiple rows; partial updatesRedundancy
Delete AnomalyRemoving row loses unrelated infoCoupled entities
RedundancySame data stored in many rowsLack of decomposition

9. Using Normalization Tools and Techniques

ToolPurpose
ERwin / ER/StudioER modeling with normalization
dbdiagram.io / DrawSQLSchema diagrams with FK detection
DBeaver / DataGripReverse-engineer + FD discovery
SchemaSpyGenerates schema docs, finds anomalies
Synthesis Algorithm3NF synthesis from FDs
Decomposition AlgorithmBCNF lossless-join decomposition

10. Balancing Normalization vs Performance

GoalApproach
OLTP (writes)3NF/BCNF — minimize redundancy, fast updates
OLAP (reads)Star/snowflake — denormalize fact tables
Hybrid (HTAP)Normalized core + materialized read views
Trade-offMore joins (normalized) vs more storage + update cost (denormalized)
Warning: Don't denormalize until profiling proves a normalized schema is too slow. Premature denormalization creates data integrity bugs.