+ All Categories
Home > Documents > SQL Temporal Support V1.3 -...

SQL Temporal Support V1.3 -...

Date post: 16-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
26
3/13/2012 1 A.Sironi 2012 Angelo Sironi Sironi IT Consulting s.n.c. [email protected] 1 A.Sironi 2012 These charts are part of a larger presentation on the subject of Temporal Data support in DB2 10 for z/OS and all the additional SQL and application-related enhancements of DB2 10. The material is suited for a 1,5 – 2.0 days of education for application programmers and Data Base Administrators. 2
Transcript
Page 1: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

1

A.Sironi 2012

Angelo Sironi

Sironi IT Consulting s.n.c.

[email protected]

1

A.Sironi 2012

These charts are part of a larger presentation on the subject of Temporal Data support in DB2 10 for z/OS and all the additional SQL and application-related enhancements of DB2 10.

The material is suited for a 1,5 – 2.0 days of education for application programmers and Data Base Administrators.

2222

Page 2: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

2

A.Sironi 2012

� TimeTimeTimeTime----varying datavarying datavarying datavarying data◦ Type

◦ Examples

� Issues and complexitiesIssues and complexitiesIssues and complexitiesIssues and complexities

� Temporal Data support in ISO/ANSI SQL:2011Temporal Data support in ISO/ANSI SQL:2011Temporal Data support in ISO/ANSI SQL:2011Temporal Data support in ISO/ANSI SQL:2011

� Temporal Data support in DB2 10 for z/OSTemporal Data support in DB2 10 for z/OSTemporal Data support in DB2 10 for z/OSTemporal Data support in DB2 10 for z/OS◦ System-time period support

◦ Business-time period support

◦ Bi-temporal support

3333

A.Sironi 2012

� A Phone CallA Phone CallA Phone CallA Phone Call◦ Occurred (i.e. began) on some date and time

◦ Lasted for so much time

◦ Was registered by the system on some timestamp

◦ Never updated?

� Not time-varying!

◦ Values replaced?

� Likely not time-varying!

◦ Values updated?

� Maybe … time-varying…

4444

Page 3: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

3

A.Sironi 2012

� Products Products Products Products pppprice historyrice historyrice historyrice history◦ Valid on specific time interval

5555

PHONE_NR CUST_ID BUS_STA_DT BUS_END_DT SYS_STA_DT SYS_END_DT

02-444-111 C. Rossi 01.01.2000 31.12.9999 01.01.2000 24.08.2001

02-444-111 C. Rossi 01.01.2000 20.08.2001 24.08.2001 31.12.9999

02-444-111 A. Bianchi 20.08.2001 31.12.9999 24.08.2001 31.12.9999

PHONE_NR CUST_ID BUS_STA_DT BUS_END_DT SYS_STA_DT SYS_END_DT

02-444-111 C. Rossi 01.01.2000 31.12.9999 04.01.2000 31.12.9999

� Customer Phone Line(s) Customer Phone Line(s) Customer Phone Line(s) Customer Phone Line(s) (Telco DW 1999)(Telco DW 1999)(Telco DW 1999)(Telco DW 1999)◦ Valid on some time interval

� Modifications stored on the system at some later time

A.Sironi 2012

� Valid Time Valid Time Valid Time Valid Time (aka Application TimeApplication TimeApplication TimeApplication Time (SQL:2011) or Business Business Business Business Time Time Time Time (DB2))◦ Specifies when the facts are true with respect to the Real World◦ Useful for data that change over time and time information is relevant to applications and users◦ Time or Time Period

� Transaction Time Transaction Time Transaction Time Transaction Time (aka System Time System Time System Time System Time (DB2) or System-versioned tables (SQL:2011))◦ The time when the System becomes aware of the fact ◦ The information is safely stored on a system file/DB

� Business Time vs. System Time Business Time vs. System Time Business Time vs. System Time Business Time vs. System Time ◦ Mr. Brown lives in Rome since 23.11.1997 (Business Time)◦ Info supplied via mail on 12.12.2002 (Business Time)◦ Info registered by the System on 23.12.2002 (System Time)

6666

Page 4: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

4

A.Sironi 2012 7

� It has been estimated that one of every 50 It has been estimated that one of every 50 It has been estimated that one of every 50 It has been estimated that one of every 50 lines of database application code involves a lines of database application code involves a lines of database application code involves a lines of database application code involves a date or time valuedate or time valuedate or time valuedate or time value◦ Data Warehouse data are, by definition, time-

varying

◦ Becoming more and more popular also for OLTP applications

� Often the timeOften the timeOften the timeOften the time----oriented nature of the data is oriented nature of the data is oriented nature of the data is oriented nature of the data is what lends its valuewhat lends its valuewhat lends its valuewhat lends its value

� Overlaying simple concepts, such as Overlaying simple concepts, such as Overlaying simple concepts, such as Overlaying simple concepts, such as duplicate prevention, on timeduplicate prevention, on timeduplicate prevention, on timeduplicate prevention, on time----varying data varying data varying data varying data can be surprisingly subtle and complexcan be surprisingly subtle and complexcan be surprisingly subtle and complexcan be surprisingly subtle and complex

Cfr.[2]

A.Sironi 2012

� How do we identify duplicates?How do we identify duplicates?How do we identify duplicates?How do we identify duplicates?

8888

PHONE_NR CUST_ID BUS_STA_DT BUS_END_DT SYS_STA_DT SYS_END_DT

02-444-111 C. Rossi 01.01.2000 31.12.9999 01.01.2000 24.08.2001

02-444-111 C. Rossi 01.01.2000 20.08.2001 24.08.2001 31.12.9999

02-444-111 A. Bianchi 20.08.2001 31.12.9999 24.08.2001 31.12.9999

� Duplicates exist when

� Same Natural Key (Phone Nr) AND …

� Overlapping Business Time periods AND …

� Overlapping System Time periods

Page 5: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

5

A.Sironi 2012 9

� Temporal Models enforce Temporal Models enforce Temporal Models enforce Temporal Models enforce Temporal Temporal Temporal Temporal Functional DependenciesFunctional DependenciesFunctional DependenciesFunctional Dependencies

� If attributes depend on time, then time is If attributes depend on time, then time is If attributes depend on time, then time is If attributes depend on time, then time is Business Time or System TimeBusiness Time or System TimeBusiness Time or System TimeBusiness Time or System Time

A.Sironi 2012 10

� Need to track history of changes … Need to track history of changes … Need to track history of changes … Need to track history of changes … ANDANDANDAND …………

� Changes occur in the Changes occur in the Changes occur in the Changes occur in the realrealrealreal world long enough world long enough world long enough world long enough before (or after) they are captured by the system before (or after) they are captured by the system before (or after) they are captured by the system before (or after) they are captured by the system … … … …

ANDANDANDAND …………

� Need to avoid loosing any piece of informationNeed to avoid loosing any piece of informationNeed to avoid loosing any piece of informationNeed to avoid loosing any piece of information

◦ Re-build past situations

◦ Justify query result differences over time

Bi-Temporal support helpful when …

Page 6: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

6

A.Sironi 2012 11

A.Sironi 2012

� Inserting a new entity recordInserting a new entity recordInserting a new entity recordInserting a new entity record◦ Usual SQL Insert with proper timings

� Business Begin DateBusiness Begin DateBusiness Begin DateBusiness Begin Date: Past, Present or Future date/time when the “fact” described by the record begins being true. Current Date/Timestamp for Current Insertion.

� Business End DateBusiness End DateBusiness End DateBusiness End Date: (Past, Future or) Now, i.e. date/time when the “fact” described by the record becomes false. Usually Null or preferably “end of era” (es. “31.12.9999”), if undefined.

� System Begin DateSystem Begin DateSystem Begin DateSystem Begin Date: Current Date (or Current Timestamp)

� System End DateSystem End DateSystem End DateSystem End Date: Usually Null or preferably “end of era” (es. “31.12.9999”)

12121212

Page 7: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

7

A.Sironi 2012 13

Modifications: 5+ cases

Current StateState Modification

Resulting States1

Current StateState Modification

Resulting States

2

3

Current StateState Modification

Resulting States

A.Sironi 2012 14

Sequenced Modifications (Update/Inserts): 5+ casesSequenced Modifications (Update/Inserts): 5+ casesSequenced Modifications (Update/Inserts): 5+ casesSequenced Modifications (Update/Inserts): 5+ cases

Current StateState Modification

Resulting State4

Current StatesState Modification

Resulting States5

� + additional extensions of first 4 cases to multiple states

� Similarly for Deletes

Page 8: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

8

A.Sironi 2012 15

� Tracking a change of an existing record Tracking a change of an existing record Tracking a change of an existing record Tracking a change of an existing record requires (for the simplest case)requires (for the simplest case)requires (for the simplest case)requires (for the simplest case)◦ One SQL update operation

◦ Two SQL Insert operations

� More complex situations exist (e.g., More complex situations exist (e.g., More complex situations exist (e.g., More complex situations exist (e.g., delayed delayed delayed delayed andandandand outoutoutout----ofofofof----sequence)sequence)sequence)sequence)

� Additional complexitiesAdditional complexitiesAdditional complexitiesAdditional complexities◦ Entity & Referential Integrity

� When using Surrogate Keys

� When not using Surrogate Keys

◦ Vacuuming

� Removal of obsolete data

A.Sironi 2012 16

� TimeTimeTimeTime----variant data support is an inherent variant data support is an inherent variant data support is an inherent variant data support is an inherent characteristic of a Data Warehouse and many characteristic of a Data Warehouse and many characteristic of a Data Warehouse and many characteristic of a Data Warehouse and many OLTP applicationsOLTP applicationsOLTP applicationsOLTP applications

� Managing Managing Managing Managing timetimetimetime----varying data can be varying data can be varying data can be varying data can be surprisingly subtle and complexsurprisingly subtle and complexsurprisingly subtle and complexsurprisingly subtle and complex◦ Data modeling

◦ Application and query design & code

� It’s very important to choose the right level of It’s very important to choose the right level of It’s very important to choose the right level of It’s very important to choose the right level of support, avoiding unnecessary complexities, support, avoiding unnecessary complexities, support, avoiding unnecessary complexities, support, avoiding unnecessary complexities, when they don’t provide real business value when they don’t provide real business value when they don’t provide real business value when they don’t provide real business value

� Be aware!Be aware!Be aware!Be aware!

Page 9: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

9

A.Sironi 2012 17

A.Sironi 2012

� INCITS DM32.2 (formerly X3H2) is responsible for SQL INCITS DM32.2 (formerly X3H2) is responsible for SQL INCITS DM32.2 (formerly X3H2) is responsible for SQL INCITS DM32.2 (formerly X3H2) is responsible for SQL standard in US standard in US standard in US standard in US ◦ International Committee for Information Technology Standards is an ANSI (American National Standards Institute) accredited standards producing organization

� ISO/IEC JTC 1/SC 32 Data Management and ISO/IEC JTC 1/SC 32 Data Management and ISO/IEC JTC 1/SC 32 Data Management and ISO/IEC JTC 1/SC 32 Data Management and Interchange/WG 3 committee is responsible for Interchange/WG 3 committee is responsible for Interchange/WG 3 committee is responsible for Interchange/WG 3 committee is responsible for SQL SQL SQL SQL Standard Internationally Standard Internationally Standard Internationally Standard Internationally

� Typically Typically Typically Typically 3 to 5 year cycle 3 to 5 year cycle 3 to 5 year cycle 3 to 5 year cycle for each new version of the for each new version of the for each new version of the for each new version of the standardstandardstandardstandard

� SQL is a multiSQL is a multiSQL is a multiSQL is a multi----part standard which currently has 9 parts part standard which currently has 9 parts part standard which currently has 9 parts part standard which currently has 9 parts ◦ The highest part number is currently 14 (parts 5, 6, 7, 8 &12) were terminated

◦ Part 2: SQL/Foundation is the SQL language specification (biggest/most important part)

� 7 7 7 7 Versions of Standard SQL Versions of Standard SQL Versions of Standard SQL Versions of Standard SQL ◦ 86 (SQL-87), 89, 92(SQL2), SQL:1999 (SQL3), SQL:2003, SQL:2008, SQL:2011

18181818

Page 10: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

10

A.Sironi 2012

� X3H2 and X3H2 and X3H2 and X3H2 and WG 3 WG 3 WG 3 WG 3 approved approved approved approved work on a new part of SQL work on a new part of SQL work on a new part of SQL work on a new part of SQL standard called standard called standard called standard called SQL/Temporal (1995)SQL/Temporal (1995)SQL/Temporal (1995)SQL/Temporal (1995)

� US proposal largely US proposal largely US proposal largely US proposal largely based on the pioneering based on the pioneering based on the pioneering based on the pioneering work work work work of of of of Prof. Rick Snodgrass of Univ. of Arizona Prof. Rick Snodgrass of Univ. of Arizona Prof. Rick Snodgrass of Univ. of Arizona Prof. Rick Snodgrass of Univ. of Arizona ◦ The US proposal proved to be controversial at ISO

� UK competing UK competing UK competing UK competing proposal based on the work of Prof. proposal based on the work of Prof. proposal based on the work of Prof. proposal based on the work of Prof. Nikos Lorentzos of Univ. Nikos Lorentzos of Univ. Nikos Lorentzos of Univ. Nikos Lorentzos of Univ. AthensAthensAthensAthens, Greece , Greece , Greece , Greece ◦ US disagreed with the ISO comments on the US proposal

◦ The US also did not see the need for the UK proposal

� ANSI ANSI ANSI ANSI and ISO decided to defer further work on and ISO decided to defer further work on and ISO decided to defer further work on and ISO decided to defer further work on SQL/Temporal SQL/Temporal SQL/Temporal SQL/Temporal until until until until SQL:99 was published SQL:99 was published SQL:99 was published SQL:99 was published ◦ After the publication of SQL:99, neither US nor UK brought in any new proposals to resolve the differences

� Because Because Because Because of inactivity, of inactivity, of inactivity, of inactivity, SQL/Temporal canceled in 2001SQL/Temporal canceled in 2001SQL/Temporal canceled in 2001SQL/Temporal canceled in 2001

19191919

A.Sironi 2012

� INCITS DM32.2 and ISO/IEC JTC1 SC32 WG3 INCITS DM32.2 and ISO/IEC JTC1 SC32 WG3 INCITS DM32.2 and ISO/IEC JTC1 SC32 WG3 INCITS DM32.2 and ISO/IEC JTC1 SC32 WG3 accepted accepted accepted accepted a a a a proposal on "systemproposal on "systemproposal on "systemproposal on "system----versioned tables" versioned tables" versioned tables" versioned tables" (T180)(T180)(T180)(T180)◦ Not a new part of the standard

◦ Temporal extensions added to SQL/Foundation

� Another temporal feature was added in 2010 in the Another temporal feature was added in 2010 in the Another temporal feature was added in 2010 in the Another temporal feature was added in 2010 in the form form form form of "applicationof "applicationof "applicationof "application----time period tablestime period tablestime period tablestime period tables” (T181)” (T181)” (T181)” (T181)

� Both Both Both Both “system“system“system“system----versioned tables” versioned tables” versioned tables” versioned tables” and and and and “application“application“application“application----time time time time period period period period tables” tables” tables” tables” are now part are now part are now part are now part of the new version of the new version of the new version of the new version of the of the of the of the SQL standard (SQL:2011SQL standard (SQL:2011SQL standard (SQL:2011SQL standard (SQL:2011))))◦ The proposal is largely inspired by the original one

◦ The syntax is quite different

20202020

Page 11: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

11

A.Sironi 2012

� Temporal data is data which changes over time Temporal data is data which changes over time Temporal data is data which changes over time Temporal data is data which changes over time ◦ An account interest rate changes over time

� Referred to as valid time dimension, real world perspective or business perspective

� SQL:2011 calls this application time

◦ The value we have in a database for a company’s credit rating changes over time � This may be different than application time due to timing differences (e.g. delays or advance registration)

� This may have nothing to do with the application time perspective (for example corrections)

� Referred to as transaction time dimension or database perspective

� SQL:2011 calls this system time (or system versioning)

� BiBiBiBi----temporal temporal temporal temporal data is data which changes over data is data which changes over data is data which changes over data is data which changes over two two two two dimensions of time independently dimensions of time independently dimensions of time independently dimensions of time independently ◦ SQL:2011 calls this application time with system versioning

21212121

A.Sironi 2012

� ApplicationApplicationApplicationApplication----time period tables are tables that time period tables are tables that time period tables are tables that time period tables are tables that contain contain contain contain a PERIOD a PERIOD a PERIOD a PERIOD clause (newlyclause (newlyclause (newlyclause (newly----introduced) with an introduced) with an introduced) with an introduced) with an useruseruseruser----defined period namedefined period namedefined period namedefined period name◦ Currently restricted to temporal periods only; may be relaxed in the future

� ApplicationApplicationApplicationApplication----time time time time period tables must contain two period tables must contain two period tables must contain two period tables must contain two additional columns to storeadditional columns to storeadditional columns to storeadditional columns to store◦ The start time of the period associated with the row ◦ The end time of the period◦ Values of both start and end columns are set by the users

� Additional Additional Additional Additional syntax is provided for users to specify syntax is provided for users to specify syntax is provided for users to specify syntax is provided for users to specify primary key/unique primary key/unique primary key/unique primary key/unique constraints that ensure no two constraints that ensure no two constraints that ensure no two constraints that ensure no two rows with rows with rows with rows with the same the same the same the same key value have overlapping key value have overlapping key value have overlapping key value have overlapping periodsperiodsperiodsperiods

22222222

Page 12: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

12

A.Sironi 2012

� Additional syntax is provided for users to Additional syntax is provided for users to Additional syntax is provided for users to Additional syntax is provided for users to specify specify specify specify referential constraints referential constraints referential constraints referential constraints ◦ the period of every child row must be completely contained in the period of exactly one parent row or in the combined period of two or more consecutive parent rows

� QueriesQueriesQueriesQueries, inserts, updates and deletes on , inserts, updates and deletes on , inserts, updates and deletes on , inserts, updates and deletes on applicationapplicationapplicationapplication----time period time period time period time period tables behave exactly tables behave exactly tables behave exactly tables behave exactly like queries, inserts, like queries, inserts, like queries, inserts, like queries, inserts, updates and updates and updates and updates and deletes on deletes on deletes on deletes on regular regular regular regular tablestablestablestables

� Additional Additional Additional Additional syntax is provided syntax is provided syntax is provided syntax is provided for updating and for updating and for updating and for updating and deleting over an arbitrary period of timedeleting over an arbitrary period of timedeleting over an arbitrary period of timedeleting over an arbitrary period of time

23232323

A.Sironi 2012

CREATE TABLE employees

(emp_name VARCHAR(50) NOT NULL,

dept_id VARCHAR(10),

start_date DATE NOT NULL,

end_date DATE NOT NULL,

PERIOD FOR emp_period (start_date,

end_date),

PRIMARY KEY (emp_name, emp_period

WITHOUT OVERLAPS),

FOREIGN KEY (dept_id, PERIOD emp_period)

REFERENCES departments (dept_id,

PERIOD dept_period));

24242424

Page 13: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

13

A.Sironi 2012

� SystemSystemSystemSystem----versioned tables are tables that contain a versioned tables are tables that contain a versioned tables are tables that contain a versioned tables are tables that contain a PERIOD PERIOD PERIOD PERIOD clause with clause with clause with clause with a prea prea prea pre----defined period name defined period name defined period name defined period name (SYSTEM_TIME) and specify (SYSTEM_TIME) and specify (SYSTEM_TIME) and specify (SYSTEM_TIME) and specify WITH SYSTEM VERSIONINGWITH SYSTEM VERSIONINGWITH SYSTEM VERSIONINGWITH SYSTEM VERSIONING

� SystemSystemSystemSystem----versioned versioned versioned versioned tables must contain two additional tables must contain two additional tables must contain two additional tables must contain two additional columnscolumnscolumnscolumns◦ One to store the start time of the SYSTEM_TIME period ◦ One to store the end time of the SYSTEM_TIME period ◦ Values of both start and end columns are set by the system◦ Users are not allowed to supply values for these columns

� Unlike Unlike Unlike Unlike regular tables, systemregular tables, systemregular tables, systemregular tables, system----versioned tables versioned tables versioned tables versioned tables preserve the preserve the preserve the preserve the old versions old versions old versions old versions of rows as the table is of rows as the table is of rows as the table is of rows as the table is updatedupdatedupdatedupdated

� Rows Rows Rows Rows whose periods intersect the current time are whose periods intersect the current time are whose periods intersect the current time are whose periods intersect the current time are called called called called current system rowscurrent system rowscurrent system rowscurrent system rows◦ Only current system rows can be updated or deleted◦ All constraints are enforced on current system rows only

� All All All All others are called historical system others are called historical system others are called historical system others are called historical system rowsrowsrowsrows

25252525

A.Sironi 2012

CREATE TABLE employees

(emp_name VARCHAR(50) NOT NULL,

dept_id VARCHAR(10),

system_start TIMESTAMP(6) GENERATED

ALWAYS AS ROW START,

system_end TIMESTAMP(6) GENERATED

ALWAYS AS ROW END,

PERIOD FOR SYSTEM_TIME (system_start,

system_end),

PRIMARY KEY (emp_name),

FOREIGN KEY (dept_id) REFERENCES

departments (dept_id);

) WITH SYSTEM VERSIONING;

26262626

Page 14: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

14

A.Sironi 2012

� Notes:Notes:Notes:Notes:1.Temporal support when time period(s)time period(s)time period(s)time period(s) involved

� i.e. value(s) change over time

� Each period limited by two timestamps

2.Basic implementation is Row-based

27272727

A.Sironi 2012 28

Page 15: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

15

A.Sironi 2012

� TableTableTableTable----level specification to control data level specification to control data level specification to control data level specification to control data management based upon timemanagement based upon timemanagement based upon timemanagement based upon time

� Two notions of timeTwo notions of timeTwo notions of timeTwo notions of time◦ Business time

� Notes the occurrence of a real world event or business decision related to the present time, the past or the future

� Useful for tracking business-relevant events over time

◦ System time

� Notes the occurrence of a data base change

� Useful for auditing & compliance

� New syntax in FROM clause to specify a time New syntax in FROM clause to specify a time New syntax in FROM clause to specify a time New syntax in FROM clause to specify a time criteriacriteriacriteriacriteria

� Application logic greatly simplifiedApplication logic greatly simplifiedApplication logic greatly simplifiedApplication logic greatly simplified

29292929

A.Sironi 2012

� Requirements addressed Requirements addressed Requirements addressed Requirements addressed ◦ Managing multiple versions of relevant data◦ Auditing

� Before DB2 V10Before DB2 V10Before DB2 V10Before DB2 V10◦ Supported by adding one or two Date / Timestamp columns, like e.g. LOAD_TIME, INSERT_TIME, UPDATE_TIME or similar names◦ Usually, only provides the latest values of table columns� Record before-image lost when updating in-place

� With DB2 10With DB2 10With DB2 10With DB2 10◦ System Time supported by � New CREATE/ALTER TABLE options

� New DML constructs

◦ Before-image preserved by DB2

30303030

Page 16: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

16

A.Sironi 2012

� Define table(s) with an associated System Time Define table(s) with an associated System Time Define table(s) with an associated System Time Define table(s) with an associated System Time Period & Transaction timestampPeriod & Transaction timestampPeriod & Transaction timestampPeriod & Transaction timestamp◦ System Period indicates starting & ending points of a time interval� DB2 time intervals are (inclusive, exclusive)

� Timestamp(12) columns used

� GENERATED ALWAYS

◦ Transaction timestamp used internally by DB2◦ User cannot modify values of Period & Trx. timestamp

� Define, for each temporal table, an associated Define, for each temporal table, an associated Define, for each temporal table, an associated Define, for each temporal table, an associated History TableHistory TableHistory TableHistory Table◦ Old versions of rows saved into it◦ Applications and users don’t need to be aware� Transparent access managed by DB2 as required in support of temporal queries

31313131

A.Sironi 2012

� Create a SystemCreate a SystemCreate a SystemCreate a System----period temporal tableperiod temporal tableperiod temporal tableperiod temporal tableCREATE TABLE SYS_INTEREST_RATE

( TYPE CHAR(6) NOT NULL

, CUST_SEGMENT CHAR(12) NOT NULL

, INTEREST_RATE DECIMAL(5,2) NOT NULL

, SYS_BEGIN TIMESTAMP(12) NOT NULL

GENERATED AS ROW BEGIN

, SYS_END TIMESTAMP(12) NOT NULL

GENERATED AS ROW END

, PERIOD SYSTEM_TIME(SYS_BEGIN, SYS_END)

, TRANS_ID TIMESTAMP(12) NOT NULL

GENERATED ALWAYS AS TRANSACTION START ID );

� Create the History Create the History Create the History Create the History tabletabletabletableCREATE TABLE SYS_INTEREST_RATE_HISTORY LIKE

SYS_INTEREST_RATE;

� Enable systemEnable systemEnable systemEnable system----period data versioningperiod data versioningperiod data versioningperiod data versioningALTER TABLE SYS_INTEREST_RATE ADD VERSIONING

USE HISTORY TABLE SYS_INTEREST_RATE_HISTORY;

32323232

Page 17: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

17

A.Sironi 2012

� Insert a couple of rows Insert a couple of rows Insert a couple of rows Insert a couple of rows

� INSERT INTO SYS_INTEREST_RATE

("TYPE", CUST_SEGMENT, INTEREST_RATE)

VALUES ('DEBIT', 'VIP', 5.32) ;

� INSERT INTO SYS_INTEREST_RATE

("TYPE", CUST_SEGMENT, INTEREST_RATE)

VALUES ('CREDIT', 'VIP', 1.03) ;

� SYS_INTEREST_RATE contentSYS_INTEREST_RATE contentSYS_INTEREST_RATE contentSYS_INTEREST_RATE content

� SYS_INTEREST_RATE_HISTORY is emptySYS_INTEREST_RATE_HISTORY is emptySYS_INTEREST_RATE_HISTORY is emptySYS_INTEREST_RATE_HISTORY is empty

Note: Note: Note: Note: APAR PM31314 has modified the row-end maximum value to ‘9999-

12-30-00.00.00.000000’ in accordance with java.sql.Timestamp which is based on java.util.Date (where hours have values from 0 to 23).

33333333

A.Sironi 2012

� Table content BEFORE (current & history)Table content BEFORE (current & history)Table content BEFORE (current & history)Table content BEFORE (current & history)

34343434

CURRENT

CURRENT

CURRENT

CURRENT

HISTORY

HISTORY

HISTORY

HISTORY

� Let’s UPDATE the interest rateLet’s UPDATE the interest rateLet’s UPDATE the interest rateLet’s UPDATE the interest rateUPDATE SYS_INTEREST_RATE

SET INTEREST_RATE = INTEREST_RATE + 0.12

WHERE TYPE = 'CREDIT'

AND CUST_SEGMENT = 'VIP';

� Table content AFTER (current & history)Table content AFTER (current & history)Table content AFTER (current & history)Table content AFTER (current & history)

CURRENT

CURRENT

CURRENT

CURRENT

HISTORY

HISTORY

HISTORY

HISTORY

Page 18: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

18

A.Sironi 2012

� Table content BEFORE (current & history)Table content BEFORE (current & history)Table content BEFORE (current & history)Table content BEFORE (current & history)

35353535

CURRENT

CURRENT

CURRENT

CURRENT

HISTORY

HISTORY

HISTORY

HISTORY

� Let’s DELETE some record(s)Let’s DELETE some record(s)Let’s DELETE some record(s)Let’s DELETE some record(s)DELETE FROM SYS_INTEREST_RATE

WHERE TYPE = ‘DEBIT'

AND CUST_SEGMENT = 'VIP';

� Table content AFTER (current & history)Table content AFTER (current & history)Table content AFTER (current & history)Table content AFTER (current & history)

CURRENT

CURRENT

CURRENT

CURRENT

HISTORY

HISTORY

HISTORY

HISTORY

A.Sironi 2012

� Table contentTable contentTable contentTable content

36363636

� Let’s change the way we query…Let’s change the way we query…Let’s change the way we query…Let’s change the way we query…◦ What were the interest rates for VIP customers at some time?

SELECT *

FROM SYS_INTEREST_RATE

FOR SYSTEM_TIME AS OF

TIMESTAMP '2011-09-21-16.00.00.000000'

WHERE CUST_SEGMENT = 'VIP'

� Query result setQuery result setQuery result setQuery result set

CURRENT

CURRENT

CURRENT

CURRENT

HISTORY

HISTORY

HISTORY

HISTORY

Page 19: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

19

A.Sironi 2012

� Table contentTable contentTable contentTable content

37373737

� Let’s change the way we query…Let’s change the way we query…Let’s change the way we query…Let’s change the way we query…◦ What were the interest rates for VIP customers at some time?

SELECT * FROM SYS_INTEREST_RATE

FOR SYSTEM_TIME FROM '2011-09-21-00.00.00.000000‘

TO CURRENT TIMESTAMP

WHERE CUST_SEGMENT = 'VIP' ;

� Query result setQuery result setQuery result setQuery result set

CURRENT

CURRENT

CURRENT

CURRENT

HISTORY

HISTORY

HISTORY

HISTORY

A.Sironi 2012

� Create a BusinessCreate a BusinessCreate a BusinessCreate a Business----period temporal tableperiod temporal tableperiod temporal tableperiod temporal table

CREATE TABLE BUS_INTEREST_RATE

( TYPE CHAR(6) NOT NULL

, CUST_SEGMENT CHAR(12) NOT NULL

, INTEREST_RATE DECIMAL(5,2) NOT NULL

, BUS_BEGIN_DATE DATE NOT NULL

, BUS_END_DATE DATE NOT NULL

, PERIOD BUSINESS_TIME

(BUS_BEGIN_DATE, BUS_END_DATE)

, PRIMARY KEY(TYPE, CUST_SEGMENT,

BUSINESS_TIME WITHOUT OVERLAPS) );

� Primary Key definitionPrimary Key definitionPrimary Key definitionPrimary Key definition

38383838

Page 20: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

20

A.Sironi 2012

� Insert a couple of rows Insert a couple of rows Insert a couple of rows Insert a couple of rows

� INSERT INTO BUS_INTEREST_RATE

("TYPE", CUST_SEGMENT, INTEREST_RATE,

BUS_BEGIN_DATE, BUS_END_DATE)

VALUES ('DEBIT', 'VIP', 5.32,

'18.10.2011', '31.12.9999') ;

� INSERT INTO BUS_INTEREST_RATE

("TYPE", CUST_SEGMENT, INTEREST_RATE,

BUS_BEGIN_DATE, BUS_EN)

VALUES ('CREDIT', 'VIP', 1.03,

'18.10.2011', '31.12.9999') ;

� BUS_INTEREST_RATE contentBUS_INTEREST_RATE contentBUS_INTEREST_RATE contentBUS_INTEREST_RATE content

39393939

A.Sironi 2012

� Table content BEFORETable content BEFORETable content BEFORETable content BEFORE

40404040

� Let’s UPDATE the interest rateLet’s UPDATE the interest rateLet’s UPDATE the interest rateLet’s UPDATE the interest rateUPDATE BUS_INTEREST_RATE

FOR PORTION OF BUSINESS_TIME FROM DATE '11.10.2012'

TO DATE '31.12.9999'

SET INTEREST_RATE = INTEREST_RATE + 0.12

WHERE TYPE = 'CREDIT' ;

� Table Table Table Table content AFTER content AFTER content AFTER content AFTER

Page 21: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

21

A.Sironi 2012

� Table content BEFORETable content BEFORETable content BEFORETable content BEFORE

41414141

� Let’s UPDATE again some record(s)Let’s UPDATE again some record(s)Let’s UPDATE again some record(s)Let’s UPDATE again some record(s)

UPDATE BUS_INTEREST_RATE

FOR PORTION OF BUSINESS_TIME

FROM DATE '11.08.2012‘ TO DATE '31.12.2012'

SET INTEREST_RATE = 1.27

WHERE TYPE = 'CREDIT' ;

� Table content Table content Table content Table content AFTERAFTERAFTERAFTER

A.Sironi 2012

� Table contentTable contentTable contentTable content

42424242

� Let’s change the way we query…Let’s change the way we query…Let’s change the way we query…Let’s change the way we query…◦ What the interest rate(s) for VIP customers at some time?

SELECT * FROM BUS_INTEREST_RATE

FOR BUSINESS_TIME AS OF DATE'2011-12-21'

WHERE CUST_SEGMENT = 'VIP‘;

� Query result setQuery result setQuery result setQuery result set

Page 22: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

22

A.Sironi 2012

� Let’s DELETE some record(s)Let’s DELETE some record(s)Let’s DELETE some record(s)Let’s DELETE some record(s)

DELETE FROM BUS_INTEREST_RATE

FOR PORTION OF BUSINESS_TIME

FROM DATE '11.07.2012‘ AND DATE '11.11.2012'

WHERE TYPE = 'CREDIT' ;

� Table content Table content Table content Table content AFTERAFTERAFTERAFTER

� Table content BEFORETable content BEFORETable content BEFORETable content BEFORE

43434343

A.Sironi 2012

� Create a SystemCreate a SystemCreate a SystemCreate a System----period temporal tableperiod temporal tableperiod temporal tableperiod temporal table

CREATE TABLE INTEREST_RATE

( TYPE CHAR(6) NOT NULL

, CUST_SEGMENT CHAR(12) NOT NULL

, INTEREST_RATE DECIMAL(5,2) NOT NULL

, BUS_START_DATE DATE NOT NULL

, BUS_END_DATE DATE NOT NULL

, PERIOD BUSINESS_TIME ( BUS_START_DATE

, BUS_END_DATE )

, SYS_BEGIN TIMESTAMP(12) NOT NULL

GENERATED AS ROW BEGIN

, SYS_END TIMESTAMP(12) NOT NULL

GENERATED AS ROW END

, PERIOD SYSTEM_TIME( SYS_BEGIN, SYS_END )

, TRANS_ID TIMESTAMP(12) NOT NULL

GENERATED ALWAYS AS TRANSACTION START ID );

44444444

Page 23: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

23

A.Sironi 2012

� Create the History tableCreate the History tableCreate the History tableCreate the History table

CREATE TABLE INTEREST_RATE_HISTORY

( TYPE CHAR(6) NOT NULL

, CUST_SEGMENT CHAR(12) NOT NULL

, INTEREST_RATE DECIMAL(5,2) NOT NULL

, BUS_START_DATE DATE NOT NULL

, BUS_END_DATE DATE NOT NULL

, SYS_BEGIN TIMESTAMP(12) NOT NULL

, SYS_END TIMESTAMP(12) NOT NULL

, TRANS_ID TIMESTAMP(12) NOT NULL

);

� or, more simply or, more simply or, more simply or, more simply ........

CREATE TABLE INTEREST_RATE_HISTORY

LIKE INTEREST_RATE;

45454545

A.Sironi 2012

� Enable systemEnable systemEnable systemEnable system----period data period data period data period data versioningversioningversioningversioning

ALTER TABLE INTEREST_RATE

ADD VERSIONING USE HISTORY TABLE

INTEREST_RATE_HISTORY;

� Define BusinessDefine BusinessDefine BusinessDefine Business----time without overlapstime without overlapstime without overlapstime without overlaps

ALTER TABLE INTEREST_RATE

ADD UNIQUE (TYPE, CUST_SEGMENT,

BUSINESS_TIME WITHOUT OVERLAPS);

46464646

Page 24: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

24

A.Sironi 2012

� Enable systemEnable systemEnable systemEnable system----period data period data period data period data versioningversioningversioningversioning

ALTER TABLE INTEREST_RATE

ADD VERSIONING

USE HISTORY TABLE INTEREST_RATE_HISTORY;

� Define BusinessDefine BusinessDefine BusinessDefine Business----time without overlapstime without overlapstime without overlapstime without overlapsALTER TABLE INTEREST_RATE ADD UNIQUE

(TYPE, CUST_SEGMENT, BUSINESS_TIME WITHOUT OVERLAPS);

� Insert a couple of rows Insert a couple of rows Insert a couple of rows Insert a couple of rows

◦ date/time: 2011-10-11 17.19.04

INSERT INTO PRIM893.INTEREST_RATE ("TYPE", CUST_SEGMENT,

INTEREST_RATE, BUS_START_DATE, BUS_END_DATE)

VALUES (‘DEBIT', 'VIP', 5.32, DATE '11.08.2011‘

, DATE '31.12.9999') ;

INSERT INTO PRIM893.INTEREST_RATE ("TYPE", CUST_SEGMENT,

INTEREST_RATE, BUS_START_DATE, BUS_END_DATE)

VALUES ('CREDIT', 'VIP', 1.03, DATE '11.08.2011‘

, DATE '31.12.9999') ;

47474747

A.Sironi 2012

SELECT *

FROM INTEREST_RATE

FOR SYSTEM_TIME AS OF CURRENT TIMESTAMP

FOR BUSINESS_TIME AS OF '31.08.2011'

ORDER BY 1;

48484848

NoteNoteNoteNote: SYS_BEGIN, SYS_END and TRANS_ID Timestamp values truncated for reason of space

Page 25: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

25

A.Sironi 2012 49494949

� Update CREDIT interest backwardUpdate CREDIT interest backwardUpdate CREDIT interest backwardUpdate CREDIT interest backward----inininin----timetimetimetime

◦ Date/time: 2011-10-15-18.13.19

UPDATE INTEREST_RATE FOR PORTION OF BUSINESS_TIME

FROM '01.09.2011' TO '31.12.9999'

SET INTEREST_RATE = 1.99

WHERE TYPE = 'CREDIT'

AND CUST_SEGMENT = 'VIP' ;

� Valuable requests to the SystemValuable requests to the SystemValuable requests to the SystemValuable requests to the System

◦ According to your knowledge on 14 October 2011, which was the Credit Interest for VIP customers on that date (or any other date)?

◦ According to your current (16 October 2011) knowledge, which was the Credit Interest for VIP customer on that date (or any other date)?

A.Sironi 2012

[1] SG24-7892 DB2 DB2 DB2 DB2 10 for z/OS 10 for z/OS 10 for z/OS 10 for z/OS Technical OverviewTechnical OverviewTechnical OverviewTechnical Overview, December 2010

[2] Richard T. Snodgrass, Managing Managing Managing Managing Temporal Temporal Temporal Temporal Data Data Data Data ---- A A A A FiveFiveFiveFive----Part Part Part Part SeriesSeriesSeriesSeries, TimeCenter TR 28, September, 1998

[3] Craig Baumunk, An An An An Overview of Temporal Features in Overview of Temporal Features in Overview of Temporal Features in Overview of Temporal Features in SQL:2011SQL:2011SQL:2011SQL:2011, January 4, 2012

[4] C.J. Date , Hugh Darwen , Nikos Lorentzos, , , , TemporalTemporalTemporalTemporal Data & Data & Data & Data & the the the the RelationalRelationalRelationalRelational Model, Model, Model, Model, The Morgan Kaufmann Series in Data Management Systems, December 3, 2002

[5] Krishna Kulkarni, Temporal Features in SQL standardTemporal Features in SQL standardTemporal Features in SQL standardTemporal Features in SQL standard, IBM Corporation, May 13, 2011

50505050

Page 26: SQL Temporal Support V1.3 - dugi.molaro.bedugi.molaro.be/wp-content/uploads/2009/10/SQL-Temporal-Support-… · 3/13/2012 2 A.Sironi2012 TimeTime- ---varying datavarying data Type

3/13/2012

26

A.Sironi 2012 51515151

The EndThe EndThe EndThe End


Recommended