Temporal features and temporal tables clearly explained and with examples! (Special: WHO'S WHO in the SQL world pt/2.. Mr. Krishna Kulkarni)
Hi guys,
Why is this an important topic?
Well, how many times you had to track changes in data contained into a table? I am sure, so many times!
Probably you solved this problem of tracking changes to your data (and so to know what happened to your data in the past) using triggers. A trigger is fired every time the value of a field in a table change. This trigger will add a row into a log table.
It is necessary to know that this operation is natively supported by many database systems and also our Microsoft SQL Server has been supporting it for several years.
Enjoy the reading!
SQL:2011 and the Temporal Features. A Bit of Theory.
We start talking about temporal tables by saying that they are supported by different database engines because they have been defined as SQL language standard.
SQL:2011 (the seventh revision of the ISO and ANSI standard for the SQL database query language) was published in December of 2011, replacing the previous SQL:2008 release.
Well, one of the most important new functionality introduced as part of SQL:2011 standard is just the ability to create and manipulate temporal tables.
We are talking of tables whose rows are associated with temporal periods and indeed, time period is a keyword of this post!
Yeah, but what is time period?
Period
Well, a time period is a mathematical concept: an interval on the timeline demarcated by a start time and an end time.
The SQL:2011 standard has chosen to manage time periods by adding the period definition as metadata.
A period definition is a named table component identifying a pair of columns that capture the period start and the period end.
Dimensions of time for temporal data support: Valid time and transaction time
Valid time is the time period during which a row in the table is regarded as correctly reflecting realty by the user of the database.
Transaction time is the time period during which a row is committed to the database.
Valid time may differ from its valid time: for example in a database we have a table that contain a Price list. Informations about a Price of an item may get inserted (this is the transaction time) before that price comes into effect (this is the valid time).
In SQL:2011 transaction time support is provided by system-versioned tables with in turn contain the system-time period.
Valid time support is provided by tables containing an application-time period.
Let's see now first the application-time period tables then the system-versioned tables.
Application-time period tables
This tables are intended to support application that must capturing time periods during which the data is believed to be valid in the real world.
A typical example could be manage a price list.
CREATE TABLE Emp(
ENo INTEGER,
EStart DATE,
EEnd DATE,
EDept INTEGER,
PERIOD FOR My_Period (EStart, EEnd)
)
This is an application-time period table because the period definition has an user-defined name (My_Period)To be clear INSERT, UPDATE and DELETE statements works as in a normal table but the syntax in the SQL:2011standard is expanded in the UPDATE and DELETE statements to allow you to manage changes that are effective only within a specified period.
You can use the "FOR PORTION OF" with "FROM DATE" and "TO DATE" extensions:
UPDATE Emp FOR PORTION OF EPeriod FROM DATE '2011-02-03' TO DATE '2011-09-10' SET EDept = 4 WHERE ENo = 22217
Suppose you have an EMP table that contains data about the department (EDept) where an employee works.
The Employee 22217 works in the Department 3 from the first day of the year 2010 until the date 2011-11-12
This employee at some time has to move to the new department 4 but will only do so from 2011-02-03 to 2011-09-11. Then he will return to Ward 3.
The UPDATE statement is the one previously seen:
UPDATE Emp FOR PORTION OF EPeriod FROM DATE '2011-02-03' TO DATE '2011-09-10' SET EDept = 4 WHERE ENo = 22217
The result is the following..
Then i execute the following DELETE statement:
DELETE Emp FOR PORTION OF EPeriod FROM DATE '2011-02-03' TO DATE '2011-09-10' WHERE ENo = 22217
This is what i get:
System-versioned tables
- Every UPDATE or DELETE on a row will preserve the old state of the row.
- The system will mantains the start and the end of the period.
- Users cannot modify the contents of historical rows.
CREATE TABLE dbo.PriceList ( [ID] int NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED , [IdList] INT not null , [IdProduct] INT , [Price] Float ,[ValidFrom] Datetime2 GENERATED ALWAYS AS ROW START , [ValidTo] Datetime2 GENERATED ALWAYS AS ROW END , PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo) ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.PriceList_History));
INSERT INTO PriceList (IdList, IdProduct, Price)
VALUES (1,1,9.88)
The content of the table will be the following:
UPDATE PRICELIST
SET PRICE = 10.01
WHERE
ID = 1
When I show the content of the table PRICELIST with the following command:
I get this data:
SELECT * FROM PRICELIST
SELECT * FROM PRICELIST
FOR SYSTEM_TIME
BETWEEN '2021-01-01 00:00:00.0000000' AND '9999-12-31 23:59:59.9999999'
I should have used at least one trigger!
What do I get?DELETE FROM PRICELIST
WHERE
ID = 1
I get correctly an empty table:
SELECT * FROM PRICELIST
SELECT * FROM PRICELIST
FOR SYSTEM_TIME
BETWEEN '2021-01-01 00:00:00.0000000' AND '9999-12-31 23:59:59.9999999'
- UPDATE and DELETE operation, as said previously, operate only on current system rows.
- Each UPDATE and DELETE led to the automatic insertion into the table of an historical system row.
For today we will not go into further details related to time tables ...such as bitemporal tables also because they are not supported by Microsoft SQL Server.
In the next paragraph, for the WHO’S IS WHO series, we will briefly mention who created this theory and formalized it as a standard.
Comments
Post a Comment