The Third Normal Form (3NF) is a crucial step in database normalization, building upon the principles of the First Normal Form (1NF) and Second Normal Form (2NF). Its primary goal is to eliminate transitive dependencies, ensuring that each non-key attribute is directly dependent on the primary key.
A transitive dependency occurs when a non-key attribute is dependent on another non-key attribute, which in turn is dependent on the primary key. In simpler terms, if we have attributes A, B, and C, where A is the primary key, and A → B and B → C, then C is transitively dependent on A through B.
Consider the following example. Let's say we have a table representing orders:
OrderID (PK) | CustomerID | CustomerName | CustomerCity | OrderDate | OrderAmount |
---|---|---|---|---|---|
101 | C1001 | Alice Smith | New York | 2023-10-26 | 150.00 |
102 | C1002 | Bob Johnson | Los Angeles | 2023-10-26 | 75.50 |
103 | C1001 | Alice Smith | New York | 2023-10-27 | 200.00 |
In this table:
OrderID
is the primary key.CustomerID
is dependent on OrderID
(each order has one customer).CustomerName
and CustomerCity
are dependent on CustomerID
(a customer has a name and city).This creates a transitive dependency: OrderID
→ CustomerID
→ CustomerName
, OrderID
→ CustomerID
→ CustomerCity
.
This leads to anomalies:
A relation is in Third Normal Form (3NF) if and only if it is in Second Normal Form (2NF) and every non-prime attribute is non-transitively dependent on every candidate key.
In simpler terms:
To remove transitive dependencies and achieve 3NF, we decompose the table into smaller, more focused tables. Each new table should represent a single entity or concept.
We split the original table into two:
CustomerID (PK) | CustomerName | CustomerCity |
---|---|---|
C1001 | Alice Smith | New York |
C1002 | Bob Johnson | Los Angeles |
OrderID (PK) | CustomerID (FK) | OrderDate | OrderAmount |
---|---|---|---|
101 | C1001 | 2023-10-26 | 150.00 |
102 | C1002 | 2023-10-26 | 75.50 |
103 | C1001 | 2023-10-27 | 200.00 |
In the Orders
table, CustomerID
is a foreign key referencing the Customers
table. Now, CustomerName
and CustomerCity
are no longer in the Orders
table, eliminating the transitive dependency. All attributes in the Customers
table are directly dependent on CustomerID
, and all attributes in the Orders
table (except the foreign key) are directly dependent on OrderID
.
Most relational databases aim for 3NF as a standard for good design. For many applications, 3NF provides an excellent balance between normalization benefits and performance. Higher normal forms (like BCNF, 4NF, 5NF) exist to address more complex dependencies but can sometimes lead to increased complexity in queries and potential performance overheads. The decision to normalize further depends on the specific requirements of your application.
To achieve 3NF, ensure your table is in 2NF and eliminate any non-key attribute that is dependent on another non-key attribute. This is typically done by splitting the table into new tables, with the dependent non-key attribute becoming the primary key of a new table and the original non-key attribute becoming a foreign key in the original table.
For more advanced normalization techniques, please refer to the documentation on Boyce-Codd Normal Form (BCNF).