Denormalization: when to copy a value and when it's a bug

Denormalization stores a value twice so reads skip a join. When a copy is the right design, when it is a bug waiting, and how SQL Server keeps a total in step.

What is denormalization?

Denormalization is adding redundant data to a normalized database on purpose: a value stored a second time in another table, or a result stored instead of computed, to improve read performance: reads skip a join or a calculation. It is a step taken after database normalization, not a schema that was never normalized. The price is paid on writes: every copy has to change when its source changes, and a copy that misses a change is an update anomaly, two rows that disagree about one fact.

A small shop shows both sides. The online shopping system template has the full database; four of its tables are enough here:

CREATE TABLE dbo.Customer (
  CustomerId int           NOT NULL IDENTITY
    CONSTRAINT PK_Customer PRIMARY KEY,
  Email      nvarchar(254) NOT NULL
    CONSTRAINT AK_Customer_Email UNIQUE,
  FullName   nvarchar(200) NOT NULL,
  Line1      nvarchar(200) NOT NULL,
  City       nvarchar(100) NOT NULL,
  PostalCode varchar(20)   NOT NULL
);
CREATE TABLE dbo.Product (
  ProductId int           NOT NULL IDENTITY
    CONSTRAINT PK_Product PRIMARY KEY,
  Sku       varchar(20)   NOT NULL
    CONSTRAINT AK_Product_Sku UNIQUE,
  Name      nvarchar(200) NOT NULL,
  Price     decimal(10,2) NOT NULL
);
CREATE TABLE dbo.CustomerOrder (
  CustomerOrderId int           NOT NULL IDENTITY
    CONSTRAINT PK_CustomerOrder PRIMARY KEY,
  CustomerId      int           NOT NULL
    CONSTRAINT FK_CustomerOrder_Customer_CustomerId
    REFERENCES dbo.Customer (CustomerId),
  PlacedAt        datetime2(0)  NOT NULL,
  ShipLine1       nvarchar(200) NOT NULL,
  ShipCity        nvarchar(100) NOT NULL,
  ShipPostalCode  varchar(20)   NOT NULL
);
CREATE TABLE dbo.OrderItem (
  CustomerOrderId int           NOT NULL,
  ProductId       int           NOT NULL,
  Quantity        smallint      NOT NULL,
  UnitPrice       decimal(10,2) NOT NULL,
  CONSTRAINT PK_OrderItem
    PRIMARY KEY (CustomerOrderId, ProductId),
  CONSTRAINT FK_OrderItem_CustomerOrder_CustomerOrderId
    FOREIGN KEY (CustomerOrderId)
    REFERENCES dbo.CustomerOrder (CustomerOrderId),
  CONSTRAINT FK_OrderItem_Product_ProductId
    FOREIGN KEY (ProductId)
    REFERENCES dbo.Product (ProductId)
);
dbo.Customer, dbo.Product, dbo.CustomerOrder and dbo.OrderItem; dashed line from Customer to CustomerOrder, solid lines from CustomerOrder and from Product to OrderItem, which has rounded corners.
dbo.OrderItem's key is the order and the product, so both its lines are solid. Nothing in the picture says UnitPrice or the Ship columns were copied from anywhere.

Two columns already repeat a value from another table. OrderItem.UnitPrice is filled from Product.Price, and ShipLine1, ShipCity and ShipPostalCode from the customer’s address. Neither is a mistake.

When copying a value is the right design

Ana orders two mugs at 8.50. A week later the mug costs 9.75 and Ana moves house:

INSERT INTO dbo.Customer
  (Email, FullName, Line1, City, PostalCode)
VALUES (N'ana@example.com', N'Ana Lopez', N'12 Elm St',
        N'Portland', '97201');
INSERT INTO dbo.Product (Sku, Name, Price)
VALUES ('KTL-01', N'Kettle', 39.00), ('MUG-02', N'Mug', 8.50);
INSERT INTO dbo.CustomerOrder
  (CustomerId, PlacedAt, ShipLine1, ShipCity, ShipPostalCode)
SELECT CustomerId, '2026-03-02 10:15', Line1, City, PostalCode
FROM dbo.Customer WHERE CustomerId = 1;
INSERT INTO dbo.OrderItem
  (CustomerOrderId, ProductId, Quantity, UnitPrice)
SELECT 1, ProductId, 2, Price
FROM dbo.Product WHERE Sku = 'MUG-02';

UPDATE dbo.Product SET Price = 9.75 WHERE Sku = 'MUG-02';
UPDATE dbo.Customer
SET Line1 = N'40 Oak Ave', PostalCode = '97205'
WHERE CustomerId = 1;

SELECT o.CustomerOrderId, o.ShipLine1,
       SUM(i.Quantity * i.UnitPrice) AS OrderTotal
FROM dbo.CustomerOrder AS o
JOIN dbo.OrderItem AS i ON i.CustomerOrderId = o.CustomerOrderId
GROUP BY o.CustomerOrderId, o.ShipLine1;

The query returns order 1 shipping to 12 Elm St with a total of 17.00, which is what happened. Had the order joined dbo.Product for its price and dbo.Customer for its address, it would now claim Ana paid 19.50 for a parcel sent to a street she didn’t live on yet.

So these copies aren’t redundant at all. UnitPrice holds the price on the day of the order, a fact of its own; Product.Price holds today’s price. They start equal and are meant to drift apart. The same goes for the ship-to address, a tax rate, an exchange rate or a product name printed on an invoice.

The test for any copied column is one question: when the source changes, should the copy change too? If not, the copy is a snapshot, the correct design, and nothing has to keep it in step. If it should, it is real redundancy, and something has to own the update.

When a copy is a bug waiting

Say the order list and the shipping email want the customer’s email without a join, so the order gets its own copy:

ALTER TABLE dbo.CustomerOrder
  ADD CustomerEmail nvarchar(254) NULL;
dbo.Customer, dbo.Product, dbo.CustomerOrder and dbo.OrderItem as before; dbo.CustomerOrder has a new column CustomerEmail with no line of its own to dbo.Customer.
CustomerEmail is a plain column: the only line to dbo.Customer comes from CustomerId, so nothing ties it to Customer.Email.

The copy is right on the day it is written. Then Ana changes her email:

UPDATE o SET CustomerEmail = c.Email
FROM dbo.CustomerOrder AS o
JOIN dbo.Customer AS c ON c.CustomerId = o.CustomerId;

UPDATE dbo.Customer SET Email = N'ana.lopez@example.com'
WHERE CustomerId = 1;

SELECT o.CustomerOrderId, o.CustomerEmail, c.Email
FROM dbo.CustomerOrder AS o
JOIN dbo.Customer AS c ON c.CustomerId = o.CustomerId;

Both updates succeed, and the query shows order 1 holding ana@example.com while dbo.Customer holds ana.lopez@example.com. The shipping confirmation goes to an address Ana gave up. Here the answer to the test is yes: an order should reach the customer where they are now, so the copy should have followed and didn’t. Nothing in the schema says it must, and every place that changes an email has to remember the orders.

The usual fix is to drop the column. CustomerId is already there, and the join finds each customer through the primary key’s index. A copy like this one earns its place only when a measured query is too slow with the join, and then it needs an owner.

Let SQL Server keep the copy in step

A foreign key can own it. Point the copy and the customer’s key together at a unique constraint on the same pair in dbo.Customer, and declare the foreign key ON UPDATE CASCADE:

ALTER TABLE dbo.Customer
  ADD CONSTRAINT AK_Customer_CustomerId_Email
  UNIQUE (CustomerId, Email);
ALTER TABLE dbo.CustomerOrder
  ALTER COLUMN CustomerEmail nvarchar(254) NOT NULL;
ALTER TABLE dbo.CustomerOrder
  DROP CONSTRAINT FK_CustomerOrder_Customer_CustomerId;
ALTER TABLE dbo.CustomerOrder
  ADD CONSTRAINT
    FK_CustomerOrder_Customer_CustomerId_CustomerEmail
  FOREIGN KEY (CustomerId, CustomerEmail)
  REFERENCES dbo.Customer (CustomerId, Email)
  ON UPDATE CASCADE;
dbo.Customer with alternate key Email; dbo.CustomerOrder with foreign key columns CustomerId and CustomerEmail.Email on one dashed line to dbo.Customer; dbo.OrderItem and dbo.Product as before.
CustomerEmail.Email is the CustomerEmail column holding Customer.Email: the copy is now part of the relationship. The cascade on update isn't drawn.

Copy the current emails over first, with the first UPDATE above. The stale order 1 would otherwise stop the new foreign key with Msg 547: SQL Server checks the existing rows when the constraint is added, and finds the drift. The column has to be NOT NULL too, since SQL Server checks none of a composite foreign key’s columns when one of them is NULL; the composite key post shows that run.

UPDATE dbo.Customer SET Email = N'ana@lopez.example'
WHERE CustomerId = 1;

INSERT INTO dbo.CustomerOrder (CustomerId, CustomerEmail,
  PlacedAt, ShipLine1, ShipCity, ShipPostalCode)
VALUES (1, N'ana@example.com', '2026-04-10 09:00',
        N'40 Oak Ave', N'Portland', '97205');
Msg 547, Level 16, State 1, Line 4
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CustomerOrder_Customer_CustomerId_CustomerEmail". The conflict occurred in database "Shop", table "dbo.Customer".
The statement has been terminated.

The first UPDATE changes Ana’s email once more, and SQL Server rewrites order 1 in the same statement. The INSERT of an order with her old email is refused. The copy can no longer disagree with its source, in either direction. The cost moved to the write: changing an email now rewrites every order that customer ever placed.

Keeping a stored order total in step

The other common copy is a result: the order’s total, stored so a list of orders doesn’t sum the lines of each one.

ALTER TABLE dbo.CustomerOrder
  ADD OrderTotal decimal(12,2) NOT NULL
    CONSTRAINT DF_CustomerOrder_OrderTotal DEFAULT (0);
dbo.Customer, dbo.Product, dbo.CustomerOrder with a new column OrderTotal, and dbo.OrderItem; a dashed line from Customer to CustomerOrder, solid lines from CustomerOrder and from Product to OrderItem, which has rounded corners.
OrderTotal is an ordinary column with a default of 0, which the picture doesn't show. Nothing on the diagram says its value comes from dbo.OrderItem.

A computed column can’t hold it. Its expression reads only its own row: AS (SELECT SUM(...) FROM dbo.OrderItem ...) fails with Msg 1046, “Subqueries are not allowed in this context”, and quoting [Quantity] from the order table fails with Msg 207. A computed column does fit a value inside one row, such as a line total Quantity * UnitPrice on dbo.OrderItem.

So the stored total needs a trigger on the lines, which recomputes every order a statement touched:

CREATE TRIGGER dbo.TR_OrderItem_OrderTotal
ON dbo.OrderItem
AFTER INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON;
UPDATE o
SET OrderTotal = ISNULL((
      SELECT SUM(i.Quantity * i.UnitPrice)
      FROM dbo.OrderItem AS i
      WHERE i.CustomerOrderId = o.CustomerOrderId), 0)
FROM dbo.CustomerOrder AS o
WHERE o.CustomerOrderId IN (
      SELECT CustomerOrderId FROM inserted
      UNION
      SELECT CustomerOrderId FROM deleted);
INSERT INTO dbo.OrderItem
  (CustomerOrderId, ProductId, Quantity, UnitPrice)
SELECT 1, ProductId, 1, Price
FROM dbo.Product WHERE Sku = 'KTL-01';
UPDATE dbo.OrderItem SET Quantity = 3
WHERE CustomerOrderId = 1 AND ProductId = 2;
DELETE FROM dbo.OrderItem
WHERE CustomerOrderId = 1 AND ProductId = 1;

SELECT CustomerOrderId, OrderTotal FROM dbo.CustomerOrder;

After the insert, order 1’s total is 56.00; after the quantity change, 64.50; after the delete, 25.50, three mugs at 8.50. Two gaps remain. Orders that existed before the trigger keep the default 0 until one of their lines changes, so they need the same UPDATE run once. And the trigger watches dbo.OrderItem only: an UPDATE dbo.CustomerOrder SET OrderTotal = 1 goes straight through, and the run stored 1.00.

An indexed view closes both gaps. SQL Server stores its rows and maintains them on every write to dbo.OrderItem, and a direct UPDATE of the view fails with Msg 4406. The order list joins it on its clustered index:

CREATE VIEW dbo.OrderTotal
WITH SCHEMABINDING
AS
SELECT CustomerOrderId,
       SUM(Quantity * UnitPrice) AS OrderTotal,
       COUNT_BIG(*) AS LineCount
FROM dbo.OrderItem
GROUP BY CustomerOrderId;
GO
CREATE UNIQUE CLUSTERED INDEX IX_OrderTotal
  ON dbo.OrderTotal (CustomerOrderId);
GO
SELECT o.CustomerOrderId, o.OrderTotal AS StoredTotal,
       t.OrderTotal
FROM dbo.CustomerOrder AS o
JOIN dbo.OrderTotal AS t WITH (NOEXPAND)
  ON t.CustomerOrderId = o.CustomerOrderId;

The view needs SCHEMABINDING, and a GROUP BY view needs COUNT_BIG(*) in its select list. The query returns order 1 with the stray 1.00 in the stored column and 25.50 from the view. WITH (NOEXPAND) tells SQL Server to read the view’s stored rows; outside Enterprise edition, a query without it expands the view and sums dbo.OrderItem all over again. With the view in place, the column and its trigger can go.

When should you denormalize?

  • A value that records a moment, the price paid or the address shipped to, is a snapshot. Store it on the order and never update it from its source.
  • A value that should follow its source is redundancy. Start with the join and an index; copy only when a measured query is still too slow.
  • Every real copy gets one owner the database runs: a cascading foreign key for a copied column, an indexed view for a sum or a count, a trigger when neither fits. Application code alone misses the next script someone runs by hand.
  • A reporting database is the exception: a star schema repeats values on purpose, and only its load writes to it, as dimensional data modeling shows.

How ERDsmith helps

The email that went stale and the total that took a stray write share a cause: a column whose source the schema didn’t name. On a diagram, a column tied to its source by a relationship looks different from one that only happens to hold the same value:

  • A relationship draws a line, and a copy carried by a foreign key shows its parent column on the box, as CustomerEmail.Email does. A copied column without one stands alone, with no line.
  • A migrated column has its parent column’s type, length, precision, scale and collation, so a copied email can’t be shorter than the one it copies.
  • A computed column’s expression that names a column its table doesn’t have is flagged, the Msg 207 case above.