4 Hours

3. Normalization: 1NF, 2NF, 3NF & Beyond

Normalization

Normalization Theory: 1NF, 2NF, 3NF

Normalization is the mathematical process of organizing data to minimize redundancy. Redundant data is the enemy: it wastes disk space and leads to Update Anomalies (where data is updated in one place but remains old in another). Normalization is a foundational concept in relational database design.

Normalization has several normal forms. Most developers aim for Third Normal Form (3NF). Anything less is considered a Smelly database design. However, normalization is not an absolute rule — sometimes denormalization is necessary for performance. This is an engineering trade-off: correctness vs. performance. Understanding normalization helps you make informed decisions.

This module covers the three main normal forms, the anomalies they prevent, and when and why you might denormalize. You'll learn to identify normalization issues and apply normalization to your schemas.

1st Normal Form (1NF)

1st Normal Form requires that each cell in a table contains exactly one value (atomic values). No repeating groups or arrays. This means you can't have columns like phone_numbers with multiple phone numbers separated by commas. You also can't have columns like order_items with multiple items. Instead, you create separate rows for each value.

Why is 1NF important? It ensures that each piece of data is stored in a single, atomic cell. This makes queries simpler and more predictable. It also prevents data duplication and makes updates easier. Violations of 1NF are common in poorly designed schemas. For example, storing multiple email addresses in a single column is a violation of 1NF.

1NF VIOLATION1NF COMPLIANT
id | name | phone_numbers
1 | Alice | 555-1234, 555-5678
2 | Bob | 555-9012
3 | Carol | 555-3456, 555-7890
user_id | phone | type
1 | 555-1234 | home
1 | 555-5678 | work
2 | 555-9012 | mobile
3 | 555-3456 | home
3 | 555-7890 | work
SQL
-- Violation of 1NF: Multiple values in a cell
CREATE TABLE users_bad (
  id INT PRIMARY KEY,
  name TEXT,
  email TEXT,
  phone_numbers TEXT -- Contains: "555-1234,555-5678"
);

-- 1NF compliant: Atomic values
CREATE TABLE users (
  id INT PRIMARY KEY,
  name TEXT,
  email TEXT
);

CREATE TABLE phone_numbers (
  user_id INT REFERENCES users(id) ON DELETE CASCADE,
  phone TEXT,
  type TEXT CHECK (type IN ('home', 'work', 'mobile')),
  PRIMARY KEY (user_id, phone)
);

2nd Normal Form (2NF)

2nd Normal Form eliminates partial dependencies. In a table with a composite primary key (multiple columns), every non-key column must depend on the whole primary key, not just part of it. If a column depends on only part of the composite key, it should be moved to its own table.

For example, consider an order_items table with (order_id, product_id) as the composite key. If the product name depends only on product_id (not the whole key), this is a partial dependency and violates 2NF.

2NF VIOLATION2NF COMPLIANT
ORDER_ITEMS (Composite PK)
order_id | product_id | quantity | product_name | product_price
101 | 1 | 2 | Laptop | 999.99
101 | 2 | 1 | Mouse | 29.99
102 | 1 | 3 | Laptop | 999.99

PROBLEM: product_name depends ONLY on product_id!
ORDER_ITEMS
order_id | product_id | quantity
101 | 1 | 2
101 | 2 | 1
102 | 1 | 3

PRODUCTS
id | name | price
1 | Laptop | 999.99
2 | Mouse | 29.99
SQL
-- Violation of 2NF: Partial dependency
CREATE TABLE order_items_bad (
  order_id INT,
  product_id INT,
  quantity INT,
  product_name TEXT, -- Depends only on product_id
  product_price NUMERIC, -- Depends only on product_id
  PRIMARY KEY (order_id, product_id)
);

-- 2NF compliant
CREATE TABLE products (
  id INT PRIMARY KEY,
  name TEXT,
  price NUMERIC
);

CREATE TABLE order_items (
  order_id INT,
  product_id INT REFERENCES products(id),
  quantity INT,
  PRIMARY KEY (order_id, product_id)
);

3rd Normal Form (3NF)

3rd Normal Form eliminates transitive dependencies. A transitive dependency occurs when a non-key column depends on another non-key column, rather than directly on the primary key. In other words, columns should only depend on the primary key, not on other non-key columns.

For example, consider a table with (order_id, customer_id, customer_name). customer_name depends on customer_id, not on order_id. This is a transitive dependency and violates 3NF.

3NF VIOLATION3NF COMPLIANT
ORDERS_BAD
id | customer_id | customer_name | order_date | total
1 | 101 | Alice | 2024-01-01 | 150.00
2 | 102 | Bob | 2024-01-02 | 75.50

PROBLEM: customer_name depends on customer_id (transitive dependency)
ORDERS
id | customer_id | order_date | total
1 | 101 | 2024-01-01 | 150.00
2 | 102 | 2024-01-02 | 75.50

CUSTOMERS
id | name
101 | Alice
102 | Bob
SQL
-- Violation of 3NF: Transitive dependency
CREATE TABLE orders_bad (
  id INT PRIMARY KEY,
  customer_id INT,
  customer_name TEXT, -- Depends on customer_id, not on order_id
  order_date DATE,
  total DECIMAL(10,2)
);

-- 3NF compliant
CREATE TABLE customers (
  id INT PRIMARY KEY,
  name TEXT
);

CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT REFERENCES customers(id),
  order_date DATE,
  total DECIMAL(10,2)
);

Identifying Update Anomalies

Why do we normalize? To prevent update anomalies. Imagine a table where we store store_name and store_address in every single product row. This leads to three types of anomalies:

  • Insertion Anomaly: Can't add a new store without a product.
  • Deletion Anomaly: Deleting the last product deletes the store.
  • Update Anomaly: Updating the store address requires updating 10,000 rows.

Normalization prevents these anomalies by storing each piece of data in exactly one place. This is why normalization is so important. It ensures data integrity and consistency. A normalized database is easier to maintain and less prone to errors.

Knowledge Check

Ready to test your understanding of 3. Normalization: 1NF, 2NF, 3NF & Beyond?

Back