Temporal tables allow a database to store and query the history of data changes automatically. Unlike standard tables that only store the "current" state, temporal tables maintain an immutable log of every previous version of a record.
A temporal table consists of two physical tables:
The engine manages two hidden columns (e.g., SysStartTime and SysEndTime) to track the validity period of each row.
The power of temporal tables lies in the ability to "travel back in time" using the standard SQL:2011 syntax.
To see the state of the Products table as it existed on January 1st, 2024:
SELECT * FROM Products
FOR SYSTEM_TIME AS OF '2024-01-01 00:00:00';
The database engine automatically scans both the current and history tables to find rows where:
SysStartTime <= '2024-01-01' AND SysEndTime > '2024-01-01'.
To find all versions of a specific record between two dates:
SELECT * FROM Products
FOR SYSTEM_TIME BETWEEN '2023-01-01' AND '2023-12-31'
WHERE ProductID = 123;
Temporal tables provide a mathematically sound audit trail.
History table is system-managed, application code cannot modify historical records. This is critical for financial compliance (SOX/GDPR).AS OF syntax allows for surgical recovery of the specific records without restoring the entire database from a backup.UPDATE to the current table triggers an INSERT into the history table.(SysEndTime, SysStartTime) to optimize for point-in-time lookups.