Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SQL 2016 Temporal Tables

Similar presentations


Presentation on theme: "Introduction to SQL 2016 Temporal Tables"— Presentation transcript:

1 Introduction to SQL 2016 Temporal Tables
Vicky Harp

2 About Me Working with SQL Server since 2001
Working at Idera since 2004

3 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

4 Shopping Cart Contents
Data Changes Over Time Postal Codes Physical Addresses Addresses Shopping Cart Contents

5 Data Changes For Different Reasons
Customer Address Customer Moved Street Name Changed Typo When Entering Data Data Corruption

6 Temporal Databases Defined in SQL:2011 Specification
Available from multiple RDBMS vendors SQL Server: System Versioned Temporal Tables

7 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

8 AdventureWorks Bike Shop
October 2015 – Marie buys a bike in Texas November 2015 – Marie moves to California January 2016 – An invalid 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

9 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 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

10 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 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

11 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 promotion to people in Texas Marie in Texas Marie receives 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

12 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 promotion to people in Texas Marie in Texas Marie receives Marie in California Marie receives 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

13 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

14 System Versioned Temporal Tables
User Table History Table Update Row Old Row select * from myTable select * from myTable for system_time as of ‘ ’

15 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 promotion to people in Texas Marie in Texas Marie receives Marie in California Marie receives 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

16 Use Cases Accessing history data Auditing data changes
Repairing invalid data on a granular basis

17 Creating System Versioned Temporal Tables
Create new table Add history to existing table Combine existing table and existing history table

18 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

19 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) );

20 Maintaining System Versioned Temporal Tables
Cannot drop, truncate, or switch out partitions Cannot delete from history table System versioning columns can be hidden

21 DEMO

22 Querying System Versioned Temporal Tables
FOR SYSTEM TIME clause AS OF FROM (start) to (end) BETWEEN (start) and (end) CONTAINED IN (start, end) ALL

23 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

24 For System Time Examples
Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6

25 For System Time Examples
Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 AS OF Jan 3

26 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

27 For System Time Examples
Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 FROM Jan 2 TO Jan 4

28 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

29 For System Time Examples
Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 BETWEEN Jan 2 AND Jan 4

30 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

31 For System Time Examples
Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 CONTAINED IN Jan 2, Jan 4

32 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

33 For System Time Examples
Jan 1 Jan 2 Jan 3 Jan 4 Jan 5 Jan 6 ALL

34 DEMO

35 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

36 Thank You!


Download ppt "Introduction to SQL 2016 Temporal Tables"

Similar presentations


Ads by Google