9.6. History Tables

The start and stop times are what you should have been catching in the first place and not the computed hours. Think raw data and single facts when designing a table. Let me use a history table for price changes. The fact to store is that a price had duration:

CREATE TABLE PriceHistory
(sku CHAR(13) NOT NULL
  REFERENCES Inventory (sku),
start_date DATE NOT NULL,
end_date DATE, -- null means current
CHECK(start date < end_date),
PRIMARY KEY (sku, start_date),
item_price DECIMAL (12,4) NOT NULL

 CHECK (item_price > 0.0000),
etc.);

You actually need more checks to assure that the start date is at 00:00 Hrs if you cannot work with whole days in your SQL engine. This is the case with MS SQL Server and the Sybase family. Likewise, ...

Get Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.