M1 - Appendix - Temporal Features in SQL2011
M1 - Appendix - Temporal Features in SQL2011
ENo Sys_Start Sys_End EName 2.3.1 Primary key and referential con-
straints on system-versioned tables
22217 2012-01-01 2012-02-03 Joe
09:00:00 10:00:00 The definition and enforcement of constraints on sys-
tem-versioned tables is considerably simpler than the
22217 2012-02-03 9999-12-31 Tom definition and enforcement of constraints on applica-
10:00:00 23:59:59 tion-time period tables. This is because constraints on
system-versioned tables need only be enforced on the
In this example, the row whose name is Tom is the current system rows. Historical system rows in a sys-
updated row; UPDATE triggers fire for this row. Note tem-versioned table form immutable snapshots of the
that the insertion of historical system rows does not fire past. Any constraints that were in effect when a histori-
any INSERT triggers for the inserted rows. Note also cal system row was created would have already been
that historical system rows created as a result of checked when that row was a current system row, so
sequence of updates for a given row form one contigu- there is never any need to enforce constraints on histori-
ous chain without any gap between their system-time cal system rows. Consequently, there is no need to
periods. include the system-period start and end columns or the
A DELETE statement on a system-versioned table period name in the definition of primary key and refer-
does not actually delete the qualifying rows; instead it ential constraints on system-versioned tables. For exam-
changes the system-time period end time of those row to ple, the following ALTER TABLE statement specifies
the transaction timestamp, indicating that those rows ENo column as the primary key of Emp table:
ceased to be current as of the transaction timestamp. For ALTER TABLE Emp
example, suppose that the current system row with ENo ADD PRIMARY KEY (ENo)
22217 is as shown below: The above constraint ensures there exists exactly one
current system row with a given ENo value.
Similarly, the following ALTER TABLE statement
ENo Sys_Start Sys_End EName specifies a referential constraint between Emp and
22217 2012-01-01 9999-12-31 Joe Dept tables:
09:00:00 23:59:59 ALTER TABLE Emp
ADD FOREIGN KEY (Edept)
REFERENCES Dept (DNo)
The following DELETE statement simply changes the The above constraint is again enforced only on the
system-time period end time of the current system row current system rows of Emp and Dept tables.
for the employee 22217 to the transaction timestamp of
the transaction in which the DELETE statement was
executed:
2.3.2 Querying system-versioned tables
DELETE FROM Emp Because system-versioned tables are intended prima-
WHERE ENo = 22217 rily for tracking historical data changes, queries on sys-
Assuming the above statement is executed in a trans- tem-versioned tables often tend to be concerned with
action with the transaction timestamp 2012-06-01 retrieving the table content as of a given point in time or
00:00:00, the final result will be the following row: between any two given points in time. SQL:2011 pro-
vides three syntactic extensions for this specific pur-
ENo EStart EEnd EName pose. These are allowed only in queries on system-
versioned tables.
22217 2012-01-01 2012-06-01 Joe The first extension is the FOR SYSTEM_TIME AS
09:00:00 00:00:00 OF syntax that is useful for querying the table content
as of a specified point in time. For example, the follow-
In this example, DELETE triggers fire for the row ing query retrieves the rows of Emp that were current as
selected for deletion. of Jan. 2, 2011:
4. Acknowledgements
The authors thank Fred Zemke and Matthias Nicola
for their valuable comments on the prior versions of this
article.
5. References
[1] Fred Zemke, “What’s new in SQL:2011”, SIGMOD
Record, Vol. 41, No. 1, March 2012, pp. 67-73,
http://www.sigmod.org/publications/sigmod-record/
1203/pdfs/10.industry.zemke.pdf/
[2] Yu Wu, Sushil Jajodia, X. Sean Wang, “Temporal
Database Bibliography Update”, In Temporal Data-
bases: Research and Practice, O. Etzion, S. Jajodia,
and S.Sripada, eds., Springer, 1998
[3] Richard Snodgrass, “Developing Time-Oriented
Database Applications in SQL”, Morgan Kauf-
mann, 1999
[4] C. J. Date, Hugh Darwen, Nikos A. Lorentzos,
“Temporal Data and the Relational Model”, Morgan
Kaufman, 2003
[5] Cynthia Saracco, Matthias Nicola, Lenisha Gandhi,
“A matter of time: Temporal data management in
DB2 10”, April 2012, http://www.ibm.com/devel-
operworks/data/library/techarticle/dm-
1204db2temporaldata/
[6] Kevin Jerrigan, “Oracle Total Recall with Oracle
Database 11g Release 2”, September 2009, http://
www.oracle.com/us/products/database/security/
total-recall-whitepaper-171749.pdf