Web Analytics Made Easy - Statcounter

Temporal Tables in SQL Server

Temporal Tables (also known as system-versioned tables) are a feature in SQL Server. They allow automatic tracking of historical changes to data over time. They maintain a history of all changes made to a table. This makes it easy to query past versions of data. You can also analyze trends.

This feature is particularly useful for scenarios like auditing, regulatory compliance, and data analysis.

How Temporal Tables Work

A temporal table consists of:

  1. Current Table: The main table storing the current version of the data.
  2. History Table: A system-maintained table storing the historical versions of the data with their validity periods.

SQL Server automatically manages the relationship between these tables and tracks the periods during which each row was valid.

Structure of Temporal Tables

  1. System Time Columns:
    • ValidFrom (SysStartTime): Timestamp when the row became valid.
    • ValidTo (SysEndTime): Timestamp when the row was no longer valid.
  2. Primary Table: Contains the current version of the data.
  3. History Table: Contains all previous versions of the data, managed automatically by SQL Server.

Creating a Temporal Table

  1. Step 1: Create a Temporal Table
CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName NVARCHAR(100),
    Price DECIMAL(10, 2),
    SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL,
    SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL,
    PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime)
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductsHistory));
  • PERIOD FOR SYSTEM_TIME: Defines the time range columns.
  • SYSTEM_VERSIONING = ON: Enables versioning.
  • HISTORY_TABLE: Specifies the table to store historical data.
  1. Step 2: Insert Data
INSERT INTO Products (ProductID, ProductName, Price) 
VALUES (1, 'Laptop', 1000.00);
  1. Step 3: Update Data
UPDATE Products 
SET Price = 1200.00 
WHERE ProductID = 1;

SQL Server automatically moves the previous version to the history table.

Querying Temporal Tables

  1. Current Data: Query the main table to see the latest data.
    • SELECT * FROM Products;
  2. Historical Data: Query the history table directly for past data.
    • SELECT * FROM ProductsHistory;
  3. Temporal Data Across Time: Use the FOR SYSTEM_TIME clause to query data valid during specific periods.
    • AS OF:
      • SELECT * FROM Products FOR SYSTEM_TIME AS OF '2024-01-01 10:00:00';
    • BETWEEN:
      • SELECT * FROM Products FOR SYSTEM_TIME FROM '2024-01-01 00:00:00' TO '2024-01-31 23:59:59';
    • ALL:
      • SELECT * FROM Products FOR SYSTEM_TIME ALL;

Benefits of Temporal Tables

  1. Historical Data Tracking:
    • Easily track how data has changed over time.
  2. Auditing and Compliance:
    • Fulfill regulatory requirements by retaining data change history.
  3. Data Recovery:
    • Recover accidentally deleted or updated data by querying past versions.
  4. Trend Analysis:
    • Analyze historical trends without requiring a custom audit mechanism.
  5. Reduced Complexity:
    • SQL Server automatically manages the history table, reducing development effort.

Considerations When Using Temporal Tables

  1. Storage:
    • Historical data can grow quickly, increasing storage requirements.
  2. Performance:
    • Insert, update, and delete operations may incur additional overhead due to history management.
  3. Retention Policies:
    • Implement strategies to archive or clean up historical data periodically.
  4. Schema Changes:
    • Modifying the schema of a temporal table requires special considerations for the history table.

Example Use Cases

  1. Audit Trail:
    • Track who changed data and when for regulatory compliance.
  2. Error Recovery:
    • Restore data accidentally deleted or updated.
  3. Historical Reporting:
    • Generate reports comparing current and historical data.
  4. Version Tracking:
    • Track versions of configuration or reference data.

Conclusion

Temporal tables in SQL Server are a powerful feature for maintaining a complete history of data changes. They automatically manage the storage and querying of historical data. This simplifies development and provides robust solutions for auditing. Additionally, it aids in compliance and data analysis. Proper planning of storage and maintenance is crucial to fully leverage their capabilities in production systems.


Discover more from Technology with Vivek Johari

Subscribe to get the latest posts sent to your email.

By vivekjohari

I am currently working as a Senior Database Professional and have around 18 years of experience in database. Degree:- Master Degree in Computer(MCA) Certification course in Data Science & Machine Learning from Indian Institute of Technology (IIT), Delhi Work experience:- Designing of the database. Database Optimization. Writing Complex Stored Procedures,Functions,Triggers etc. Designing and developing SSIS & DTS packages. Designing SQL Reports using SSRS. Database Server Maintenance. Certification:- MCTS: DA-100: Analysing Data with Microsoft Power BI MCTS: DP-300: Administering Relational Databases on Microsoft Azure Microsoft certified Sql DBA in Sql server 2008 (MCTS). Microsoft certified BI professional in Sql server 2008 (MCTS). Oracle certified profession DBA in ORACLE 10g (OCP) certified profession DBA in ORACLE 9i (OCP) My other publication Technical Blog:- Technologies with Vivek Johari Guest Author and Blogger at sqlservercentral.com

Leave a Reply

Discover more from Technology with Vivek Johari

Subscribe now to keep reading and get access to the full archive.

Continue reading