SQL Server 2016 – Temporal Tables Part 2

Hello Friends,

In my previous blog post, we have seen two different ways for the creation of a temporal history table. First was the default, where SQL Server will create the table and assign a default name to that table. While the second was specify the name of that table and SQL Server will create the history table with that name. In both these cases structure of the history table will be same and history table will have one clustered row store index. In today’s world where we have the support for column store indexes, because of the workload pattern, you can also create clustered column store index.

Yes, you read it correctly. You can create a temporal history table with a clustered column store index. Afterwards you can specify this table as the temporal history table while creating temporal table. Let me show you how to do the same:

-------------------- Step 1: Create a temporal History table ------------------
USE [TEMPORAL]
GO
CREATE TABLE dbo.TemporalTableDemo3_History
(
	EmpID BIGINT NOT NULL,
	FName VARCHAR(100) NOT NULL,
	MName VARCHAR(100) NOT NULL,
	LName VARCHAR(100) NOT NULL,
	DeptID INT NOT NULL,
	LifeStart DATETIME2, 
    LifeEnd DATETIME2
)
GO
---------------- Step 2: Create a clustered column store index -----------------
CREATE CLUSTERED COLUMNSTORE INDEX CCIX_TemporalTableDemo3_History
ON dbo.TemporalTableDemo3_History;
GO
---------- Step 3: Create a temporal table and specified history table ---------
USE [TEMPORAL]
GO
CREATE TABLE TemporalTableDemo3
(
	EmpID BIGINT NOT NULL PRIMARY KEY, --One Primary Key is mandatory
	FName VARCHAR(100) NOT NULL,
	MName VARCHAR(100) NOT NULL,
	LName VARCHAR(100) NOT NULL,
	DeptID INT NOT NULL,
	-- two columns with datatime2 data type specified as GENERATED ALWAYS AS ROW START/END
	LifeStart DATETIME2 GENERATED ALWAYS AS ROW START, 
    LifeEnd DATETIME2 GENERATED ALWAYS AS ROW END,
    PERIOD FOR SYSTEM_TIME (LifeStart, LifeEnd)
)
WITH (SYSTEM_VERSIONING=ON (HISTORY_TABLE = dbo.TemporalTableDemo3_History) )
GO

After executing the above TSQL code, I got the below mentioned error, because I have specified LifeStart and LifeEnd both the columns as NULL. These columns will be used internally by SQL Server to store the valid period information.

Msg 13530, Level 16, State 1, Line 22

Setting SYSTEM_VERSIONING to ON failed because system column ‘LifeStart’ in history table ‘TEMPORAL.dbo.TemporalTableDemo3_History’ corresponds to a period column in table ‘TEMPORAL.dbo.TemporalTableDemo3’ and cannot be nullable.

Let me try to modify the temporal history table and then I’ll create the temporal table again:

   
------ Step 4: Alter the temporal history table period columns to NOT NULL -----
ALTER TABLE dbo.TemporalTableDemo3_History
	ALTER COLUMN LifeStart DATETIME2 NOT NULL;
GO
ALTER TABLE dbo.TemporalTableDemo3_History
	ALTER COLUMN LifeEnd DATETIME2 NOT NULL;
GO

---------- Step 5: Create a temporal table and specified history table ---------
CREATE TABLE TemporalTableDemo3
(
	EmpID BIGINT NOT NULL PRIMARY KEY, --One Primary Key is mandatory
	FName VARCHAR(100) NOT NULL,
	MName VARCHAR(100) NOT NULL,
	LName VARCHAR(100) NOT NULL,
	DeptID INT NOT NULL,
	-- two columns with datatime2 data type specified as GENERATED ALWAYS AS ROW START/END
	LifeStart DATETIME2 GENERATED ALWAYS AS ROW START, 
    LifeEnd DATETIME2 GENERATED ALWAYS AS ROW END,
    PERIOD FOR SYSTEM_TIME (LifeStart, LifeEnd)
)
WITH (SYSTEM_VERSIONING=ON (HISTORY_TABLE = dbo.TemporalTableDemo3_History) )
GO

All ran fine. We can check the details in Object Explorer:

SQL Server 2016 - Temporal Tables Part 2 -1

In SSMS 2016, Temporal history table is shown under the temporal table, but If you will see the above information on SQL Server 2012 management studio the it will look like two different tables and clustered column store index will be shown as Non Clustered Index:

SQL Server 2016 - Temporal Tables Part 2 -2

In the next blog post we will look into some more details about temporal tables.

HAPPY LEARNING!

Regards:

Prince Kumar Rastogi

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook

Follow Prince Rastogi on Twitter | Follow Prince Rastogi on FaceBook

   

About Prince Rastogi

Prince Rastogi is working as Database Administrator at Elephant Insurance, Richmond. He is having more than 8 years of experience and worked in ERP Domain, Wealth Management Domain. Currently he is working in Insurance domain. In the starting of his career he was working on SQL Server, Internet Information Server and Visual Source Safe. He is post graduate in Computer Science. Prince is ITIL certified professional. Prince likes to explore technical things for Database World and Writing Blogs. He is Technical Editor and Blogger at SQLServerGeeks.com. He is a regular speaker at DataPlatformDay events in Delhi NCR. He has also presented some in depth sessions about SQL Server in SQL Server Conferences in Bangalore.

View all posts by Prince Rastogi →

Leave a Reply

Your email address will not be published.