Introduction
Denormalization is used to make data easier and faster to read by adding some repeated information. It helps when you want quick answers without joining many tables.
There is no single SQL command for denormalization. It means you add extra columns or tables that repeat some data to avoid joins. Example: Adding a 'customer_name' column in an 'orders' table even if it exists in 'customers' table.
-- Normalized tables CREATE TABLE customers ( customer_id INT PRIMARY KEY, customer_name VARCHAR(100) ); CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, order_date DATE );
-- Denormalized table CREATE TABLE orders_denormalized ( order_id INT PRIMARY KEY, customer_id INT, customer_name VARCHAR(100), order_date DATE );
-- Create normalized tables CREATE TABLE customers ( customer_id INT PRIMARY KEY, customer_name VARCHAR(100) ); CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, order_date DATE ); -- Insert sample data INSERT INTO customers VALUES (1, 'Alice'), (2, 'Bob'); INSERT INTO orders VALUES (101, 1, '2024-01-01'), (102, 2, '2024-01-02'); -- Query with join (normalized) SELECT o.order_id, c.customer_name, o.order_date FROM orders o JOIN customers c ON o.customer_id = c.customer_id; -- Create denormalized table CREATE TABLE orders_denormalized ( order_id INT PRIMARY KEY, customer_id INT, customer_name VARCHAR(100), order_date DATE ); -- Insert denormalized data INSERT INTO orders_denormalized VALUES (101, 1, 'Alice', '2024-01-01'), (102, 2, 'Bob', '2024-01-02'); -- Query denormalized table SELECT order_id, customer_name, order_date FROM orders_denormalized;
Orders with columns OrderID, CustomerName, and CustomerAddress repeated in every row, what is a likely effect when updating a customer's address?UPDATE Orders SET CustomerPhone = '123-4567' WHERE CustomerID = 5;but some rows still show old phone numbers. What is the most likely problem?
Orders and Customers tables frequently. Which denormalization approach is best to improve read speed while managing data consistency?Pass the test and this pattern moves unknown