Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 March SQL Server 2016: System-versioned Temporal Table to store History of data change

SQL Server 2016: System-versioned Temporal Table to store History of data change

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

SQL Server 2016 introduced a new type of user table that is called System-versioned Temporal Table to keep a full history of data changes.

This is a very good addition to the SQL Server 2016 because to track data change history is always an important task for a big transactional system.

SQL Server already provides few features to track data change like Change Data Capture (CDC), SQL Server Auditing, can create a manual DDL Trigger.

Now, SQL Server 2016 System-versioned temporal table which is something good and very easy to implement on a specific table.
You can audit all data changes and can perform require analysis on it. In Case of invalid delete or update, you can recover your data.

Rules and Requirements:

  • Can create your table using SYSTEM_VERSIONING=ON
  • Define your history table name HISTORY_TABLE = TableName, which tracks data change history. (It creates automatically).
  • Must define two additional columns [ValidFrom] and [ValidTo] with default ROW START and ROW END and must refer to as SYSTEM_TIME period columns.
  • Must have a Primary key column.
  • Same number of column and structure between both the tables.

Full demonstration:

Create a sampel System Version Table:

1
2
3
4
5
6
7
8
9
10
CREATE TABLE tbl_Students
(
StudID INT PRIMARY KEY
,StudName VARCHAR(50)
,StudClass CHAR(1)
,ValidFrom DATETIME2 (2) GENERATED ALWAYS AS ROW START
,ValidTo DATETIME2 (2) GENERATED ALWAYS AS ROW END
,PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.tbl_StudentsHistory));
GO

Insert few sample records:

1
2
3
4
5
INSERT INTO tbl_Students
(StudID,StudName,StudClass)
VALUES (1,'Roy','A'),(2,'Bony','B'),(3,'Mahi','C')
,(4,'Maria','A'),(5,'Ron','B'),(6,'Murey','C')
GO

Execute few DML statements:

1
2
3
4
5
6
UPDATE tbl_Students SET StudClass = 'B'
WHERE StudID = 4;
GO
 
DELETE FROM tbl_Students WHERE StudID = 2
GO

Check the result of both tables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SELECT *FROM tbl_StudentsHistory
 
StudID StudName StudClass ValidFrom ValidTo
------- ------------- --------- --------------------------- ---------------------------
4 Maria A 2017-01-06 05:47:23.73 2017-01-06 05:48:54.43
2 Bony B 2017-01-06 05:47:23.73 2017-01-06 05:50:12.52
 
 
SELECT *FROM tbl_Students
 
StudID StudName StudClass ValidFrom ValidTo
------- --------------- --------- --------------------------- ---------------------------
1 Roy A 2017-01-06 05:47:23.73 9999-12-31 23:59:59.99
3 Mahi C 2017-01-06 05:47:23.73 9999-12-31 23:59:59.99
4 Maria B 2017-01-06 05:48:54.43 9999-12-31 23:59:59.99
5 Ron B 2017-01-06 05:47:23.73 9999-12-31 23:59:59.99
6 Murey C 2017-01-06 05:47:23.73 9999-12-31 23:59:59.99
Mar 5, 2018Anvesh Patel

Please visit other related articles...

  • SQL Server 2016: Enable System-versioned Temporal Table on existing Table
  • SQL Server 2016: Drop table operation failed on table because it is not supported operation on system-versioned temporal tables
  • SQL Server 2016: Introduced Live Query Statistics, Monitor Live Query Execution
  • SQL Server 2016: sys.dm_db_column_store_row_group_physical_stats to find fragmentation of Columnstore Indexes
  • SQL Server 2016: sys.dm_column_store_object_pool to find memory pool usage by Columnstore Indexes
  • SQL Server 2016: Columnstore Index on Memory Optimized In Memory OLTP Tables
  • SQL Server 2016: Script to get information of Cached Functions (Total Execution Time)
  • SQL Server 2014: Introduced DBCC CLONEDATABASE TO generate only Schema and Statistics
SQL Server: Allow Multiple NULL Values in UNIQUE ConstraintSQL Server 2016: Drop table operation failed on table because it is not supported operation on system-versioned temporal tables
Anvesh Patel

Database Engineer

ImageMarch 5, 2018 SQL ServerAnvesh Patel, Change data capture, Data change audit, data history, database, database research and development, dbrnd, DDL Changes, history table, SQL Query, SQL Server, SQL Server 2016, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, System-versioned, Temporal Table, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... }
  • Anvesh Patel { Great... }
  • Anvesh Patel { Great... }
  • Anvesh Patel { Great... }
  • Anvesh Patel { Great... }
  • Anvesh Patel { Great... }
  • Anvesh Patel { Great... }
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....