SlideShare una empresa de Scribd logo
1 de 22
Descargar para leer sin conexión
BASLE BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA
HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH
SQL Server 2016 Temporal Tables
Willfried Färber – BS
willfried.faerber@trivadis.com
Agenda
1. Overview of SQL Server 2016 temporal tables
2. Data Definition for temporal tables
3. Data Manipulation with temporal tables
4. Usage Scenarios
Why – History Tables
• Auditing all data changes and performing data
forensics when necessary
• Reconstructing state of the data as of any time in
the past
• Calculating trends over time – Anomaly Detection
• Maintaining a slowly changing dimension for
decision support applications
• Recovering from accidental data changes and
application errors
What is a Temporal Table?
• A temporal table is a table for which a PERIOD definition exists and which
contains system columns with a datatype of datetime2 into which the period
of validity is recorded by the system, and which has an associated history
table into which the system records all prior versions of each record with
their period of validity.
• With a temporal table, the value of each record at any point in time can be
determined, rather than just the current value of each record. A temporal
table is also referred to as a system-versioned table.
Temporal Tables
Automatically keeps track of changed data
• Along with period of validity
• Available in all editions of SQL Server (from the Express!)
• Available in SQL Azure
• SQL ANSI 2011 compliant
• Provide native support for “as-of-time” queries
• Transparent to existing applications (if needed)
• Integrated with existing features (eg: Partitioning, Hekaton)
• Integrated with new features (eg: Stretch Database)
Insert, Update, Delete
New clauses – Data Definition
CREATE TABLE Department (
DeptID int NOT NULL PRIMARY KEY CLUSTERED ,
DeptName varchar(50) NOT NULL ,
ManagerID INT NULL ,
ParentDeptID int 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.DepartmentHistory) ) ;
GENERATED ALWAYS AS ROW START
GENERATED ALWAYS AS ROW END
PERIOD FOR SYSTEM_TIME
SYSTEM_VERSIONING = ON
BEGIN TRANSACTION
-- Add required period columns and designation
ALTER TABLE dbo.Person ADD
DateTimeStart DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL
CONSTRAINT DFT_person_datetimeStart DEFAULT('19000101 00:00:00'),
DateTimeEnd DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL
CONSTRAINT DFT_person_datetimeEnd DEFAULT('99991231 23:59:59'),
PERIOD FOR SYSTEM_TIME (DateTimeStart, DateTimeEnd);
-- Remove temporary DEFAULT constraints
ALTER TABLE dbo.Person
DROP CONSTRAINT DFT_person_datetimeStart, DFT_person_datetimeEnd;
-- Turn system versioning on
ALTER TABLE dbo.Person
SET ( SYSTEM_VERSIONING = ON ( HISTORY_TABLE = dbo.PersonHistory ) );
COMMIT TRANSACTION;
System generated temporal table
SQL Server 2016 Temporal Tables9 30-Oct-17
Creating through SSMS
Querying temporal data
FOR predicate
SQL Server 2016 Temporal Tables11 30-Oct-17
Expression Qualifying Rows
AS OF <date_time> SysStartTime < = date_time AND SysEndTime >
date_time
FROM <start_date_time>
TO <end_date_time>
SysStartTime < end_date_time AND SysEndTime >
start_date_time
BETWEEN
<start_date_time> AND
<end_date_time>
SysStartTime < = end_date_time AND SysEndTime >
start_date_time
CONTAINED IN
(<start_date_time>,
<end_date_time>)
SysStartTime > = start_date_time AND SysEndTime < =
end_date_time
ALL all rows
New clauses – AS OF
DECLARE @datetime AS DATETIME2 = '20161108 20:00:00'
SELECT *
FROM dbo.Person FOR SYSTEM_TIME
AS OF @datetime
ORDER BY DateTimeStart ASC,
DateTimeEnd ASC
Clauses SYSTEM_TIME allow to realize
temporal queries transparently for user and
applications.
FOR SYSTEM_TIME AS OF @datetime
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysEnd > @datetime
sysStart <= @datetime
New clauses – AS OF
Returns a table with a rows containing
the values that were actual (current) at
the specified point in time in the past.
FOR SYSTEM_TIME
FROM @Start TO @End
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysStart < @End
sysEnd > @Start
New clauses – AS OF
Returns a table with the values for all row
versions that were active within the
specified time range, regardless of
whether they started being active before
the <start_date_time> parameter value for
the FROM argument or ceased being
active after the <end_date_time>
parameter value for the TO argument.
FOR SYSTEM_TIME
BETWEEN @Start AND @End
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysStart <= @End
sysEnd > @Start
New clauses – AS OF
Same as above in the FOR SYSTEM_TIME
FROM <start_date_time> TO
<end_date_time> description, except the
table of rows returned includes rows that
became active on the upper boundary
defined by the <end_date_time> endpoint.
FOR SYSTEM_TIME
CONTAINED IN (@Start,@End)
Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan)
sysStart >= @Start
sysEnd <= @End
New clauses – AS OF
Returns a table with the values for all row
versions that were opened and closed within
the specified time range defined by the two
datetime values for the CONTAINED IN
argument.
Considerations for resource use
• A temporal table must have a primary key defined in order to correlate records between the
current table and the history table, and the history table cannot have a primary key defined.
• The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values
must be defined with a datatype of datetime2.
• If the name of a history table is specified during history table creation, you must specify the
schema and table name.
• By default, the history table is PAGE compressed.
• If current table is partitioned, the history table is created on default file group because partitioning
configuration is not replicated automatically from the current table to the history table.
• Temporal and history tables cannot be FILETABLE and can contain columns of any
supported datatype other than FILESTREAM since FILETABLE and FILESTREAM allow data
manipulation outside of SQL Server and thus system versioning cannot be guaranteed.
More work, but much better …
SQL Server 2016 Temporal Tables18 30-Oct-17
Define the history table by your self
• Define a separate filegroup
• Use a special schema – iE. History
• Maybe use Column Store Index
• Maybe use Partitions
• Create Clustered Index on the file table
CREATE CLUSTERED INDEX uidx_<tablename>_hist ON <tablename>
(ValidTo ASC, ValidFrom ASC, EmployeeID ASC)
ON [History];
Limitations…
• TRUNCATE TABLE is not supported while SYSTEM_VERSIONING is ON
• Direct modification of the data in a history table is not permitted.
• ON DELETE CASCADE and ON UPDATE CASCADE are not permitted on the current table. In
other words, when temporal table is referencing table in the foreign key relationship
(corresponding to parent_object_id in sys.foreign_keys) CASCADE options are not allowed. To
work around this limitation, use application logic or after triggers to maintain consistency on delete
in primary key table (corresponding to referenced_object_id in sys.foreign_keys). If primary key
table is temporal and referencing table is non-temporal, there’s no such limitation.
• INSTEAD OF triggers are not permitted on either the current or the history table and AFTER
triggers are permitted only on the current table.
• Regular queries only affect data in the current table. To query data in the history table, you must
use temporal queries.
Security for temporal tables
SQL Server 2016 Temporal Tables20 30-Oct-17
• Creating a temporal table requires CONTROL rights
• DBOwner has Control rights in the database
• When SYSTEM_VERSIONING is ON users cannot alter history data regardless of
their actual permissions on current or the history table. This includes both data and
schema modifications
• Querying history data requires SELECT permission on the history table
• Merely because a user has SELECT permission on the current table does not
mean that they have SELECT permission on the history table
https://msdn.microsoft.com/en-us/library/mt604469.aspx
Willfried Färber
Principal Consultant
Tel. +41 58 459 50 11
willfried.faerber@trivadis.com
30.10.2017 SQL Server 2016 Temporal Tables21
Source code for the demos
SQL Server 2016 Temporal Tables22 30.10.2017

Más contenido relacionado

La actualidad más candente

DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 

La actualidad más candente (20)

Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
Refactoring database
Refactoring databaseRefactoring database
Refactoring database
 
Sql server lesson7
Sql server lesson7Sql server lesson7
Sql server lesson7
 
Sql2
Sql2Sql2
Sql2
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
Oracle Table Partitioning - Introduction
Oracle Table Partitioning  - IntroductionOracle Table Partitioning  - Introduction
Oracle Table Partitioning - Introduction
 
Chapter16
Chapter16Chapter16
Chapter16
 
Overview of Oracle database12c for developers
Overview of Oracle database12c for developersOverview of Oracle database12c for developers
Overview of Oracle database12c for developers
 
Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL
 
Les09
Les09Les09
Les09
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Oracle Data Redaction
Oracle Data RedactionOracle Data Redaction
Oracle Data Redaction
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Oracle Data Redaction - UKOUG - TECH14
Oracle Data Redaction - UKOUG - TECH14Oracle Data Redaction - UKOUG - TECH14
Oracle Data Redaction - UKOUG - TECH14
 
SKILLWISE-DB2 DBA
SKILLWISE-DB2 DBASKILLWISE-DB2 DBA
SKILLWISE-DB2 DBA
 
Rdbms day3
Rdbms day3Rdbms day3
Rdbms day3
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 

Similar a Trivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried Färber

Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaaPerfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Cuneyt Goksu
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.ppt
Iftikhar70
 

Similar a Trivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried Färber (20)

Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaaPerfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
Perfect trio : temporal tables, transparent archiving in db2 for z_os and idaa
 
Walking down the memory lane with temporal tables
Walking down the memory lane with temporal tablesWalking down the memory lane with temporal tables
Walking down the memory lane with temporal tables
 
Sql server 2016 new features
Sql server 2016 new featuresSql server 2016 new features
Sql server 2016 new features
 
Sql server 2016 new features
Sql server 2016 new featuresSql server 2016 new features
Sql server 2016 new features
 
Informix partitioning interval_rolling_window_table
Informix partitioning interval_rolling_window_tableInformix partitioning interval_rolling_window_table
Informix partitioning interval_rolling_window_table
 
Sql 2016 - What's New
Sql 2016 - What's NewSql 2016 - What's New
Sql 2016 - What's New
 
Webinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstrationWebinar - MariaDB Temporal Tables: a demonstration
Webinar - MariaDB Temporal Tables: a demonstration
 
Time Travelling With DB2 10 For zOS
Time Travelling With DB2 10 For zOSTime Travelling With DB2 10 For zOS
Time Travelling With DB2 10 For zOS
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Geek Sync I Polybase and Time Travel (Temporal Tables)
Geek Sync I Polybase and Time Travel (Temporal Tables)Geek Sync I Polybase and Time Travel (Temporal Tables)
Geek Sync I Polybase and Time Travel (Temporal Tables)
 
Flashback time travel vs Flash back Data Archive.pdf
Flashback time travel  vs Flash back Data Archive.pdfFlashback time travel  vs Flash back Data Archive.pdf
Flashback time travel vs Flash back Data Archive.pdf
 
Temporal Tables, Transparent Archiving in DB2 for z/OS and IDAA
Temporal Tables, Transparent Archiving in DB2 for z/OS and IDAATemporal Tables, Transparent Archiving in DB2 for z/OS and IDAA
Temporal Tables, Transparent Archiving in DB2 for z/OS and IDAA
 
Redefining tables online without surprises
Redefining tables online without surprisesRedefining tables online without surprises
Redefining tables online without surprises
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.ppt
 
database-stucture-and-space-managment.ppt
database-stucture-and-space-managment.pptdatabase-stucture-and-space-managment.ppt
database-stucture-and-space-managment.ppt
 
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
 
MariaDB Temporal Tables
MariaDB Temporal TablesMariaDB Temporal Tables
MariaDB Temporal Tables
 
Time is of the essence - The Fourth Dimension in Oracle Database 12c (on Flas...
Time is of the essence - The Fourth Dimension in Oracle Database 12c (on Flas...Time is of the essence - The Fourth Dimension in Oracle Database 12c (on Flas...
Time is of the essence - The Fourth Dimension in Oracle Database 12c (on Flas...
 
Survey On Temporal Data And Change Management in Data Warehouses
Survey On Temporal Data And Change Management in Data WarehousesSurvey On Temporal Data And Change Management in Data Warehouses
Survey On Temporal Data And Change Management in Data Warehouses
 

Más de Trivadis

Más de Trivadis (20)

Azure Days 2019: Azure Chatbot Development for Airline Irregularities (Remco ...
Azure Days 2019: Azure Chatbot Development for Airline Irregularities (Remco ...Azure Days 2019: Azure Chatbot Development for Airline Irregularities (Remco ...
Azure Days 2019: Azure Chatbot Development for Airline Irregularities (Remco ...
 
Azure Days 2019: Trivadis Azure Foundation – Das Fundament für den ... (Nisan...
Azure Days 2019: Trivadis Azure Foundation – Das Fundament für den ... (Nisan...Azure Days 2019: Trivadis Azure Foundation – Das Fundament für den ... (Nisan...
Azure Days 2019: Trivadis Azure Foundation – Das Fundament für den ... (Nisan...
 
Azure Days 2019: Business Intelligence auf Azure (Marco Amhof & Yves Mauron)
Azure Days 2019: Business Intelligence auf Azure (Marco Amhof & Yves Mauron)Azure Days 2019: Business Intelligence auf Azure (Marco Amhof & Yves Mauron)
Azure Days 2019: Business Intelligence auf Azure (Marco Amhof & Yves Mauron)
 
Azure Days 2019: Master the Move to Azure (Konrad Brunner)
Azure Days 2019: Master the Move to Azure (Konrad Brunner)Azure Days 2019: Master the Move to Azure (Konrad Brunner)
Azure Days 2019: Master the Move to Azure (Konrad Brunner)
 
Azure Days 2019: Keynote Azure Switzerland – Status Quo und Ausblick (Primo A...
Azure Days 2019: Keynote Azure Switzerland – Status Quo und Ausblick (Primo A...Azure Days 2019: Keynote Azure Switzerland – Status Quo und Ausblick (Primo A...
Azure Days 2019: Keynote Azure Switzerland – Status Quo und Ausblick (Primo A...
 
Azure Days 2019: Grösser und Komplexer ist nicht immer besser (Meinrad Weiss)
Azure Days 2019: Grösser und Komplexer ist nicht immer besser (Meinrad Weiss)Azure Days 2019: Grösser und Komplexer ist nicht immer besser (Meinrad Weiss)
Azure Days 2019: Grösser und Komplexer ist nicht immer besser (Meinrad Weiss)
 
Azure Days 2019: Get Connected with Azure API Management (Gerry Keune & Stefa...
Azure Days 2019: Get Connected with Azure API Management (Gerry Keune & Stefa...Azure Days 2019: Get Connected with Azure API Management (Gerry Keune & Stefa...
Azure Days 2019: Get Connected with Azure API Management (Gerry Keune & Stefa...
 
Azure Days 2019: Infrastructure as Code auf Azure (Jonas Wanninger & Daniel H...
Azure Days 2019: Infrastructure as Code auf Azure (Jonas Wanninger & Daniel H...Azure Days 2019: Infrastructure as Code auf Azure (Jonas Wanninger & Daniel H...
Azure Days 2019: Infrastructure as Code auf Azure (Jonas Wanninger & Daniel H...
 
Azure Days 2019: Wie bringt man eine Data Analytics Plattform in die Cloud? (...
Azure Days 2019: Wie bringt man eine Data Analytics Plattform in die Cloud? (...Azure Days 2019: Wie bringt man eine Data Analytics Plattform in die Cloud? (...
Azure Days 2019: Wie bringt man eine Data Analytics Plattform in die Cloud? (...
 
Azure Days 2019: Azure@Helsana: Die Erweiterung von Dynamics CRM mit Azure Po...
Azure Days 2019: Azure@Helsana: Die Erweiterung von Dynamics CRM mit Azure Po...Azure Days 2019: Azure@Helsana: Die Erweiterung von Dynamics CRM mit Azure Po...
Azure Days 2019: Azure@Helsana: Die Erweiterung von Dynamics CRM mit Azure Po...
 
TechEvent 2019: Kundenstory - Kein Angebot, kein Auftrag – Wie Du ein individ...
TechEvent 2019: Kundenstory - Kein Angebot, kein Auftrag – Wie Du ein individ...TechEvent 2019: Kundenstory - Kein Angebot, kein Auftrag – Wie Du ein individ...
TechEvent 2019: Kundenstory - Kein Angebot, kein Auftrag – Wie Du ein individ...
 
TechEvent 2019: Oracle Database Appliance M/L - Erfahrungen und Erfolgsmethod...
TechEvent 2019: Oracle Database Appliance M/L - Erfahrungen und Erfolgsmethod...TechEvent 2019: Oracle Database Appliance M/L - Erfahrungen und Erfolgsmethod...
TechEvent 2019: Oracle Database Appliance M/L - Erfahrungen und Erfolgsmethod...
 
TechEvent 2019: Security 101 für Web Entwickler; Roland Krüger - Trivadis
TechEvent 2019: Security 101 für Web Entwickler; Roland Krüger - TrivadisTechEvent 2019: Security 101 für Web Entwickler; Roland Krüger - Trivadis
TechEvent 2019: Security 101 für Web Entwickler; Roland Krüger - Trivadis
 
TechEvent 2019: Trivadis & Swisscom Partner Angebote; Konrad Häfeli, Markus O...
TechEvent 2019: Trivadis & Swisscom Partner Angebote; Konrad Häfeli, Markus O...TechEvent 2019: Trivadis & Swisscom Partner Angebote; Konrad Häfeli, Markus O...
TechEvent 2019: Trivadis & Swisscom Partner Angebote; Konrad Häfeli, Markus O...
 
TechEvent 2019: DBaaS from Swisscom Cloud powered by Trivadis; Konrad Häfeli ...
TechEvent 2019: DBaaS from Swisscom Cloud powered by Trivadis; Konrad Häfeli ...TechEvent 2019: DBaaS from Swisscom Cloud powered by Trivadis; Konrad Häfeli ...
TechEvent 2019: DBaaS from Swisscom Cloud powered by Trivadis; Konrad Häfeli ...
 
TechEvent 2019: Status of the partnership Trivadis and EDB - Comparing Postgr...
TechEvent 2019: Status of the partnership Trivadis and EDB - Comparing Postgr...TechEvent 2019: Status of the partnership Trivadis and EDB - Comparing Postgr...
TechEvent 2019: Status of the partnership Trivadis and EDB - Comparing Postgr...
 
TechEvent 2019: More Agile, More AI, More Cloud! Less Work?!; Oliver Dörr - T...
TechEvent 2019: More Agile, More AI, More Cloud! Less Work?!; Oliver Dörr - T...TechEvent 2019: More Agile, More AI, More Cloud! Less Work?!; Oliver Dörr - T...
TechEvent 2019: More Agile, More AI, More Cloud! Less Work?!; Oliver Dörr - T...
 
TechEvent 2019: Kundenstory - Vom Hauptmann zu Köpenick zum Polizisten 2020 -...
TechEvent 2019: Kundenstory - Vom Hauptmann zu Köpenick zum Polizisten 2020 -...TechEvent 2019: Kundenstory - Vom Hauptmann zu Köpenick zum Polizisten 2020 -...
TechEvent 2019: Kundenstory - Vom Hauptmann zu Köpenick zum Polizisten 2020 -...
 
TechEvent 2019: Vom Rechenzentrum in die Oracle Cloud - Übertragungsmethoden;...
TechEvent 2019: Vom Rechenzentrum in die Oracle Cloud - Übertragungsmethoden;...TechEvent 2019: Vom Rechenzentrum in die Oracle Cloud - Übertragungsmethoden;...
TechEvent 2019: Vom Rechenzentrum in die Oracle Cloud - Übertragungsmethoden;...
 
TechEvent 2019: The sleeping Power of Data; Eberhard Lösch - Trivadis
TechEvent 2019: The sleeping Power of Data; Eberhard Lösch - TrivadisTechEvent 2019: The sleeping Power of Data; Eberhard Lösch - Trivadis
TechEvent 2019: The sleeping Power of Data; Eberhard Lösch - Trivadis
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Trivadis TechEvent 2017 SQL Server 2016 Temporal Tables by Willfried Färber

  • 1. BASLE BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH SQL Server 2016 Temporal Tables Willfried Färber – BS willfried.faerber@trivadis.com
  • 2. Agenda 1. Overview of SQL Server 2016 temporal tables 2. Data Definition for temporal tables 3. Data Manipulation with temporal tables 4. Usage Scenarios
  • 3. Why – History Tables • Auditing all data changes and performing data forensics when necessary • Reconstructing state of the data as of any time in the past • Calculating trends over time – Anomaly Detection • Maintaining a slowly changing dimension for decision support applications • Recovering from accidental data changes and application errors
  • 4. What is a Temporal Table? • A temporal table is a table for which a PERIOD definition exists and which contains system columns with a datatype of datetime2 into which the period of validity is recorded by the system, and which has an associated history table into which the system records all prior versions of each record with their period of validity. • With a temporal table, the value of each record at any point in time can be determined, rather than just the current value of each record. A temporal table is also referred to as a system-versioned table.
  • 5. Temporal Tables Automatically keeps track of changed data • Along with period of validity • Available in all editions of SQL Server (from the Express!) • Available in SQL Azure • SQL ANSI 2011 compliant • Provide native support for “as-of-time” queries • Transparent to existing applications (if needed) • Integrated with existing features (eg: Partitioning, Hekaton) • Integrated with new features (eg: Stretch Database)
  • 7. New clauses – Data Definition CREATE TABLE Department ( DeptID int NOT NULL PRIMARY KEY CLUSTERED , DeptName varchar(50) NOT NULL , ManagerID INT NULL , ParentDeptID int 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.DepartmentHistory) ) ; GENERATED ALWAYS AS ROW START GENERATED ALWAYS AS ROW END PERIOD FOR SYSTEM_TIME SYSTEM_VERSIONING = ON
  • 8. BEGIN TRANSACTION -- Add required period columns and designation ALTER TABLE dbo.Person ADD DateTimeStart DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL CONSTRAINT DFT_person_datetimeStart DEFAULT('19000101 00:00:00'), DateTimeEnd DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL CONSTRAINT DFT_person_datetimeEnd DEFAULT('99991231 23:59:59'), PERIOD FOR SYSTEM_TIME (DateTimeStart, DateTimeEnd); -- Remove temporary DEFAULT constraints ALTER TABLE dbo.Person DROP CONSTRAINT DFT_person_datetimeStart, DFT_person_datetimeEnd; -- Turn system versioning on ALTER TABLE dbo.Person SET ( SYSTEM_VERSIONING = ON ( HISTORY_TABLE = dbo.PersonHistory ) ); COMMIT TRANSACTION;
  • 9. System generated temporal table SQL Server 2016 Temporal Tables9 30-Oct-17 Creating through SSMS
  • 11. FOR predicate SQL Server 2016 Temporal Tables11 30-Oct-17 Expression Qualifying Rows AS OF <date_time> SysStartTime < = date_time AND SysEndTime > date_time FROM <start_date_time> TO <end_date_time> SysStartTime < end_date_time AND SysEndTime > start_date_time BETWEEN <start_date_time> AND <end_date_time> SysStartTime < = end_date_time AND SysEndTime > start_date_time CONTAINED IN (<start_date_time>, <end_date_time>) SysStartTime > = start_date_time AND SysEndTime < = end_date_time ALL all rows
  • 12. New clauses – AS OF DECLARE @datetime AS DATETIME2 = '20161108 20:00:00' SELECT * FROM dbo.Person FOR SYSTEM_TIME AS OF @datetime ORDER BY DateTimeStart ASC, DateTimeEnd ASC Clauses SYSTEM_TIME allow to realize temporal queries transparently for user and applications.
  • 13. FOR SYSTEM_TIME AS OF @datetime Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysEnd > @datetime sysStart <= @datetime New clauses – AS OF Returns a table with a rows containing the values that were actual (current) at the specified point in time in the past.
  • 14. FOR SYSTEM_TIME FROM @Start TO @End Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysStart < @End sysEnd > @Start New clauses – AS OF Returns a table with the values for all row versions that were active within the specified time range, regardless of whether they started being active before the <start_date_time> parameter value for the FROM argument or ceased being active after the <end_date_time> parameter value for the TO argument.
  • 15. FOR SYSTEM_TIME BETWEEN @Start AND @End Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysStart <= @End sysEnd > @Start New clauses – AS OF Same as above in the FOR SYSTEM_TIME FROM <start_date_time> TO <end_date_time> description, except the table of rows returned includes rows that became active on the upper boundary defined by the <end_date_time> endpoint.
  • 16. FOR SYSTEM_TIME CONTAINED IN (@Start,@End) Reference: http://sqlmag.com/sql-server/first-look-system-versioned-temporal-tables-part-2-querying-data-and-optimization-conside (Itzik Ben Gan) sysStart >= @Start sysEnd <= @End New clauses – AS OF Returns a table with the values for all row versions that were opened and closed within the specified time range defined by the two datetime values for the CONTAINED IN argument.
  • 17. Considerations for resource use • A temporal table must have a primary key defined in order to correlate records between the current table and the history table, and the history table cannot have a primary key defined. • The SYSTEM_TIME period columns used to record the SysStartTime and SysEndTime values must be defined with a datatype of datetime2. • If the name of a history table is specified during history table creation, you must specify the schema and table name. • By default, the history table is PAGE compressed. • If current table is partitioned, the history table is created on default file group because partitioning configuration is not replicated automatically from the current table to the history table. • Temporal and history tables cannot be FILETABLE and can contain columns of any supported datatype other than FILESTREAM since FILETABLE and FILESTREAM allow data manipulation outside of SQL Server and thus system versioning cannot be guaranteed.
  • 18. More work, but much better … SQL Server 2016 Temporal Tables18 30-Oct-17 Define the history table by your self • Define a separate filegroup • Use a special schema – iE. History • Maybe use Column Store Index • Maybe use Partitions • Create Clustered Index on the file table CREATE CLUSTERED INDEX uidx_<tablename>_hist ON <tablename> (ValidTo ASC, ValidFrom ASC, EmployeeID ASC) ON [History];
  • 19. Limitations… • TRUNCATE TABLE is not supported while SYSTEM_VERSIONING is ON • Direct modification of the data in a history table is not permitted. • ON DELETE CASCADE and ON UPDATE CASCADE are not permitted on the current table. In other words, when temporal table is referencing table in the foreign key relationship (corresponding to parent_object_id in sys.foreign_keys) CASCADE options are not allowed. To work around this limitation, use application logic or after triggers to maintain consistency on delete in primary key table (corresponding to referenced_object_id in sys.foreign_keys). If primary key table is temporal and referencing table is non-temporal, there’s no such limitation. • INSTEAD OF triggers are not permitted on either the current or the history table and AFTER triggers are permitted only on the current table. • Regular queries only affect data in the current table. To query data in the history table, you must use temporal queries.
  • 20. Security for temporal tables SQL Server 2016 Temporal Tables20 30-Oct-17 • Creating a temporal table requires CONTROL rights • DBOwner has Control rights in the database • When SYSTEM_VERSIONING is ON users cannot alter history data regardless of their actual permissions on current or the history table. This includes both data and schema modifications • Querying history data requires SELECT permission on the history table • Merely because a user has SELECT permission on the current table does not mean that they have SELECT permission on the history table https://msdn.microsoft.com/en-us/library/mt604469.aspx
  • 21. Willfried Färber Principal Consultant Tel. +41 58 459 50 11 willfried.faerber@trivadis.com 30.10.2017 SQL Server 2016 Temporal Tables21
  • 22. Source code for the demos SQL Server 2016 Temporal Tables22 30.10.2017