SlideShare a Scribd company logo
Text 
Tracking your Data Across the Fourth Dimension 
Wellington Waterloo Web Makers Meetup
“In physics, spacetime is any mathematical model 
that combines space and time into a single 
interwoven continuum.” 
–Wikipedia
Databases are Good at 
‘Now’ 
CRUD 
Create data 
Read data 
Update data 
Delete data
Bread and Butter Queries 
How many people work 
in department X? 
What’s the total amount 
paid in commissions by 
department? 
What was the top selling 
product in the last year?
The Fourth Dimension… 
Compare reporting 
relationships one month ago, 
today and in one months time 
Show me all changes made 
to this data over time 
As of one month ago, what 
did I believe to be the truth 
about this data yesterday and 
what did I actually believe 
was the truth yesterday?
“A temporal database is a database with built-in 
support for handling data involving time…” 
–Wikipedia 
”
A Little History… 
Need for temporal support identified as early as 1992 
Initial attempts to get support for this into the SQL 
standard were rejected 
Finally made it into the SQL:2011 standard
Some Sample Data 
Owner Property 
Jeremy ‘Some place’
What forms of temporal data 
are usually stored?
Temporal Aspects 
Decision Time 
Valid Time 
Transaction Time 
A table that implements one of these is temporal, two 
is bi-temporal, more is multi-temporal
Decision Time 
Records when a decision was taken 
Stored as a timestamp 
You may be doing this already…
Decision Time Example 
Owner Property Decision Time 
Jeremy ‘Some place’ 2012-09-28 
Adam ‘Some place’ 2014-02-21
Valid Time 
The time during which a fact is true with respect to the 
real world 
Modelled as a range between two timestamps 
Closed at lower bound, can be open at upper bound
Valid Time Example 
Owner Property StartVT EndVT 
Jeremy ‘Some place’ 2012-09-28 2014-02-20 
Adam ‘Some place’ 2014-02-21 ∞
Valid Time Example 
Owner Property StartVT EndVT 
Jeremy ‘Some place’ 2012-09-28 2014-03-20 
Adam ‘Some place’ 2014-03-21 ∞
Transaction Time 
The time period during which a fact stored in the 
database is considered to be true 
Modelled as a range between two timestamps 
Closed at lower bound, can be open at upper bound
Transaction Time Example 
Owner Property StartVT EndVT StartTT EndTT 
Jeremy ‘Some 
place’ 
2012-09-2 
8 
2014-02-2 
0 
2012-09-2 
8 
2014-09-0 
9 
Adam ‘Some 
place’ 
2014-02-2 
1 ∞ 2014-02-2 
1 
2014-09-0 
9 
Jeremy ‘Some 
place’ 
2012-09-2 
8 
2014-03-2 
0 
2014-09-0 
9 ∞ 
Adam ‘Some 
place’ 
2014-03-2 
1 ∞ 2014-09-0 
9 ∞
Valid Time and Transaction 
Time are Orthogonal 
Valid Time 
Transaction Time
Enough theory! How do I add 
this stuff to my db schema?
SQL:2011 Temporal 
Additions 
! 
PERIOD type for date ranges in table definitions 
Can query for data in a period using new predicates 
CONTAINS, OVERLAPS, EQUALS, PRECEDES and 
IMMEDIATELY SUCCEEDS 
Can also update or delete data matching a portion of a 
period using FOR PORTION OF {period_name} FROM 
{date} TO {date}
Implementing Valid Time 
Done by adding columns for start and end with a 
period constraint on the columns 
Period must also be included as part of the primary key
Table Definition using Valid 
Time 
CREATE TABLE Emp( 
ENo INTEGER, 
StartVT DATE, 
EndVT DATE, 
EDept INTEGER, 
PERIOD FOR EValidTime (StartVT, EndVT), 
PRIMARY KEY (ENo, EValidTime WITHOUT OVERLAPS), 
)
Foreign Key Problems… 
It’s possible that rows in child tables may reference 
rows in parent tables that are not currently valid and 
vice versa 
Therefore foreign key constraints have to be updated 
where needed to include period data
Valid time foreign key 
definition 
ALTER TABLE Emp 
ADD FOREIGN KEY (Edept, PERIOD EValidTime) 
REFERENCES Dept (DNo, PERIOD DValidTime)
Querying a Table using Valid 
Time 
SELECT Ename, Edept 
FROM Emp 
WHERE ENo = 22217 AND 
EValidTime CONTAINS DATE ‘2011-01-02’; 
! 
SELECT Ename, Edept 
FROM Emp 
WHERE ENo = 22217 AND 
EValidTime OVERLAPS 
PERIOD (DATE ‘2010-01-01', DATE ‘2011-01-01');
Implementing Transaction 
Time 
RDBMS automatically creates a snapshot when data is 
updated or deleted 
Done by adding columns for start, end and a period for 
them 
Must also add WITH SYSTEM VERSIONING to create 
table statement
Table Definition using 
Transaction Time 
CREATE TABLE Emp 
ENo INTEGER, 
Sys_start TIMESTAMP(12) GENERATED 
ALWAYS AS ROW START, 
Sys_end TIMESTAMP(12) GENERATED 
ALWAYS AS ROW END, 
EName VARCHAR(30), 
PERIOD FOR SYSTEM_TIME (Sys_start, Sys_end) 
) WITH SYSTEM VERSIONING
Querying a Table using 
Transaction Time 
SELECT ENo,EName,Sys_Start,Sys_End 
FROM Emp FOR SYSTEM_TIME AS OF 
TIMESTAMP '2011-01-02 00:00:00’; 
! 
SELECT ENo,EName,Sys_Start,Sys_End 
FROM Emp FOR SYSTEM_TIME FROM 
TIMESTAMP '2011-01-02 00:00:00’TO 
TIMESTAMP '2011-12-31 00:00:00’;
Bi-Temporal table definition 
CREATE TABLE Emp( 
ENo INTEGER, 
StartVT DATE, 
EndVT DATE, 
EDept INTEGER, 
PERIOD FOR EValidTime (StartVT, EndVT), 
Sys_start TIMESTAMP(12) GENERATED ALWAYS AS ROW START, 
Sys_end TIMESTAMP(12) GENERATED ALWAYS AS ROW END, 
EName VARCHAR(30), 
PERIOD FOR SYSTEM_TIME (Sys_start, Sys_end), 
PRIMARY KEY (ENo, EValidTime WITHOUT OVERLAPS), 
FOREIGN KEY (Edept, PERIOD EValidTime) 
REFERENCES Dept (DNo, PERIOD DValidTime) 
) WITH SYSTEM VERSIONING
Who supports SQL:2011 
Temporal? 
IBM DB2. Called “Time Travel Queries”, although 
different syntax for FOR SYSTEM_TIME AS OF 
Oracle 12c, in compliance with SQL:2011 
Others?
Further Reading 
‘Developing Time Oriented Applications in SQL’ by 
Richard Snodgrass 
‘Temporal Features in SQL:2011’ 
‘Time and Relational Theory: Temporal Databases in 
the Relational Model and SQL'
Thanks for listening 
Any questions? 
Contact me: 
@JCook21 
jeremycook0@gmail.com 
I’d love some feedback!

More Related Content

Similar to Track your data across the fourth dimension

My Portfolio
My PortfolioMy Portfolio
My Portfolio
dmcglasson
 
Temporal Case Management 1998
Temporal Case Management  1998Temporal Case Management  1998
Temporal Case Management 1998
David Tryon
 
Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8
Fulvio Corno
 
Temporal
TemporalTemporal
Temporal
sunsie
 
Temporal databases
Temporal databasesTemporal databases
Temporal databases
Dabbal Singh Mahara
 
Dublin Meetup: Cassandra anti patterns
Dublin Meetup: Cassandra anti patternsDublin Meetup: Cassandra anti patterns
Dublin Meetup: Cassandra anti patterns
Christopher Batey
 
AWS Office Hours: Amazon Elastic MapReduce
AWS Office Hours: Amazon Elastic MapReduce AWS Office Hours: Amazon Elastic MapReduce
AWS Office Hours: Amazon Elastic MapReduce
Amazon Web Services
 
TSQL Functions (SQL Server)
TSQL Functions (SQL Server)TSQL Functions (SQL Server)
TSQL Functions (SQL Server)
Steve Stedman
 
Chris Seebacher Portfolio
Chris Seebacher PortfolioChris Seebacher Portfolio
Chris Seebacher Portfolio
guest3ea163
 
My2dw
My2dwMy2dw
My2dw
ketan533
 
Buenos Aires Drools Expert Presentation
Buenos Aires Drools Expert PresentationBuenos Aires Drools Expert Presentation
Buenos Aires Drools Expert Presentation
Mark Proctor
 
Do You Have the Time
Do You Have the TimeDo You Have the Time
Do You Have the Time
Michael Antonovich
 
Best Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop Professionals
Best Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop ProfessionalsBest Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop Professionals
Best Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop Professionals
Cloudera, Inc.
 
Speedment & Sencha at Oracle Open World 2015
Speedment & Sencha at Oracle Open World 2015Speedment & Sencha at Oracle Open World 2015
Speedment & Sencha at Oracle Open World 2015
Speedment, Inc.
 
Re-Engineering PostgreSQL as a Time-Series Database
Re-Engineering PostgreSQL as a Time-Series DatabaseRe-Engineering PostgreSQL as a Time-Series Database
Re-Engineering PostgreSQL as a Time-Series Database
All Things Open
 
Data Warehousing and Data Mining
Data Warehousing and Data MiningData Warehousing and Data Mining
Data Warehousing and Data Mining
idnats
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris Seebacher
 

Similar to Track your data across the fourth dimension (20)

ITReady DW Day2
ITReady DW Day2ITReady DW Day2
ITReady DW Day2
 
My Portfolio
My PortfolioMy Portfolio
My Portfolio
 
My Portfolio
My PortfolioMy Portfolio
My Portfolio
 
Temporal Case Management 1998
Temporal Case Management  1998Temporal Case Management  1998
Temporal Case Management 1998
 
Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8Dates and Times in Java 7 and Java 8
Dates and Times in Java 7 and Java 8
 
Temporal
TemporalTemporal
Temporal
 
Temporal databases
Temporal databasesTemporal databases
Temporal databases
 
Dublin Meetup: Cassandra anti patterns
Dublin Meetup: Cassandra anti patternsDublin Meetup: Cassandra anti patterns
Dublin Meetup: Cassandra anti patterns
 
JBoss World 2011 - Drools
JBoss World 2011 - DroolsJBoss World 2011 - Drools
JBoss World 2011 - Drools
 
AWS Office Hours: Amazon Elastic MapReduce
AWS Office Hours: Amazon Elastic MapReduce AWS Office Hours: Amazon Elastic MapReduce
AWS Office Hours: Amazon Elastic MapReduce
 
TSQL Functions (SQL Server)
TSQL Functions (SQL Server)TSQL Functions (SQL Server)
TSQL Functions (SQL Server)
 
Chris Seebacher Portfolio
Chris Seebacher PortfolioChris Seebacher Portfolio
Chris Seebacher Portfolio
 
My2dw
My2dwMy2dw
My2dw
 
Buenos Aires Drools Expert Presentation
Buenos Aires Drools Expert PresentationBuenos Aires Drools Expert Presentation
Buenos Aires Drools Expert Presentation
 
Do You Have the Time
Do You Have the TimeDo You Have the Time
Do You Have the Time
 
Best Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop Professionals
Best Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop ProfessionalsBest Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop Professionals
Best Practices for the Hadoop Data Warehouse: EDW 101 for Hadoop Professionals
 
Speedment & Sencha at Oracle Open World 2015
Speedment & Sencha at Oracle Open World 2015Speedment & Sencha at Oracle Open World 2015
Speedment & Sencha at Oracle Open World 2015
 
Re-Engineering PostgreSQL as a Time-Series Database
Re-Engineering PostgreSQL as a Time-Series DatabaseRe-Engineering PostgreSQL as a Time-Series Database
Re-Engineering PostgreSQL as a Time-Series Database
 
Data Warehousing and Data Mining
Data Warehousing and Data MiningData Warehousing and Data Mining
Data Warehousing and Data Mining
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 

Recently uploaded

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 

Recently uploaded (20)

From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 

Track your data across the fourth dimension

  • 1. Text Tracking your Data Across the Fourth Dimension Wellington Waterloo Web Makers Meetup
  • 2. “In physics, spacetime is any mathematical model that combines space and time into a single interwoven continuum.” –Wikipedia
  • 3. Databases are Good at ‘Now’ CRUD Create data Read data Update data Delete data
  • 4. Bread and Butter Queries How many people work in department X? What’s the total amount paid in commissions by department? What was the top selling product in the last year?
  • 5. The Fourth Dimension… Compare reporting relationships one month ago, today and in one months time Show me all changes made to this data over time As of one month ago, what did I believe to be the truth about this data yesterday and what did I actually believe was the truth yesterday?
  • 6. “A temporal database is a database with built-in support for handling data involving time…” –Wikipedia ”
  • 7. A Little History… Need for temporal support identified as early as 1992 Initial attempts to get support for this into the SQL standard were rejected Finally made it into the SQL:2011 standard
  • 8. Some Sample Data Owner Property Jeremy ‘Some place’
  • 9. What forms of temporal data are usually stored?
  • 10. Temporal Aspects Decision Time Valid Time Transaction Time A table that implements one of these is temporal, two is bi-temporal, more is multi-temporal
  • 11. Decision Time Records when a decision was taken Stored as a timestamp You may be doing this already…
  • 12. Decision Time Example Owner Property Decision Time Jeremy ‘Some place’ 2012-09-28 Adam ‘Some place’ 2014-02-21
  • 13. Valid Time The time during which a fact is true with respect to the real world Modelled as a range between two timestamps Closed at lower bound, can be open at upper bound
  • 14. Valid Time Example Owner Property StartVT EndVT Jeremy ‘Some place’ 2012-09-28 2014-02-20 Adam ‘Some place’ 2014-02-21 ∞
  • 15. Valid Time Example Owner Property StartVT EndVT Jeremy ‘Some place’ 2012-09-28 2014-03-20 Adam ‘Some place’ 2014-03-21 ∞
  • 16. Transaction Time The time period during which a fact stored in the database is considered to be true Modelled as a range between two timestamps Closed at lower bound, can be open at upper bound
  • 17. Transaction Time Example Owner Property StartVT EndVT StartTT EndTT Jeremy ‘Some place’ 2012-09-2 8 2014-02-2 0 2012-09-2 8 2014-09-0 9 Adam ‘Some place’ 2014-02-2 1 ∞ 2014-02-2 1 2014-09-0 9 Jeremy ‘Some place’ 2012-09-2 8 2014-03-2 0 2014-09-0 9 ∞ Adam ‘Some place’ 2014-03-2 1 ∞ 2014-09-0 9 ∞
  • 18. Valid Time and Transaction Time are Orthogonal Valid Time Transaction Time
  • 19. Enough theory! How do I add this stuff to my db schema?
  • 20. SQL:2011 Temporal Additions ! PERIOD type for date ranges in table definitions Can query for data in a period using new predicates CONTAINS, OVERLAPS, EQUALS, PRECEDES and IMMEDIATELY SUCCEEDS Can also update or delete data matching a portion of a period using FOR PORTION OF {period_name} FROM {date} TO {date}
  • 21. Implementing Valid Time Done by adding columns for start and end with a period constraint on the columns Period must also be included as part of the primary key
  • 22. Table Definition using Valid Time CREATE TABLE Emp( ENo INTEGER, StartVT DATE, EndVT DATE, EDept INTEGER, PERIOD FOR EValidTime (StartVT, EndVT), PRIMARY KEY (ENo, EValidTime WITHOUT OVERLAPS), )
  • 23. Foreign Key Problems… It’s possible that rows in child tables may reference rows in parent tables that are not currently valid and vice versa Therefore foreign key constraints have to be updated where needed to include period data
  • 24. Valid time foreign key definition ALTER TABLE Emp ADD FOREIGN KEY (Edept, PERIOD EValidTime) REFERENCES Dept (DNo, PERIOD DValidTime)
  • 25. Querying a Table using Valid Time SELECT Ename, Edept FROM Emp WHERE ENo = 22217 AND EValidTime CONTAINS DATE ‘2011-01-02’; ! SELECT Ename, Edept FROM Emp WHERE ENo = 22217 AND EValidTime OVERLAPS PERIOD (DATE ‘2010-01-01', DATE ‘2011-01-01');
  • 26. Implementing Transaction Time RDBMS automatically creates a snapshot when data is updated or deleted Done by adding columns for start, end and a period for them Must also add WITH SYSTEM VERSIONING to create table statement
  • 27. Table Definition using Transaction Time CREATE TABLE Emp ENo INTEGER, Sys_start TIMESTAMP(12) GENERATED ALWAYS AS ROW START, Sys_end TIMESTAMP(12) GENERATED ALWAYS AS ROW END, EName VARCHAR(30), PERIOD FOR SYSTEM_TIME (Sys_start, Sys_end) ) WITH SYSTEM VERSIONING
  • 28. Querying a Table using Transaction Time SELECT ENo,EName,Sys_Start,Sys_End FROM Emp FOR SYSTEM_TIME AS OF TIMESTAMP '2011-01-02 00:00:00’; ! SELECT ENo,EName,Sys_Start,Sys_End FROM Emp FOR SYSTEM_TIME FROM TIMESTAMP '2011-01-02 00:00:00’TO TIMESTAMP '2011-12-31 00:00:00’;
  • 29. Bi-Temporal table definition CREATE TABLE Emp( ENo INTEGER, StartVT DATE, EndVT DATE, EDept INTEGER, PERIOD FOR EValidTime (StartVT, EndVT), Sys_start TIMESTAMP(12) GENERATED ALWAYS AS ROW START, Sys_end TIMESTAMP(12) GENERATED ALWAYS AS ROW END, EName VARCHAR(30), PERIOD FOR SYSTEM_TIME (Sys_start, Sys_end), PRIMARY KEY (ENo, EValidTime WITHOUT OVERLAPS), FOREIGN KEY (Edept, PERIOD EValidTime) REFERENCES Dept (DNo, PERIOD DValidTime) ) WITH SYSTEM VERSIONING
  • 30. Who supports SQL:2011 Temporal? IBM DB2. Called “Time Travel Queries”, although different syntax for FOR SYSTEM_TIME AS OF Oracle 12c, in compliance with SQL:2011 Others?
  • 31. Further Reading ‘Developing Time Oriented Applications in SQL’ by Richard Snodgrass ‘Temporal Features in SQL:2011’ ‘Time and Relational Theory: Temporal Databases in the Relational Model and SQL'
  • 32. Thanks for listening Any questions? Contact me: @JCook21 jeremycook0@gmail.com I’d love some feedback!