Database Normalization Basics: First, Second and Third Normal Form
Normalization is a design method that splits tables so that each fact is written in only one place. Instead of heavy terminology, we follow a single order table and see how it is tidied from first to third normal form.
Why normalization is needed
Putting customers, products and orders in one big table feels convenient at first. But the same customer address repeats once per order, a changed address must be corrected on every row, and if even one row is missed, the same customer ends up with two different addresses. These problems are called anomalies.
There are three kinds. An update anomaly is when only some copies get changed, an insertion anomaly is when a new product cannot be registered without an order, and a deletion anomaly is when removing the last order also erases the customer. Normalization prevents all three in advance.
Update anomaly: the same value on many rows gets changed only partly
Insertion anomaly: new information cannot be added without other unrelated information
Deletion anomaly: deleting one fact accidentally erases another
First normal form: one value per cell
First normal form requires that each cell holds a single value. If the product cell of an order table says pencil, notebook, eraser separated by commas, it is not in first normal form. Such a cell makes it hard to find orders containing a particular product and hard even to count items.
The fix is to write one row per product in each order. Then the pair of order number and product identifies a row, and the repeating group disappears.
Bad example: several values such as pencil, notebook in one product cell
Fixed example: order 1 with pencil on one row and order 1 with notebook on another
State clearly which column or columns form the primary key that identifies a row
Second normal form: separate facts that depend on part of the key
Second normal form matters when the primary key has two or more columns. If a table keyed by order number and product number also stores product name and unit price, those values depend only on the product number, not on the whole key. The name and price repeat every time that product is ordered.
The fix is to make a product table keyed by product number only, move name and price there, and leave only order number, product number and quantity in the order detail table. If the price changes, you correct a single place in the product table.
Original table: order number, product number, product name, unit price, quantity
Result: product (number, name, price) and order detail (order number, product number, quantity)
If you must preserve the price at the time of purchase, you can choose to store that price in the order detail on purpose
Third normal form: separate facts that depend on non-key columns
Third normal form removes cases where one non-key column determines another non-key column. If an order table holds customer number along with customer name and city, the name and city belong to the customer, not to the order. They are determined only indirectly through the order, which is called a transitive dependency.
The fix is to create a customer table, move name and city into it, and leave only the customer number in the order table. Now when a customer moves, changing one row in the customer table is reflected in every order.
Original order: order number, customer number, customer name, customer city, order date
Result: customer (number, name, city) and order (order number, customer number, order date)
Connect the order's customer number to the customer table with a foreign key
Limits of normalization, and checking with an ERD
The more you normalize, the more tables there are and the more joining queries need. For report-oriented data that is read far more than written, designers sometimes allow duplication on purpose. A safe approach is to reach third normal form first and only step back carefully when a real performance problem appears.
After splitting tables, it helps to check the result as a picture. Paste your table definitions into the SQL to ERD tool on this site and the tables and relationships are drawn, so you can see whether the same fact repeats across tables and whether any foreign key is missing.
Check 1: when this value changes, is there only one place to edit?
Check 2: is every column explained by the whole key and nothing but the key?
Check 3: are all foreign keys that connect the tables drawn?