In this article, we will be discussing about Temporal Tables in SQL Server and Azure SQL Database.
Temporal tables in SQL Server 2016 and later as well as in Azure SQL Database is a new type of user table which was was introduced in ISO/ANSI SQL 2011. Read on to learn more temporal tables and see a comprehensive example that uses them.
What are Temporal Tables in SQL Server and Azure SQL Database?
Temporal tables keep a full history of data changes (based on UTC time) by using a pair of current-historical tables. These tables has the same structure.
The period of validity for every row is managed by the system (i.e. Database Engine) by using two explicitly-defined columns of the DateTime2 datatype.
Example with Temporal Tables (T-SQL)
Below you can find an example of a user table’s definition that has system versioning enabled:
--Create table along with specifying its historical table CREATE TABLE dbo.Students2 ( studentID INT NOT NULL IDENTITY(1,1) CONSTRAINT PK_Students PRIMARY KEY, studentName VARCHAR(150) NOT NULL, studentAddress VARCHAR(150) NOT NULL, regDate DATE NOT NULL, SysStartTime DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL, SysEndTime DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL, PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime) ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.Students2History)); GO
The above DDL will create two tables: (i) Students2, and (ii) Students2History.
In the “dbo.Students2” table, the user needs to populate only the first 4 columns (studentID, studentName, studentAddress and regDate). Note: In this case “studentID” will be populated automatically too because it is an identity column. The rest of the columns will be maintained automatically by SQL Server’s Database Engine. Table “dbo.Students2History” will be populated fully automatically by the Database Engine.
In the below screenshot we can see both tables:
Learn essential SQL Server development tips! Enroll to our Online Course!
Check our online course titled “Essential SQL Server Development Tips for SQL Developers”
(special limited-time discount included in link).Sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
Let’s check both tables for any contents:
Now, let’s insert some data in the “dbo.Students2” table:
--Insert initial data INSERT INTO dbo.Students2 ( studentName , studentAddress , regDate ) VALUES ( 'John Adams', 'Address 1', GETDATE() ); GO
Let’s check again both tables for any contents:
As you can see, the “dbo.Students2” table was populated with data.
The “dbo.Students2History” table was not populated with any data because system versioning works only in case of an update operation against one or more rows.
That being said, let’s try to update the row:
--Now let's update a record UPDATE dbo.Students2 SET studentAddress='Address 1 Update' WHERE studentID=1; GO
Let’s check again the table contents:
Now you can see that the table “Students2History” contains the original values for row with studentID=1 just before the update operation took place.
Let’s update the same record again:
--Update the same record UPDATE dbo.Students2 SET studentAddress='Address 1 Update 2' WHERE studentID=1; GO
Let’s check the table contents:
As you can see, the table “Students2History” contains also the “new” original value for row with studentID=1 just before the new update operation took place. So in total, the historical table contains the rows for studentID=1 just before the two update operations took place. The validity of each row can be determined from the “SysStartTime” and “SysEndTime” values.
Conclusion
- Auditing all data changes (i.e. for data forensics)
- Data reconstruction/recovery
- Trends monitoring over time
Featured Online Courses:
- AI Essentials: A Beginner’s Guide to Artificial Intelligence
- SQL Server 2022: What’s New – New and Enhanced Features
- Working with Python on Windows and SQL Server Databases
- Introduction to Azure Database for MySQL
- Boost SQL Server Database Performance with In-Memory OLTP
- Introduction to Azure SQL Database for Beginners
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- Data Management for Beginners – Main Principles
- A Guide on How to Start and Monetize a Successful Blog
Learn More:
- MS Docs Article: What’s New in SQL Server 2016, November Update
- MS Docs Article on Temporal Tables
Check Also:
- SQL Server 2016 Top Features
- The set identity_insert Command in SQL Server
- The Import Flat File Wizard in SSMS v17.3
- Listing all Tables of a Linked Server’s Database
- How to Import and Export Unstructured Data in SQL Server – FileTables
- How to Import and Export Unstructured Data in SQL Server – FILESTREAM
- How to Import and Export Unstructured Data in SQL Server – The IMAGE Datatype
- …more
Did you find this article useful and interesting? Feel free to leave your comment!
If you enjoy my SQL Server administration tips and articles, I have something special just for you. It is one of my eBooksand it is called “Administering SQL Server“. Check it out!
Subscribe to our newsletter and stay up to date with our latest articles on SQL Server and related technologies!
Check out our latest software releases! All our software tools have free trial versions to download.
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou, a distinguished Senior Database and Software Architect, brings over 20 years of expertise to the IT industry. A Certified Database, Cloud, and AI professional, he earned the Microsoft Data Platform MVP title for nine consecutive years (2009-2018). As the founder of SQLNetHub and GnoelixiAI Hub, Artemakis is dedicated to sharing his knowledge and democratizing education on various fields such as: Databases, Cloud, AI, and Software Development. His commitment to simplicity and knowledge sharing defines his impactful presence in the tech community.