Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
Database Tools
SQL Scripts & Samples
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
News Via RSS Feed

follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted July 7, 2016

Introduction to Azure SQL Database Temporal Tables

By Marcin Policht

As announced on June 1, 2016, SQL Server 2016 has reached its general availability. This means that you finally have the option to implement some of its new features in the production environment. While we still have to wait for their full support in Azure SQL Database V12, it is likely that their preview stage will be concluded soon. In this article, we will focus on Temporal tables, which is one example of these new features.

The primary purpose of Temporal tables is to provide the full visibility of historical changes of content stored in Azure SQL Database tables. Effectively, you have the ability to identify the state of your data at an arbitrary point in time. This comes in handy in a variety of scenarios, including performing analysis of usage trends of database-bound applications, identifying data lineage, or carrying compliance and regulatory audits. In addition, you have a rather unique ability of granular recovery of individual table entries, without resorting to traditional restore methods. All of these benefits can be realized without the need for programming or database design changes. Existing applications interact with their data sources in the same manner as they would without having temporal tables in place.

The new functionality leverages a couple of extra columns that must be included in the original table to determine the timeline of changes to its content, which subsequently are captured in the associated table dedicated to storing historical records. The two columns are of the datetime2 data type and represent, respectively, the beginning and end (designated in the table schema by the GENERATED ALWAYS AS ROW START, GENERATED ALWAYS AS ROW END, and PERIOD FOR SYSTEM_TIME options) of the period during which a particular row had a specific set of values. In addition, the table must have the SYSTEM_VERSIONING option enabled (set to ON). There are several additional requirements that must be satisfied in order for a table to support this functionality, including, for example the presence of the primary key or absence of INSTEAD OF triggers (for a comprehensive overview of Temporal Tables considerations and limitations, refer to the MSDN Library).

As you would expect, the history table does not support direct modifications. In addition, once the SYSTEM_VERSIONING option is enabled, you will not be able to modify the schema of the temporal table (to modify it, you would have to temporarily disable the system versioning). You can, however, manage independently the indexing of the history table (including dropping and creating new indexes). Considering that its size is likely to grow far beyond the size of the original table, you might consider taking advantage of the clustered columnstore indexes to maximize performance of index-based operations while keeping the index size in check. Note, however, that support for columnstore indexing requires the Premium service tier (temporal tables are available in all tiers of Azure SQL Database).

You can configure a new table as temporal during its creation (as part of the CREATE TABLE T-SQL statement or afterwards (by using the ALTER TABLE T-SQL statement. You also have the option of using the default history table generated automatically when enabling system versionign or point to an existing one as long as it satisfies all requirements outlined in Temporal Tables considerations and limitations MSDN Library article). All subsequent changes are automatically recorded in the history table.

To create a new temporal table, you would use a T-SQL statement containing the following code (which you can apply to the target Azure SQL database using any of traditional SQL Server management tools, including SQL Server Management Studio, SQL Server Data Tools, or even sqlcmd):

CREATE TABLE dbo.<temporal_table_name>
  , [ValidFrom] datetime2 (0) GENERATED ALWAYS AS ROW START
  , [ValidTo] datetime2 (0) GENERATED ALWAYS AS ROW END
  , PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
 WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.<history_table_name>));

Similarly, to modify an existing table by enabling system versioning functionality, you would run (note that we assign defaults to both datetime2 columns) the ALTER TABLE statement resembling the following:

ALTER TABLE dbo.<existing_table_name>
    [ValidFrom] datetime2 (0) GENERATED ALWAYS AS ROW START
    , [ValidTo] datetime2 (0) GENERATED ALWAYS AS ROW END HIDDEN   
        CONSTRAINT DF_ValidTo DEFAULT '9999.12.31 23:59:59.99'
    , PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo); 

ALTER TABLE WebsiteUserInfo  
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.<history_table_name>));

This concludes our introduction to the Temporal tables functionality in Azure SQL Database. In our upcoming articles, we will provide examples of querying their content by using new T-SQL constructs available in Azure SQL Database V12.

See all articles by Marcin Policht

MS SQL Archives

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM