Regularization
Normalization
Eliminating data redundancy and improving integrity. 1NF to 5NF.
Normalization
Eliminating data redundancy and improving integrity. 1NF to 5NF.
Normalization is the process of splitting tables in database design to minimize redundancy and ensure data integrity. A core concept of relational database theory proposed by E.F. Codd in the 1970s, it prevents insertion/update/deletion anomalies.
First Normal Form (1NF) requires that each column hold only atomic values. Second Normal Form (2NF) removes partial functional dependencies so that every non-key attribute depends on the entire primary key. Third Normal Form (3NF) removes transitive functional dependencies, so a non-key attribute must not depend on another non-key attribute.
BCNF (Boyce-Codd Normal Form) is a stronger version of 3NF in which every determinant must be a candidate key. 4NF and 5NF deal with multivalued dependencies and join dependencies, and in practice, normalization is usually applied only up to 3NF or BCNF.
Normalization improves data integrity, but it can degrade query performance because it increases the number of joins. When read performance is important, denormalization is applied selectively to strike a balance.
-- 정규화 전: 모든 데이터가 한 테이블에 (중복 발생)
CREATE TABLE orders_denorm (
order_id INT,
customer_name VARCHAR(100),
customer_email VARCHAR(100),
customer_address VARCHAR(200), -- 고객 주소 중복
product_name VARCHAR(100),
product_price DECIMAL(10,2) -- 상품 가격 중복
);
-- 정규화 후: 테이블 분리
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
address VARCHAR(200)
);
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10,2)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT REFERENCES customers(id),
order_date DATE
);
CREATE TABLE order_items (
id INT PRIMARY KEY,
order_id INT REFERENCES orders(id),
product_id INT REFERENCES products(id),
quantity INT
);
-- 조회 시 JOIN 사용
SELECT c.name, o.order_date, p.name, oi.quantity
FROM customers c
JOIN orders o ON c.id = o.customer_id
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id;
Senior: Customer information is being stored redundantly for every order in this table. We need normalization.
Junior: Won't joins make the queries more complicated?
Senior: Yes, but if a customer's address changes, we have to update every order. With normalization, we only have to update the customers table.
Interviewer: Please explain the advantages and disadvantages of normalization.
Candidate: The advantages are reduced data redundancy, storage savings, and prevention of update anomalies. The disadvantages are lower query performance due to more joins and increased query complexity. In read-heavy systems, we strike a balance with selective denormalization.
Interviewer: What is the difference between 3NF and BCNF?
Candidate: In 3NF, non-key attributes must depend only on the key, while in BCNF every determinant must be a candidate key. If there is a determinant that is not a candidate key, the relation satisfies 3NF but violates BCNF.
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: You stored the product price directly in the orders table. Isn't that a normalization violation?
Author: That was intentional. I wanted to preserve the price at the time of the order, even if the product price changes later.
Reviewer: Good call. In a case like this, it's intentional denormalization driven by a business requirement. Please leave a comment explaining the reason.