62
Temporal Databases: Data Models Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp [email protected] November 2, 2015 daisy.aau.dk Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 1 / 50

Temporal Databases: Data Models

  • Upload
    torp42

  • View
    300

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Temporal Databases: Data Models

Temporal Databases: Data Models

Kristian Torp

Department of Computer ScienceAalborg University

people.cs.aau.dk/˜[email protected]

November 2, 2015

daisy.aau.dk

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 1 / 50

Page 2: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 2 / 50

Page 3: Temporal Databases: Data Models

Learning Goals

Learning GoalsUnderstand event versus state tables

Understand how temporal information is stored

Understand temporal integrity constraints

NoteCore temporal ideas, not specific DBMS productWe will use integers for timestamps

simply because it takes up less space on slides

Will focus on state tablesMost expressiveHardest to understand (compared to event)Supported by IBM, Microsoft, and Teradata

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 3 / 50

Page 4: Temporal Databases: Data Models

Purpose of Temporal Data Model

BenefitsUnderstand and measure changes (over time)Tracking/traceability

Avoid being sued

Predictions (mostly about the future :-))

Legal reasons (Sarbanes-Oxley Act)

Example (Dell/Toyota/VW Recalls)2006-08-16 Dell recalls 4.1 million lithium-ion batteries

2013-03-19 VW recalls 384,181 vehicles with gearbox issues

2013-10-17 Toyota recalls 885,000 vehicles with airbag issues

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 4 / 50

Page 5: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 5 / 50

Page 6: Temporal Databases: Data Models

Event versus State Tables

Example (Event: Stock Prices)

StockID DateTime Price

IBM 2013-11-11 12:33:44 47Apple 2013-11-11 12:33:47 161IBM 2013-11-12 10:20:45 48IBM 2013-11-13 23:59:00 51Apple 2013-11-13 08:01:02 148

Example (State: CEO)

StockID CEO DateFrom DateTo

Apple Steve Jobs 1997 2011Apple Tim Cook 2011 now

IBM Louis V. Gerstner 1993 2002IBM Samuel J. Palmisano 2002 2011IBM Virginia M. Rometty 2011 now

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 6 / 50

Page 7: Temporal Databases: Data Models

Transaction-Time Event (log)

Example (Web-Server Log)

session id url tt

11 main.htm 111 main.htm 322 main.htm 811 about.htm 1222 order.htm 1522 download.htm 1533 about.htm 15

NoteOnly a single timestamp column

Append-only, i.e., only inserts and no deletes or updates

Sorted on the timestamp column (tt)

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 7 / 50

Page 8: Temporal Databases: Data Models

Quiz: Event or State?

ExamplesYour IT certifications?

A personal monthly qualification bonus for a year?

A permanent monthly qualification bonus?

The period for your mortgage loans?

The daily police report?

The owner of a land property? (BBR in Denmark)

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 8 / 50

Page 9: Temporal Databases: Data Models

Summary: Event vs. State Tables

Main PointsState is intervals, e.g., [2015-11-20 08:00:00, 2015-11-26 16:00:00)

State is single timestamps, e.g., 2015-11-20 12:00:00

State is most often usedCan convert between event and state in many cases

The SQL statements are non-trivial!

NoteIBM DB2 uses state data model for both valid-time andtransaction-time

Microsoft uses state data model for transaction time

TeraData uses state data model for both valid-time andtransaction-time

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 9 / 50

Page 10: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 10 / 50

Page 11: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 11 / 50

Page 12: Temporal Databases: Data Models

Valid-Time State Tables

Example (AllEmps)

Name Dept vts vte

Joe Shoe 7 10Kim Toy 7 11Joe Toy 10 now

Kim Sports 11 13Kim Shoe 13 now

NoteName and Dept are explicit columnsvts and vte are implicit columns

Handled like plain columns in the DBMSIn some DBMS modeled as a single column of type period

Explicit and implicit columns pure syntax!

now = the variable now (or nobind-now)

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 12 / 50

Page 13: Temporal Databases: Data Models

Visualization of the Database Variable now

Example (AllEmps at time 15)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Kim, ShoeKim, Sports

Joe, ToyKim, Toy

Joe, Shoe

Example (AllEmps at time 20)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Kim, ShoeKim, Sports

Joe, ToyKim, Toy

Joe, Shoe

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 13 / 50

Page 14: Temporal Databases: Data Models

Visualization of the Database Variable now

Example (AllEmps at time 15)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Kim, ShoeKim, Sports

Joe, ToyKim, Toy

Joe, Shoe

Example (AllEmps at time 20)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Kim, ShoeKim, Sports

Joe, ToyKim, Toy

Joe, Shoe

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 13 / 50

Page 15: Temporal Databases: Data Models

The Database Variable now

Example (Using now)

Name Dept vts vte

Joe Toy 10 now

Kim Shoe 13 now

Notenow indicates that the content of the row is valid

now is a database variableVariables not supported in relational DBMSs

Uses a value like 9999-12-30 or 9999-12-31

now only allowed in the vte column (not in the vts column)

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 14 / 50

Page 16: Temporal Databases: Data Models

Explicit Timestamps and now

Example (Explicit Timestamping Current-Time is 8)

Name vts vte

Anna 2 6Bart 4 12Curt 14 20Dan 2 now

Eric 8 now

Finn 14 now

NoteAnna is data in the past

Bart overlaps current-time

Curt and Finn are into the future

Dan is from the past and forward (now)

Eric if from now and forward (now)

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 15 / 50

Page 17: Temporal Databases: Data Models

Timeslicing and nowExample (Explicit Timestamping Current-Time is 8)

Name vts vte

Anna 2 6Bart 4 12Curt 14 20Dan 2 now

Eric 8 now

Finn 14 now

Example (Emp as of Time 8)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

FinnEric

DanCurt

BartAnna

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 16 / 50

Page 18: Temporal Databases: Data Models

Timeslicing and nowExample (Explicit Timestamping Current-Time is 8)

Name vts vte

Anna 2 6Bart 4 12Curt 14 20Dan 2 now

Eric 8 now

Finn 14 now

Example (Emp as of Time 12)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

FinnEric

DanCurt

BartAnna

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 16 / 50

Page 19: Temporal Databases: Data Models

Timeslicing and nowExample (Explicit Timestamping Current-Time is 8)

Name vts vte

Anna 2 6Bart 4 12Curt 14 20Dan 2 now

Eric 8 now

Finn 14 now

Example (Emp as of Time 16)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

FinnEric

DanCurt

BartAnna

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 16 / 50

Page 20: Temporal Databases: Data Models

Timeslicing and nowExample (Explicit Timestamping Current-Time is 8)

Name vts vte

Anna 2 6Bart 4 12Curt 14 20Dan 2 now

Eric 8 now

Finn 14 now

Example (Emp as of Time 20)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

FinnEric

DanCurt

BartAnna

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 16 / 50

Page 21: Temporal Databases: Data Models

Timeslicing and nowExample (Explicit Timestamping Current-Time is 8)

Name vts vte

Anna 2 6Bart 4 12Curt 14 20Dan 2 now

Eric 8 now

Finn 14 now

Example (Emp as of Time 24)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

FinnEric

DanCurt

BartAnna

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 16 / 50

Page 22: Temporal Databases: Data Models

Quiz: now

Example (Are these two rows the same?)

Name Dept vts vte

Joe Shoe 7 12Joe Shoe 7 now

HintsAt time 8: How many employees do we have the 14th?

How long will Joe work for us? (Asked at times 5, 10, and 15)

Example (Which Rows are Valid?)

Name Dept vts vte

Ann Toy 10 now

Bar Shoe now 20Curt Toy 10 30Dan Shoe now now

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 17 / 50

Page 23: Temporal Databases: Data Models

Quiz: Any Double Agents?

Example (List of Spies)

SID Country vts vte

007 UK 5 now

008 UK 5 15008 USSR 15 now

009 UK 5 20009 USSR 10 20

Example (Spies at time 25)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

009, USSR009, UK

008, USSR008, UK

007, UK

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 18 / 50

Page 24: Temporal Databases: Data Models

Quiz: Any Double Agents?

Example (List of Spies)

SID Country vts vte

007 UK 5 now

008 UK 5 15008 USSR 15 now

009 UK 5 20009 USSR 10 20

Example (Spies at time 25)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

009, USSR009, UK

008, USSR008, UK

007, UK

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 18 / 50

Page 25: Temporal Databases: Data Models

Interval TypesDefinition (Closed Interval)An interval i [a, b] is closed if c ∈ i for a ≤ c ≤ b

Definition (Half-open Interval)An interval i [a, b) is half-open if c ∈ i for a ≤ c < b

Example (Closed Intervals)[4, 8] - [2, 6] = [7, 8]

Example (Half-open Intervals)[4, 8) - [2, 6) = [6, 8)

NoteHalf-open intervals are more convenient to work with

You avoid a lot of +/− 1Do not need to worry about granularity (seconds, hours)

+/- on half-open intervals always results in half-open intervals

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 19 / 50

Page 26: Temporal Databases: Data Models

Quiz: Conceptual Difference?

Example (Departments)

Name Dept vts vte

Ann HR 1 10Ann RD 10 20Ann HR 20 40

Name Dept vts vte

Ann HR 1 15Ann RD 10 20Ann HR 20 40

Example (Salaries)

Name Salary vts vte

Ann 40 1 10Ann 50 10 20Ann 70 20 40

Name Salary vts vte

Ann 40 1 15Ann 50 10 20Ann 70 20 40

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 20 / 50

Page 27: Temporal Databases: Data Models

Quiz: Conceptual Difference?

Example (Departments)

Name Dept vts vte

Ann HR 1 10Ann RD 10 20Ann HR 20 40

Name Dept vts vte

Ann HR 1 15Ann RD 10 20Ann HR 20 40

Example (Salaries)

Name Salary vts vte

Ann 40 1 10Ann 50 10 20Ann 70 20 40

Name Salary vts vte

Ann 40 1 15Ann 50 10 20Ann 70 20 40

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 20 / 50

Page 28: Temporal Databases: Data Models

Summary: Valid-Time State

Main PointsTwo implicit columns vts and vteTimestamp value can be

System supplied, called implicit timestampingUser supplied, called explicit timestamping

The vte column can contain the database variable nowThe value of now changes with the wall-clockImpemented using large value, e.g., 9999-12-31

Intervals are typically half-open [4, 10)Makes computations easier

Timestamp intervals are typically not coalesced

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 21 / 50

Page 29: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 22 / 50

Page 30: Temporal Databases: Data Models

Value-Equivalent Rows

Definition (Value-Equivalent)Two rows r1 and r2 are value-equivalent if their explicit columns values areall equal.

Example

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

(Ann, HR)(Ann, HR)

(Bart, RD)(Bart, RD)

(Curt, RD)(Curt, RD)

The Ann rows are value-equivalent

The Bart rows are value-equivalent

The Curt rows are value-equivalent

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 23 / 50

Page 31: Temporal Databases: Data Models

Quiz: Value-Equivalent Rows

Example (Which Table contains Value-Equivalent Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann HR 15 now

Name Dept vts vte

Ann HR 2 6Ann RD 6 12Bart RD 12 now

Example (Which Table contains Value-Equivalent Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Curt HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 24 / 50

Page 32: Temporal Databases: Data Models

Quiz: Value-Equivalent Rows

Example (Which Table contains Value-Equivalent Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann HR 15 now

Name Dept vts vte

Ann HR 2 6Ann RD 6 12Bart RD 12 now

Example (Which Table contains Value-Equivalent Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Curt HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 24 / 50

Page 33: Temporal Databases: Data Models

Sequenced Duplicate

Definition (Sequenced Duplicate)Two rows r1 and r2 are sequenced duplicates if they are value-equivalentin overlapping time periods.

Example

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

(Ann, HR)(Ann, HR)

(Bart, RD)(Bart, RD)

(Curt, RD)(Curt, RD)

The Ann rows are sequenced-duplicates in the interval [6, 12)

The Bart rows are not sequenced-duplicates

The Curt rows are value-equivalent in the period [6, 14)

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 25 / 50

Page 34: Temporal Databases: Data Models

Quiz: Sequenced Duplicate

Example (Which Table contains Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann HR 5 now

Name Dept vts vte

Ann HR 2 6Ann RD 2 12Bart RD 12 now

Example (Which Table contains Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Ann HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 26 / 50

Page 35: Temporal Databases: Data Models

Quiz: Sequenced Duplicate

Example (Which Table contains Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann HR 5 now

Name Dept vts vte

Ann HR 2 6Ann RD 2 12Bart RD 12 now

Example (Which Table contains Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Ann HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 26 / 50

Page 36: Temporal Databases: Data Models

Current-Duplicate Rows

Definition (Current-Duplicate)Two rows r1 and r2 are value current-duplicate rows if their explicit columnsvalues are all equal in the current-time timeslice.

Example (Which Table contains Current-Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 now

Ann HR 5 now

Name Dept vts vte

Ann HR 2 now

Ann HD 16 now

Bart RD 12 now

Example (Which Table contains Current-Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Curt HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 27 / 50

Page 37: Temporal Databases: Data Models

Current-Duplicate Rows

Definition (Current-Duplicate)Two rows r1 and r2 are value current-duplicate rows if their explicit columnsvalues are all equal in the current-time timeslice.

Example (Which Table contains Current-Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 now

Ann HR 5 now

Name Dept vts vte

Ann HR 2 now

Ann HD 16 now

Bart RD 12 now

Example (Which Table contains Current-Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Curt HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 27 / 50

Page 38: Temporal Databases: Data Models

Current-Duplicate Rows

Definition (Current-Duplicate)Two rows r1 and r2 are value current-duplicate rows if their explicit columnsvalues are all equal in the current-time timeslice.

Example (Which Table contains Current-Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 now

Ann HR 5 now

Name Dept vts vte

Ann HR 2 now

Ann HD 16 now

Bart RD 12 now

Example (Which Table contains Current-Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 8 15Ann AD 15 now

Name Dept vts vte

Ann HR 2 12Bart HR 2 12Curt HR 2 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 27 / 50

Page 39: Temporal Databases: Data Models

Non-Sequenced Duplicate

Definition (Non-Sequenced Duplicate)Two rows r1 and r2 are non-sequenced duplicates if all their columns(explicit and implicit) are identical.

Example

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

(Ann, HR)(Ann, HR)

(Bart, RD)(Bart, RD)

(Curt, RD)(Curt, RD)

The Ann rows are not non-sequenced-duplicates

The Bart rows are not non-sequenced-duplicates

The Curt rows are non-sequenced-duplicates

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 28 / 50

Page 40: Temporal Databases: Data Models

Quiz: Non-Sequenced Duplicate

Example (Which Table contains Non-Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 2 now

Ann HR 2 now

Name Dept vts vte

Ann HR 2 10Ann RD 2 now

Ann HR 2 10

Example (Which Table contains Non-Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 2 8Ann AD 2 8

Name Dept vts vte

Ann HR 2 12Ann HR 2 10Ann HR 10 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 29 / 50

Page 41: Temporal Databases: Data Models

Quiz: Non-Sequenced Duplicate

Example (Which Table contains Non-Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 2 now

Ann HR 2 now

Name Dept vts vte

Ann HR 2 10Ann RD 2 now

Ann HR 2 10

Example (Which Table contains Non-Sequenced Duplicate Rows?)

Name Dept vts vte

Ann HR 2 8Ann RD 2 8Ann AD 2 8

Name Dept vts vte

Ann HR 2 12Ann HR 2 10Ann HR 10 12

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 29 / 50

Page 42: Temporal Databases: Data Models

Summary: Uniqueness

Example

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

(Ann, HR)(Ann, HR)

(Bart, RD)(Bart, RD)

(Curt, RD)(Curt, RD)

Value Equi. Seq. Duplicate Non-Seq. Duplicate

Ann rows X X

Bart rows X

Curt rows X X X

NoteIf non-sequenced duplicates then also sequenced duplicates

It sequenced duplicates then also value equivalent

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 30 / 50

Page 43: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 31 / 50

Page 44: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 32 / 50

Page 45: Temporal Databases: Data Models

Uniqueness over Time

CasesYour DNA profile remains constant over time

Your cell-phone number varies slowly over time, but always one

Your checking balances varies almost daily, but always one

The IBM stock value changes every second but always exactly onevalue

NoteThe notaion of a primary key caries over to temporal databases

The notaion of a foreign key caries over to temporal databases

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 33 / 50

Page 46: Temporal Databases: Data Models

Primary Keys Informally

CasesYou have been working for a company for eight years. In this timeinterval you salary has changed several times. However, at any pointin time you have always had exactly one salary.

You have been married three times. However at any point in time youhave had either zero or one spouse.

Your car has driven 56,000 kilometers but for all millimeters driventhere has been exactly one driver.

You have a house that is 34 years old. Since the house was buildthere has at any time only been one owner of the house.

NoteThere is a general concept of time-varying uniqueness

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 34 / 50

Page 47: Temporal Databases: Data Models

Temporal Primary Key Example

Example ( trainer at time 19)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

2, Bo, 302, Bo, 201, Ann, 251, Ann, 201, Ann, 10

Example (As Valid-Time Table)

tid tname salary vts vte

1 Ann 10 1 71 Ann 20 9 151 Ann 25 15 now

2 Bo 20 2 132 Bo 30 13 18

NoteAt any point in time is tid a unique value!

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 35 / 50

Page 48: Temporal Databases: Data Models

Not Temporal Primary Key

Example ( tid not a Temporal Primary Key)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

2, Bo, 202, Bo, 302, Bo, 20

1, Ann, 252, Ann, 20

1, Ann, 10

Example ( tid a Temporal Primary Key?)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

5, Dan, 204, Dan, 20

3, Cho, 402, Bo, 402, Bo, 302, Bo, 20

1, Ann, 251, Ann, 10

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 36 / 50

Page 49: Temporal Databases: Data Models

Summary: Primary Key

Main PointsA temporal primary-key is a straight-forward extension of snapshotprimary keys

A temporal primary-key much more computation intesive

NoteLimited or no support for temporal primary keys in DBMSs

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 37 / 50

Page 50: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 38 / 50

Page 51: Temporal Databases: Data Models

Foreign Keys Informally

CasesYou cannot have a contract to rent a house in the period April toOctober if the house is first built in September

You cannot call your friend after the subscription of her/his cell-phonehas expired

You expect that the cinema has a copy of the movie Blade Runner IIavailable during the entire period where you have bought a ticket tosee this movie in the cinema

NoteThere is a constraint between the time interval associated with thephenomenon stored in the database.

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 39 / 50

Page 52: Temporal Databases: Data Models

Temporal Foreign Key ExampleExample (emp and dept at time 19)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

2, Bo, 102, Bo, 101, Ann, 101, Ann, 201, Ann, 10

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

20, RD20, RD20, RD10, HR10, HR

Example (As Tables)tid tname did vts vte

1 Ann 10 1 91 Ann 20 10 131 Ann 10 16 now

2 Bo 10 2 62 Bo 10 13 18

did dname vts vte

10 HR 1 910 HR 11 now

20 RD 1 620 RD 8 1320 RD 16 now

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 40 / 50

Page 53: Temporal Databases: Data Models

Idea in Foreign-Key Solution

RequirementsThe emp vts value must be covered by an interval in dept

The emp vte value must be covered by an interval in dept

There cannot be any ”holes” in the coverage of the interval for emp

Example (emp and dept at time 19)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

EmpDept Dept

“Hole”

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 41 / 50

Page 54: Temporal Databases: Data Models

Part 1 as a FigureExample (Overlap of vts)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

EmpCase 1Case 2Case 3Case 4Case 5Case 6Case 7Case 8Case 9Case 10Case 11Case 12Case 13

NoteAllen’s operators

13 cases because interval to interval comparisonKristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 42 / 50

Page 55: Temporal Databases: Data Models

Part 1 in SQL

Examplewhere not e x i s t s ( s e l e c t ∗ −− v ts i s not covered

from dept as swhere s . d id = emp. d idand s . v t s <= emp. v ts and emp. v ts < s . v te )

Note≤ when comparing vts values

< when comparing a vts value to a vte value

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 43 / 50

Page 56: Temporal Databases: Data Models

Part 2 as a Figure and SQLExample (Overlap of vte)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

EmpCase 1Case 2Case 3Case 4Case 5

Example (Overlap vte in SQL)or not e x i s t s (

s e l e c t ∗ −− vte i s not coveredfrom dept as ewhere e . d id = emp. d idand e . v t s < emp. v te and emp. v te <= e . v te )

Note< when comparing a vts value to a vte value

≤ when comparing vte values

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 44 / 50

Page 57: Temporal Databases: Data Models

Part 3 Example as a Figure

Example (Overlap of vts)

VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Emps

eb1

b2

Conditionss overlaps emp.vts

e overlaps emp.vteExist

A b1 that fills the “holes” towards emp.vteA b2 that exists the filling of the “hold” towards emp.vte

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 45 / 50

Page 58: Temporal Databases: Data Models

Part 3 in SQL

Exampleor e x i s t s ( −− there i s a hole

s e l e c t ∗from dept as b1where b1 . d id = emp. d idand emp. v ts < b1 . v te and b1 . v te < emp. v teand not e x i s t s (

s e l e c t ∗from dept as b2where b2 . d id = emp. d idand b2 . v t s <= b1 . v te and b1 . v te < b2 . v te ) )

NoteThere exists a row in dept where the vte is in the interval [emp.vts,emp.vte),

and there is no row in dept that has an interval that extends towardsemp.vte

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 46 / 50

Page 59: Temporal Databases: Data Models

Entire SolutionExamples e l e c t ∗from empwhere e x i s t s ( s e l e c t ∗

from dept as swhere s . d id = emp. d idand s . v t s <= emp. v ts and emp. v ts < s . v te )

and e x i s t s ( s e l e c t ∗from dept as ewhere e . d id = emp. d idand e . v t s < emp. v te and emp. v te <= e . v te )

and not e x i s t s ( s e l e c t ∗from dept as b1where b1 . d id = emp. d idand emp. v ts < b1 . v te and b1 . v te < emp. v teand not e x i s t s

( s e l e c t ∗from dept as b2where b2 . d id = emp. d idand b2 . v t s <= b1 . v te and b1 . v te < b2 . v te ) )

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 47 / 50

Page 60: Temporal Databases: Data Models

Summary: Foreign Key

Main PointsA temporal foreign-key is a straight-forward extension of non-temporalforeign-keys

A temporal foreign-key is very expensive to enforce

NoteLimited or no support for temporal foreign-keys in DBMSs

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 48 / 50

Page 61: Temporal Databases: Data Models

Outline

1 State and Event Tables

2 State TablesValid Time

3 Uniqueness

4 Temporal KeysPrimary KeysForeign Keys

5 Summary

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 49 / 50

Page 62: Temporal Databases: Data Models

Summary: Temporal Data Model

Main PointsEvent single timestampState two timestamps (from and to)

What is supported by IBM, Microsoft, and Teradata

now = nobind-now (in column vte)Implemented by large timestamp, e.g., 9999-12-31

UC = until-changed (in column tte)Implemented by large timestamp, e.g., 9999-12-31

Temporal primary-key and foreign-keyCan be hard to understand

NoteUC is sometimes all so called now or nobind now

Kristian Torp (Aalborg University) Temporal Databases: Data Models November 2, 2015 50 / 50