wasAsked.

What is database normalization and what are the normal forms?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · SQL

Community answers

Posted by wasAsked editorial collection

Normalization is the process of organising a relational database into tables so that each fact is stored once, reducing redundancy and preventing update, insert and delete anomalies. It is done by applying a series of rules called normal forms. Why it matters — anomalies in an unnormalized table OrderId | CustomerName | CustomerPhone | Product | Price - Update anomaly: changing a customer's phone requires updating many rows; missing one leaves inconsistent data. - Insert anomaly: you cannot store a new customer until they place an order. - Delete anomaly: deleting a customer's only order also deletes the customer's details. First Normal Form (1NF) - Each column contains atomic (indivisible) values; no repeating groups or lists in a column; each row is unique (has a primary key). - Violation: a Phones column containing "98765, 91234". Fix: a separate CustomerPhones table, one phone per row. Second Normal Form (2NF) - Must be in 1NF, and every non-key column must depend on the whole primary key, not just part of a composite key (no partial dependency). - Violation: OrderItems(OrderId, ProductId, Quantity, ProductName) — ProductName depends only on ProductId. Fix: move ProductName to a Products table. Third Normal Form (3NF) - Must be in 2NF, and non-key columns must not depend on other non-key columns (no transitive dependency). - Violation: Employees(EmployeeId, DepartmentId, DepartmentName) — DepartmentName depends on DepartmentId, not directly on EmployeeId. Fix: a Departments table. - A common summary: every non-key attribute depends on "the key, the whole key, and nothing but the key". Boyce-Codd Normal Form (BCNF) - A stricter 3NF: for every functional dependency X → Y, X must be a candidate key. Handles edge cases with overlapping candidate keys (for example, a course scheduling table where an instructor determines the course). Fourth Normal Form (4NF) - No multi-valued dependencies: independent multi-valued facts should not be stored in the same table. Example: Employee, Skill and Language in one table creates artificial combinations; split into EmployeeSkills and EmployeeLanguages. Fifth Normal Form (5NF) - Deals with join dependencies; tables cannot be decomposed further without losing information. Rarely relevant in everyday design. Normalized design example Customers(CustomerId PK, Name, Phone) Products(ProductId PK, Name, Price) Orders(OrderId PK, CustomerId FK, OrderDate) OrderItems(OrderId FK, ProductId FK, Quantity, UnitPrice, PK(OrderId, ProductId)) Note: UnitPrice is deliberately stored in OrderItems as the price at the time of purchase — a historical fact, not redundancy. Denormalization - Deliberately adding redundancy to improve read performance: reporting tables, data warehouses (star schemas), cached totals, read models in CQRS. - Trade-off: faster reads and simpler queries vs more storage and the need to keep copies consistent. Practical guidance - Most transactional (OLTP) systems aim for 3NF or BCNF. - Analytical systems (OLAP) often use denormalized star or snowflake schemas. - Normalize first for correctness, then denormalize specific areas when performance measurements justify it. Interview summary: "Normalization organises data to remove redundancy and anomalies. 1NF requires atomic values, 2NF removes partial dependencies on composite keys, 3NF removes transitive dependencies, and BCNF tightens 3NF. Most OLTP databases target 3NF and denormalize selectively for performance."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics