Introduction to SQL 2016 Temporal Tables Vicky Harp
About Me Working with SQL Server since 2001 Working at Idera since 2004 Twitter: @vickyharp Email: vicky.harp@idera.com
Thank You Sponsors! Visit the Sponsor tables to enter their end of day raffles. Turn in your completed Event Evaluation form at the end of the day in the Registration area to be entered in additional drawings. Want more free training? Check out the Houston Area SQL Server User Group which meets on the 2nd Tuesday of each month. Details at http://houston.sqlpass.org
Shopping Cart Contents Data Changes Over Time Postal Codes Physical Addresses Email Addresses Shopping Cart Contents
Data Changes For Different Reasons Customer Address Customer Moved Street Name Changed Typo When Entering Data Data Corruption
Temporal Databases Defined in SQL:2011 Specification Available from multiple RDBMS vendors SQL Server: System Versioned Temporal Tables
Types of Temporal Data Application Time / Real World Time Marie moved from Texas to California on November 15, 2015 System Time Marie’s address was changed from Texas to California on February 10, 2016
AdventureWorks Bike Shop October 2015 – Marie buys a bike in Texas November 2015 – Marie moves to California January 2016 – An invalid email promotion is sent out to people in Texas February 2016 – Marie buys a bike in California March 2016 – AdventureWorks sends a coupon to apologize for the invalid promotion in Texas
AdventureWorks Bike Shop Before Temporal Tables Date Activity Temporal Type System Data Reality October 2015 Marie buys a bike in Texas November 2015 Marie moves to California January 2016 Invalid email promotion to people in Texas February 2016 Marie buys a bike in California March 2016 Company wants to send a coupon to people in Texas
AdventureWorks Bike Shop Before Temporal Tables Date Activity Temporal Type System Data Reality October 2015 Marie buys a bike in Texas System November 2015 Marie moves to California Real World January 2016 Invalid email promotion to people in Texas February 2016 Marie buys a bike in California March 2016 Company wants to send a coupon to people in Texas
AdventureWorks Bike Shop Before Temporal Tables Date Activity Temporal Type System Data Reality October 2015 Marie buys a bike in Texas System Marie in Texas November 2015 Marie moves to California Real World January 2016 Invalid email promotion to people in Texas Marie in Texas Marie receives email February 2016 Marie buys a bike in California Marie in California March 2016 Company wants to send a coupon to people in Texas Marie does not receive coupon
AdventureWorks Bike Shop Before Temporal Tables Date Activity Temporal Type System Data Reality October 2015 Marie buys a bike in Texas System Marie in Texas November 2015 Marie moves to California Real World Marie in California January 2016 Invalid email promotion to people in Texas Marie in Texas Marie receives email Marie in California Marie receives email February 2016 Marie buys a bike in California March 2016 Company wants to send a coupon to people in Texas Marie does not receive coupon Marie is entitled to a coupon
System Versioned Temporal Tables Adds a history table to a user table Old rows are moved to history table as data changes History can be queried with new TSQL syntax Requires no changes to applications
System Versioned Temporal Tables User Table History Table Update Row Old Row select * from myTable select * from myTable for system_time as of ‘2016-01-01’
AdventureWorks Bike Shop With Temporal Tables Date Activity Temporal Type System Data Reality October 2015 Marie buys a bike in Texas System Marie in Texas November 2015 Marie moves to California Real World Marie in California January 2016 Invalid email promotion to people in Texas Marie in Texas Marie receives email Marie in California Marie receives email February 2016 Marie buys a bike in California March 2016 Company wants to send a coupon to people in Texas Marie receives a coupon Marie is entitled to a coupon
Use Cases Accessing history data Auditing data changes Repairing invalid data on a granular basis
Creating System Versioned Temporal Tables Create new table Add history to existing table Combine existing table and existing history table
Current and History Tables Must have the same columns May have different physical structure May be differently indexed May be partitioned, columnstore, and/or memory-optimized
Creating System Versioned Temporal Tables create table PersonTemporal ( BusinessEntityID int not null primary key clustered , FirstName dbo.Name not null , LastName dbo.Name not null , City nvarchar(30) , State dbo.Name , SystemStartTime datetime2 generated always as row start not null , SystemEndTime datetime2 generated always as row end not null , Period for System_Time (SystemStartTime,SystemEndTime) ) with (system_versioning = on (history_table = PersonTemporalHistory) );
Maintaining System Versioned Temporal Tables Cannot drop, truncate, or switch out partitions Cannot delete from history table System versioning columns can be hidden
DEMO
Querying System Versioned Temporal Tables FOR SYSTEM TIME clause AS OF FROM (start) to (end) BETWEEN (start) and (end) CONTAINED IN (start, end) ALL
Querying System Versioned Temporal Tables AS OF Value in table as of that date Did not need to be modified ON that date Example: Updated Marie’s record to California on February 5 As of January she’s in Texas As of March she is in California
For System Time Examples Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6
For System Time Examples Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 AS OF Jan 3
Querying System Versioned Temporal Tables FROM (start) to (end) Value(s) in table from start date to end date EXCLUSIVE of end date Example: Updated Marie’s record to California on February 5 From January 10 to February 10 she’s in Texas and California From January 5 to February 5 she’s in Texas ONLY
For System Time Examples Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 FROM Jan 2 TO Jan 4
Querying System Versioned Temporal Tables BETWEEN (start) and (end) Value(s) in table from start date to end date INCLUSIVE of end date Example: Updated Marie’s record to California on February 5 From January 10 to February 10 she’s in Texas and California From January 5 to February 5 she’s in Texas and California
For System Time Examples Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 BETWEEN Jan 2 AND Jan 4
Querying System Versioned Temporal Tables CONTAINED IN (start, end) Value(s) in table from start date to end date Must both start and end in the timeframe Therefore all data will be from the history table Example: Inserted Marie’s Texas record November 10 Updated Marie’s record to California on February 5 Contained in (November 1, March 1) returns Marie’s Texas record Contained in (February 1, March 1) returns no data for Marie
For System Time Examples Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 CONTAINED IN Jan 2, Jan 4
Querying System Versioned Temporal Tables ALL Returns all rows from current and history table Any restrictions come from WHERE clause Example: Inserted Marie’s Texas record November 10 Updated Marie’s record to California on February 5 Corrected typo on February 6 ALL returns Nov 10, Feb 5, and Feb 6
For System Time Examples Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 ALL
DEMO
Review System versioned tables available in SQL 2016 Versioning may be turned on and off Current and history tables can be separately queried Use FOR SYSTEM TIME clause to automatically locate in the correct table
Thank You!