Search notes:

SQL: Temporal data

Support for temporal data was introduced in SQL:2011
Temporal tables should not be mistaken with temporary tables.
period data type: an interval. period is to time as geometry is to spatial data.
With support for temporal data:

Time dimensions

Three time dimensions
Time models:

Transaction time

What was the ACID state of the data as of <time>?
Thus, it only stores past data states.
To be maintained by the system (»system versioned tables«).
Useful for auditing.
Records for which the current time falls into their period are called system rows. Other records are called historical system rows.
Only system rows can be updated or deleted. Similarly, constraints are enforced on system rows only.

Syntax

The create table statement contains a period clause whose name is system_time.
create table t (
   col_one …,
   …
   validFrom     timestamp(12) generated always as row begin not null,
   validTo       timestamp(12) generated always as row end   not null,
   trxId         timestamp(12) generated always as transaction start id,
   period system_time (vaildFrom, validTo)
)
[ with (system_versioning = on) ] -- ???
;

create table t_hist like t;
alter  table t add versioning use history table t_hist;


select … from t as of system time '2019-04-03 09:52:18' where …;
select … from t as of system time  timestamp '2019-04-03';
select … from t as of system time  current_timestamp;
select … from t as of system time  current_timestamp - 10 days;
select … from t as of system time  current_timestamp -  7 minutes;

On DB2:
select • from t for system_time as of '2019-04-03 09:52:18' where …
select • from t as of timestamp       '2019-04-03 09:52:18' where …
Oracle (flashback query):
select * from t as of timestamp       '2019-04-03 09:52:18' where …


-- Pseude columns
select
   version_starttime,
   version_endtime,
   version_xid
   …

Business Time (aka Application time)

Maintained by user/applications (not by system).
Possibility of entering future dates (I'll change my address on 1st of November of 2019). Such an update statement is called proactive update.
The create table statement also contains a period clause, however, for business time tables, the name of the period is user-definable.

Syntax

The standard does (not yet?) propose a syntax for querying Business Time.
DB2:
select * from t for business_time as of '2019-04-03 09:52:18';

Support for temporal data

Some databases that support temporal data:

See also

System versioned tables in SQL Server

Links

https://pgxn.org/dist/temporal_tables
wiki.postgresql.org/wiki/SQL2011Temporal
http://metadata-standards.org/Document-library/Documents-by-number/WG2-N1501-N1550/WG2_N1536_koa046-Temporal-features-in-SQL-standard.pdf

Index