Temporal Databases: Queries 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: Queries November 2, 2015 1 / 122
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
NoteThese names are not official just used for reference in thispresentation.
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 15 / 122
Five Cases Simplified Overlap Operators
Example (The Cases)
0 1 2 3 4 5 6 7 8 9 10 11 12
Y YY
YY
YX
The Boolean Expressions
Case Expression
No Y.vte ≤ X.vts or X.vte ≤ Y.vtsRight X.vts < Y.vts and Y.vts < X.vteLeft X.vts < Y.vte and Y.vte < X.vteFull Y.vts ≤ X.vts and X.vte ≤ X.vte
Do intervals overlap?X.vts < Y.vte AND Y.vts < X.vte
Get Overlap[greatest(X.vte, Y.vte), least(X.vts, Y.vts))
provided that the intervals X and Y overlap
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 16 / 122
Query Types: Current, Sequence, and Non-Sequenced
NoteCurrent state for backwards compatibility
Sequenced for built-in temporal semantic/support
Non-sequenced for accessing timestamps columns directlyKristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 17 / 122
Valid Time versus Transaction Time
ExerciseIn groups, use 3 minutes to compare valid time and transaction time.
Valid TimePast, present, and future
User supplied
Supports now
Physical delete
“History correction”
Transaction TimePast and present
System supplied
Supports now
Logical delete
History preserving (log)
Valid Time for sure if:Time is into the future
Time can be supplied by the user
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 18 / 122
Valid Time versus Transaction Time
ExerciseIn groups, use 3 minutes to compare valid time and transaction time.
Valid TimePast, present, and future
User supplied
Supports now
Physical delete
“History correction”
Transaction TimePast and present
System supplied
Supports now
Logical delete
History preserving (log)
Valid Time for sure if:Time is into the future
Time can be supplied by the user
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 18 / 122
Valid Time versus Transaction Time
ExerciseIn groups, use 3 minutes to compare valid time and transaction time.
Valid TimePast, present, and future
User supplied
Supports now
Physical delete
“History correction”
Transaction TimePast and present
System supplied
Supports now
Logical delete
History preserving (log)
Valid Time for sure if:Time is into the future
Time can be supplied by the user
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 18 / 122
Summary: Motivation I
Valid, Transaction, and User-Defined TimeValid time and transaction time are orthogonalValid time: Past, present, and future
Can be user-defined (default similar to transaction time)
Transaction time: Past and presentAlways system defined
User-defined time (if not valid time or transaction time)e.g., birthday
Query TypesCurrent
Sequenced
Non-sequenced
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 19 / 122
Summary: Motivation II
Database VariablesSpecial variable now
Different semantics valid time and transaction time
Databases do not support variablesnow = 9999-12-31 (maximum value of date domain)
IntervalsTimestamps are half-open intervals
Closed intervals +/- 1 therefore more complicated
Overlap between intervals is essential to understandNot difficult5 cases to consider
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 20 / 122
Outline
1 Overview and FoundationCurrent, Valid Time, Transaction Time, and BitemporalOverlap Between Intervals
2 Temporal Join
3 Aggregation
4 Coalescing
5 Set Operators
6 TimesliceValid TimeBitemporal
7 Summary
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 21 / 122
The Idea: One
Example (Emp)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HRKim, HR
Example (Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
HR, 10
Example (Emp Temporal Natural Join Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HR, 10Kim, HR, 10
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 22 / 122
The Idea: Two
Example (Emp)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HRKim, HR
Example (Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
HR, 10HR, 20
Example (Emp Temporal Natural Join Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HR, 10Kim, HR, 10
Kim, HR, 20
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 23 / 122
The Idea: Three
Example (Emp)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HRKim, HR
Example (Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
HR, 5
Example (Emp Temporal Natural Join Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 24 / 122
The Idea: Four
Example (Emp)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HRKim, RD
Example (Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
HR, 10RD, 20
Example (Emp Temporal Natural Join Dept)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Joe, HR, 10Kim, RD, 10
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 25 / 122
Natural Join Informal Definition
Idea Temporal Natural JoinThe explicit attribute must be equal
The intersection of the timestamp intervals must be non-empty
The Overlap Cases to Consider
0 1 2 3 4 5 6 7 8 9 10 11 12
Before After
Left Overlaps
During
Right Overlaps
Total Overlaps
X
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 26 / 122
Temporal Natural-Join Example
s e l e c t cname , adr , ptype , p . vts , p . v te −− t o t a l over lapfrom c u s t v t as c , p r o p e r t y v t as pwhere c . c id = p . c idand c . v t s <= p . v ts and p . v te <= c . v te unions e l e c t cname , adr , ptype , c . vts , p . v te −− r i g h t over lapfrom c u s t v t as c , p r o p e r t y v t as pwhere c . c id = p . c idand c . v t s between p . v t s and p . v teand c . v te > p . v te unions e l e c t cname , adr , ptype , p . vts , c . v te −− l e f t over lapfrom c u s t v t as c , p r o p e r t y v t as pwhere c . c id = p . c idand c . v te between p . v t s and p . v teand c . v t s < p . v t s union −− dur ings e l e c t cname , adr , ptype , c . vts , c . v tefrom c u s t v t as c , p r o p e r t y v t as pwhere c . c id = p . c idand c . v t s between p . v t s and p . v teand c . v te between p . v t s and p . v te
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 27 / 122
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 33 / 122
Main Idea in Finding Constant Regions
Five cases to considerNo other interval overlaps me
Another interval has vts between my vts and vte
Another interval has vte between my vts and vte
Another interval totally overlaps me
Another interval is after me
NoteFor each case there can be no vts or vte between returned result
A single interval can qualify for more than one of the cases aboveSQL is declarative therefore ”after” could be ”before”
But just one of the cases can be considered
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 34 / 122
Main Query
Example (The Main Query)s e l e c t count (ename) as cnt ,
cons t reg ions . v t s as vts ,cons t reg ions . v te as vte
from emp vt , cons t reg ionswhere emp vt . v t s < cons t reg ions . v teand cons t reg ions . v t s < emp vt . v tegroup by cons t reg ions . vts , cons t reg ions . v teorder by vts , v te
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 35 / 122
Constant Regions Part 1: No Overlap
wi th cons t reg ions as (−− get t1 . v t s and t1 . v tes e l e c t t1 . v t s as vts , t1 . v te as vtefrom emp vt t1where not e x i s t s (
s e l e c t ∗from emp vt t3where ( t1 . v t s < t3 . v t s and t3 . v t s < t1 . v te ) or
( t1 . v t s < t3 . v te and t3 . v te < t1 . v te ) )
Example
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
result = t1
not exists t3
not exists t3
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 36 / 122
Constant Regions Part 2:
union−− t1 . v t s and t2 . v t ss e l e c t t1 . v t s as vts , t2 . v t s as vtefrom emp vt t1 , emp vt t2where t1 . v t s < t2 . v t s and t2 . v t s < t1 . v teand not e x i s t s (
s e l e c t ∗from emp vt t3where ( t1 . v t s < t3 . v t s and t3 . v t s < t2 . v t s ) or
( t1 . v t s < t3 . v te and t3 . v te < t2 . v t s ) )
Example
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
t1
t2
result
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 37 / 122
Constant Regions Part 3:
union−− t1 . v t s and t2 . v tes e l e c t t1 . v t s as vts , t2 . v te as vtefrom emp vt t1 , emp vt t2where t1 . v t s < t2 . v te and t2 . v te < t1 . v teand not e x i s t s (
s e l e c t ∗from emp vt t3where ( t1 . v t s < t3 . v t s and t3 . v t s < t2 . v te ) or
( t1 . v t s < t3 . v te and t3 . v te < t2 . v te ) )
Example
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
t1
t2
result
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 38 / 122
Constant Regions Part 4: Before
union−− t1 . v te and t2 . v t ss e l e c t t1 . v te as vts , t2 . v t s as vtefrom emp vt t1 , emp vt t2where t1 . v te < t2 . v t sand not e x i s t s (
s e l e c t ∗from emp vt t3where ( t1 . v te < t3 . v t s and t3 . v t s < t2 . v t s ) or
( t1 . v te < t3 . v te and t3 . v te < t2 . v t s ) )
Example
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
t1
t2
result
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 39 / 122
Constant Regions Part 5:
union−− t1 . v te and t2 . v tes e l e c t t1 . v te as vts , t2 . v te as vtefrom emp vt t1 , emp vt t2where t2 . v t s < t1 . v te and t1 . v te < t2 . v teand not e x i s t s (
s e l e c t ∗from emp vt t3where ( t1 . v te < t3 . v t s and t3 . v t s < t2 . v te ) or
( t1 . v te < t3 . v te and t3 . v te < t2 . v te ) ) )
Example
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
t1
t2
result
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 40 / 122
Correctness of Constant Regions
Five = Original row + Four Combinations[t1.vts, t1.vte)
[t1.vts, t2.vts)
[t1.vts, t2.vte)
[t2.vte, t2.vts)
[t2.vte, t2.vte)
NoteAll cases are covered
Informal argument
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 41 / 122
Quiz
Query in EnglishFind the constant regions and average budget for all departments overtime.
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 48 / 122
Example: Valid-Time State Data Model
Example (State and Event)
pname vts vte
Bush 1989 1993Clinton 1993 1997Clinton 1997 2001Bush jr. 2001 2005Bush jr. 2005 2009Obama 2009 2013Obama 2013 2017
pname vts vte
Bush 1989 1993Clinton 1993 2001Bush jr. 2001 2009Obama 2009 2017
QuestionsDo the tables have the same content?
Does it have anything to do with duplicate elimination?
What can you say about primary keys (tricky)?
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 49 / 122
Example: Event Data Model
Example (Similar Content)
pname vts vte
Bush 1989 1993Clinton 1993 1997Clinton 1997 2001Bush jr. 2001 2005Bush jr. 2005 2009Obama 2009 2013Obama 2013 2017
pname vts
Bush 1989Clinton 1993Clinton 1997Bush jr. 2001Bush jr. 2005Obama 2009Obama 2013
pname vts
Bush 1989Clinton 1993Bush jr. 2001Obama 2009
QuestionsAny implicit assumptions for state or event table?
Future data any differences?
What are the pros/cons state versus event table?
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 50 / 122
Quiz: Coalescing
QuestionsHow much space can be saved coalescing temporal data?
Informal argument using examples!
Can data in valid-time event tables be un-coalesced?Informal argument using examples!
AnswersThe saving coalescing the data stored can be significant. Considermany overlaps of value equivalent rows.
Event-tables can also be un-coalesced.
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 51 / 122
Quiz: Coalescing
QuestionsHow much space can be saved coalescing temporal data?
Informal argument using examples!
Can data in valid-time event tables be un-coalesced?Informal argument using examples!
AnswersThe saving coalescing the data stored can be significant. Considermany overlaps of value equivalent rows.
Event-tables can also be un-coalesced.
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 51 / 122
Advantages and Disadvantages
CoalescedMinimal space
Row delete info. lost
Queries fasterLess data
Modifications slowerMany checks
Un-coalescedAdditional space
Possibly much larger
Queries slowerMore data
Modifications fasterLess checking
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 52 / 122
Coalescing in SQL (Proposed by Celko/Bohlen)s e l e c t d i s t i n c t f i . ename , f i . v ts , l a . v tefrom pro jemp vt f i , p ro jemp vt l awhere f i . v t s < l a . v teand f i . ename = l a . enameand not e x i s t s ( −− ensure no holes and extend towards f i . v t s
s e l e c t ∗from pro jemp vt miwhere mi . ename = f i . enameand f i . v t s < mi . v t s and mi . v t s < l a . v teand not e x i s t s (
s e l e c t ∗from pro jemp vt a1where a1 . ename = f i . enameand a1 . v t s < mi . v t s and mi . v t s <= a1 . v te ) )
and not e x i s t s ( −− ensure only maximal per iodss e l e c t ∗from pro jemp vt a2where a2 . ename = f i . enameand ( a2 . v t s < f i . v t s and f i . v t s <= a2 . v te
or a2 . v t s <= l a . v te and l a . v te < a2 . v te ) )order by f i . ename , f i . v t s
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 53 / 122
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 60 / 122
Set Intersection: The CasesExample (Before)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
empty
Example (After)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
empty
Example (Right Overlap)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Left Overlap)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Included)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empbossresult
Example (Totally Overlaps)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empbossresult
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 61 / 122
Set Intersection: Informally
DefinitionExplicit columns must be equalIf overlap between the intervals then take the
The largest of the vts valuesThe smallest of the vte values
Otherwise skip the row
NoteRecall that vts ≤ vte for interval to be valid
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 62 / 122
Set Intersection in SQL
s e l e c t emp. ename , emp. dept , −− over lap emp and bossg rea tes t (emp. vts , boss . v t s ) as vts ,l e a s t (emp. vte , boss . v te ) as v te
from emp dept vt as emp, bossemp dept vt as bosswhere emp. ename = boss . ename and emp. dept = boss . deptand emp. v ts < boss . v te and boss . v t s < emp. v teand grea tes t (emp. vts , boss . v t s ) <= l e a s t (emp. vte , boss . v te )
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 63 / 122
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 65 / 122
Set Union: The CasesExample (Before)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empbossresult result
Example (After)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result result
Example (Right Overlap)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Left Overlap)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Included)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empbossresult
Example (Totally Overlaps)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empbossresult
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 66 / 122
Set Union: Informally
DefinitionExplicit columns must be equalIf overlap between the intervals then take the
The smallest of the vts valuesThe largest of the vte values
Else take the intervals in the first table,
and take the intervals in the second table
NoteThree cases to consider
Mutual exclusive cases therefore union all between cases
The rows must be snapshot equal, i.e., have the same explicitcolumns
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 67 / 122
Set Union in SQLs e l e c t emp. ename , emp. dept , −− over lap emp and boss
l e a s t (emp. vts , boss . v t s ) as vts ,g rea tes t (emp. vte , boss . v te ) as v te
from emp dept vt as emp, bossemp dept vt as bosswhere emp. ename = boss . ename and emp. dept = boss . deptand emp. v ts < boss . v te and boss . v t s < emp. v te union a l ls e l e c t emp. ename , emp. dept , emp. vts , emp. v te −− emp no over lapfrom emp dept vt as empwhere not e x i s t s (
s e l e c t ∗from bossemp dept vt as bwhere emp. ename = b . ename and emp. dept = b . deptand emp. v ts < b . v te and b . v t s < emp. v te ) union a l l
s e l e c t boss . ename , boss . dept , boss . vts , boss . v te −− boss no over lapfrom bossemp dept vt as bosswhere not e x i s t s (
s e l e c t ∗from emp dept vt as bwhere boss . ename = b . ename and boss . dept = b . deptand boss . v t s < b . v te and b . v t s < boss . v te )
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 70 / 122
Set Difference: The CasesExample (Before)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (After)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Right Overlap)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Left Overlap)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result
Example (Included)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
result result
Example (Totally Overlaps)
VT0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
empboss
empty
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 71 / 122
Set Difference: Informally
DefinitionExplicit columns must be equalIf overlap between the intervals then
Row with interval [emp.vts, boss.vts) if emp.vts< boss.vtsRow with interval [boss.vte, emp.vte) if boss.vte< emp.vte
Otherwise skip the row
NoteBefore or after does not matter
Total overlap does not add rows to the result
During is handled like a combination of left and right overlap
One interval may be split into one or two intervals
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 72 / 122
Set Difference in SQL, Part 1
s e l e c t emp. ename , emp. dept , emp. vts , emp. v te −− no over lapfrom emp dept vt as empwhere not e x i s t s (
s e l e c t ∗from bossemp dept vt as bwhere emp. ename = b . ename and emp. dept = b . deptand emp. v ts < b . v te and b . v t s < emp. v te )
union
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 73 / 122
Set Difference in SQL, Part 2
s e l e c t emp. ename , emp. dept , emp. vts , boss . v t s −− r i g h t over lapfrom emp dept vt as emp, bossemp dept vt as bosswhere emp. ename = boss . ename and emp. dept = boss . deptand emp. v ts < boss . v t s and boss . v t s < emp. v te and
not e x i s t s (s e l e c t ∗from bossemp dept vt as bwhere emp. ename = b . ename and emp. dept = b . deptand emp. v ts < b . v te and b . v t s < boss . v t s )
unions e l e c t emp. ename , emp. dept , boss . vte , emp. v te −− l e f t over lapfrom emp dept vt as emp, bossemp dept vt as bosswhere emp. ename = boss . ename and emp. dept = boss . deptand boss . v te < emp. v te and emp. v ts < boss . v te and
not e x i s t s (s e l e c t ∗from bossemp dept vt as bwhere emp. ename = b . ename and emp. dept = b . deptand boss . v te < b . v te and b . v t s < emp. v te )
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 74 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ?
Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ?
Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ?
No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R?
Yes!In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R?
Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R?
Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result?
Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result?
Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Quiz: Set Operations
QuestionsR ∪ S = S ∪ R ? Yes!
R ∩ S = S ∩ R ? Yes!
R − S = S − R ? No!Can R ∪ S return fewer rows than there are in R? Yes!
In contrast to the non-temporal version
Can R − S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R ∩ S return more rows than there are in R? Yes!In contrast with non-temporal version
Can R − S return an empty result? Yes!Like the non-temporal version!
Can R ∩ S return an empty result? Yes!Like the non-temporal version!
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 75 / 122
Summary: Set Operators
Main PointsNo big surprises in how union, intersection, and difference are defined
The size of the result sets may be a surprise!Set difference is surprisingly difficult
Challenge that single row can be split into two rows
NoteNotation of union compatibility still applies!Alternative interpretations of the set operators exists
However not in widespread usage
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 76 / 122
Outline
1 Overview and FoundationCurrent, Valid Time, Transaction Time, and BitemporalOverlap Between Intervals
2 Temporal Join
3 Aggregation
4 Coalescing
5 Set Operators
6 TimesliceValid TimeBitemporal
7 Summary
Kristian Torp (Aalborg University) Temporal Databases: Queries November 2, 2015 77 / 122