Temporal Databases

90
1 Temporal Databases Temporal Databases 1. VALID-TIME TEMPORAL DATA MODEL 2. TIME NORMALIZATION 3. TEMPORAL QUERY LANGUAGE 4. CONCEPTUAL DESIGN AND LOGICAL DESIGN Dr. Dang Tran Khanh Head, Department of Information Systems Director, Advances in Security & Information Systems Lab Faculty of Computer Science & Engineering HCM University of Technology, Vietnam

description

Temporal Databases. VALID-TIME TEMPORAL DATA MODEL TIME NORMALIZATION TEMPORAL QUERY LANGUAGE CONCEPTUAL DESIGN AND LOGICAL DESIGN. Dr. Dang Tran Khanh Head, Department of Information Systems Director, Advances in Security & Information Systems Lab - PowerPoint PPT Presentation

Transcript of Temporal Databases

Page 1: Temporal Databases

11

Temporal DatabasesTemporal Databases

1. VALID-TIME TEMPORAL DATA MODEL

2. TIME NORMALIZATION

3. TEMPORAL QUERY LANGUAGE

4. CONCEPTUAL DESIGN AND LOGICAL DESIGN

Dr. Dang Tran KhanhHead, Department of Information SystemsDirector, Advances in Security & Information Systems LabFaculty of Computer Science & EngineeringHCM University of Technology, Vietnam

Page 2: Temporal Databases

22

What is temporal DB?What is temporal DB? Temporal databasesTemporal databases, encompass all DB , encompass all DB

applications that require some aspect of time when applications that require some aspect of time when organizing their information. organizing their information.

They exhibit the need for developing They exhibit the need for developing a set of a set of unifying conceptsunifying concepts for application developers to use. for application developers to use.

Temporal DB applications have been developed Temporal DB applications have been developed since the early days of database usage. However, since the early days of database usage. However, in creating these applications, it was mainly left to in creating these applications, it was mainly left to the application developers to discover, design, the application developers to discover, design, program, and implement the temporal conceptsprogram, and implement the temporal concepts..

Page 3: Temporal Databases

33

Applications of temporal dbApplications of temporal db

There are many examples of applications where There are many examples of applications where some aspect of time is needed to maintain the some aspect of time is needed to maintain the information in a DB.information in a DB.

• Health careHealth care: patient histories need to be maintained: patient histories need to be maintained• InsuranceInsurance: claims and accident histories are required: claims and accident histories are required• FinanceFinance: stock price histories need to be maintained.: stock price histories need to be maintained.• Personnel managementPersonnel management: salary and position history need : salary and position history need

to be maintainedto be maintained• BankingBanking: credit histories: credit histories

Page 4: Temporal Databases

44

This chapter will introduce some of the This chapter will introduce some of the concepts that have been developed to deal concepts that have been developed to deal with the complexity of temporal database with the complexity of temporal database applications.applications.

TerminologyTerminology Valid timeValid time. The . The valid timevalid time denotes denotes whenwhen facts facts

are true with respect to the real world.are true with respect to the real world. Transaction timeTransaction time. The . The transaction timetransaction time of a of a

database fact is the time when the fact is database fact is the time when the fact is current in the databasecurrent in the database. .

Page 5: Temporal Databases

55

Unlike valid time, transaction time may be Unlike valid time, transaction time may be associated with any database entity. associated with any database entity.

Although time is a continuous value in the real Although time is a continuous value in the real world, for database application, time is usually world, for database application, time is usually discretized as timepoints on a discretized as timepoints on a timelinetimeline..• Chronon. A Chronon. A chrononchronon is the shortest duration of time is the shortest duration of time

supported by a temporal DBMS. It is a non-supported by a temporal DBMS. It is a non-decomposable unit of time.decomposable unit of time.

Page 6: Temporal Databases

66

1. VALID-TIME TEMPORAL DATA MODEL1. VALID-TIME TEMPORAL DATA MODEL

Temporal database systems typically use relational Temporal database systems typically use relational databases, which provide well-defined data models databases, which provide well-defined data models and query languages. However, the relational and query languages. However, the relational model has two significant shortcomings regarding model has two significant shortcomings regarding temporal data: temporal data:

1. The relational model provides poor support for storing 1. The relational model provides poor support for storing complex temporal information. complex temporal information.

An example of this shortcoming is that the relational An example of this shortcoming is that the relational model does not support automatic merging of model does not support automatic merging of temporally temporally overlappingoverlapping data. data.

Page 7: Temporal Databases

77

2. 2. The SQL query language provides very limited support for The SQL query language provides very limited support for expressing temporal queries.expressing temporal queries.

Therefore, applications that work with complex Therefore, applications that work with complex

temporal data should define their owntemporal data should define their own

(1) temporal models and (1) temporal models and

(2) query systems(2) query systems

Page 8: Temporal Databases

88

Intervals, state tables and event tablesIntervals, state tables and event tables

Several extensions to the relational model have Several extensions to the relational model have been proposed to deal with two above been proposed to deal with two above shortcomings. shortcomings.

Valid-timeValid-time databasesdatabases: time factor is attached to all : time factor is attached to all tuples in a temporal table. tuples in a temporal table.

• In valid time databases, two-dimensional relational In valid time databases, two-dimensional relational tables are extended to incorporate tables are extended to incorporate timetime as a third as a third dimension. dimension.

• In these tables, every tuple holds In these tables, every tuple holds temporaltemporal information information denoting the information’s valid time.denoting the information’s valid time.

Page 9: Temporal Databases

99

Two types of temporal tables:Two types of temporal tables: - - eventevent tablestables, which hold , which hold instantinstant timestamps, and timestamps, and

- state- state tablestables, which hold , which hold interval timestampsinterval timestamps. .

EX: laboratory-test values are always stored in event EX: laboratory-test values are always stored in event tables. Information about drug treatments can be held in tables. Information about drug treatments can be held in state tables. state tables.

Temporal data in state table can be represented as Temporal data in state table can be represented as intervalsintervals, which are bounded by , which are bounded by startstart and and stop stop timepointstimepoints. .

EX: [d04:d10] is the interval with start timepoint EX: [d04:d10] is the interval with start timepoint d04d04 denoting the 4th day and stop timepoint denoting the 4th day and stop timepoint d10d10 denoting the denoting the 10th day.10th day.

Page 10: Temporal Databases

1010

Interval-extended relational modelInterval-extended relational model Since temporal data in both state tables and event Since temporal data in both state tables and event

tables can be represented as tables can be represented as intervalsintervals, we have an , we have an interval-stampinginterval-stamping method for modeling a temporal method for modeling a temporal DB. A relation in such a database is called a DB. A relation in such a database is called a historyhistory. .

Each tuple will store the temporal dimension of an Each tuple will store the temporal dimension of an entity over a closed interval; entity over a closed interval; a pair of columnsa pair of columns will will be required to represent the endpoints of the be required to represent the endpoints of the interval. interval.

This temporal data model is also called the This temporal data model is also called the interval-extended relational modelinterval-extended relational model, or , or historical historical data modeldata model..

Page 11: Temporal Databases

1111

Example temporal tableExample temporal table

A temporal table SALARYA temporal table SALARY that holds information that holds information about employees and their salariesabout employees and their salaries. .

EmpnoEmpno SalSal TTStartStart TTEndEnd

5252 1818 55 99

5252 2020 1010 2020

5252 2525 2121 3838

5252 3131 3939 4747

5252 3838 4848 NowNow

9797 3030 1212 1717

9797 3535 1818 NowNow

Page 12: Temporal Databases

1212

Point Type of intervalsPoint Type of intervals In our temporal data model, timepoints will have In our temporal data model, timepoints will have

only a single only a single granularitygranularity, which is at the smallest , which is at the smallest level of interest in the DB applications. level of interest in the DB applications.

EX: If granularity is one day, then we can say that the EX: If granularity is one day, then we can say that the

timepoints are all values of type DATE, and type DATE is timepoints are all values of type DATE, and type DATE is the the point typepoint type of intervals. of intervals.

When we consider an interval value, [d04:d10], When we consider an interval value, [d04:d10], the interval includes its begin and end points the interval includes its begin and end points d04d04 and and d10d10, by definition. , by definition.

The interval consists of The interval consists of a set of pointsa set of points arranged arranged according to some agreed ordering. according to some agreed ordering.

Page 13: Temporal Databases

1313

A given type T is usable as a A given type T is usable as a point typepoint type of all the of all the following are defined for it:following are defined for it:

A A total orderingtotal ordering, according to which the infix operator , according to which the infix operator “>” is defined for every pair of values v1 and v2 of type T.“>” is defined for every pair of values v1 and v2 of type T.

“ “firstfirst” and “” and “lastlast” operators, which return the smallest and ” operators, which return the smallest and the largest value of T, respectively, according to the above the largest value of T, respectively, according to the above ordering.ordering.

“ “nextnext” and “” and “priorprior” operators, which return the successor ” operators, which return the successor and the predecessor, respectively, of any given value of and the predecessor, respectively, of any given value of type T.type T.

Page 14: Temporal Databases

1414

Interval operationsInterval operations Since intervals are represented as pairs of Since intervals are represented as pairs of

timepoints, comparisons between intervals are timepoints, comparisons between intervals are based on timepoint comparisons of the upper and based on timepoint comparisons of the upper and lower bounds. lower bounds.

The interval comparison operators are BEFORE, The interval comparison operators are BEFORE, AFTER, DURING, CONTAINS, OVERLAPS, AFTER, DURING, CONTAINS, OVERLAPS, MEETS, STARTS, FINISHES, and EQUAL. This MEETS, STARTS, FINISHES, and EQUAL. This set of comparisons was originally defined by Allen. set of comparisons was originally defined by Allen.

Let Let I1, I2I1, I2 be two intervals, and be two intervals, and begin(I), end(I)begin(I), end(I) be be respectively the lower bound and upper bound of the respectively the lower bound and upper bound of the interval I. The definitions of 13 interval comparisons interval I. The definitions of 13 interval comparisons are given in Table 1.are given in Table 1.

Page 15: Temporal Databases

1515

Comparison operatorComparison operator MeaningMeaning

11 I1 before I2I1 before I2 I1I1EE < I2 < I2SS

22 I1 after I2I1 after I2 I2I2EE<I1<I1SS

33

44

I1 during I2I1 during I2

I1 contains I2I1 contains I2

(I1(I1SS>I2>I2SS I1 I1EE I2 I2EE))

(I1(I1SSI2I2SS I1 I1EE < I2 < I2EE))

(I2(I2SS>I1>I1SS I2 I2EE I1 I1EE) )

(I2(I2SSI1I1SS I2 I2EE < I1 < I1EE))

55

66

I1 overlaps I2I1 overlaps I2

I1 overlapped_by I2I1 overlapped_by I2

I1I1SS < I2 < I2SS I1 I1EE > I2 > I2SS

I1I1EE < I2 < I2EE

I2I2SS < I1 < I1SS I2 I2EE > I1 > I1SS

I2I2EE < I1 < I1EE

Table 1Table 1

Page 16: Temporal Databases

1616

Comparison OperatorComparison Operator MeaningMeaning

77

88I1 meets I2I1 meets I2

I1 met_by I2I1 met_by I2I1I1EE = I2 = I2SS

I2I2EE = I1 = I1SS

99

1010I1 starts I2I1 starts I2

I1 started_by I2I1 started_by I2I1I1SS = I2 = I2SS I1 I1EE<I2<I2EE

I1I1SS = I2 = I2SS I2 I2EE<I1<I1EE

1111

1212I1 finishes I2I1 finishes I2

I1 finished_by I2I1 finished_by I2I1I1SS > I2 > I2SS I1 I1EE=I2=I2EE

I2I2SS > I1 > I1SS I1 I1EE=I2=I2EE

1313 I1 equivalent I2I1 equivalent I2 I1I1SS = I2 = I2SS I1 I1EE=I2=I2EE

We say I1 MERGES I2 if I1 and I2 satisfy any of the comparison operators from (3) to (13) in Table 1.

Page 17: Temporal Databases

1717

Fold operationFold operation Operators such as Operators such as Union, Difference, ProjectionUnion, Difference, Projection, ,

and and Cartesian productCartesian product of the standard relational of the standard relational model remain the same in the valid-time temporal model remain the same in the valid-time temporal data model.data model.

Besides, there one important operator that works Besides, there one important operator that works on temporal relations: on temporal relations: fold.fold.

Tuples in a temporal relation that agree on the Tuples in a temporal relation that agree on the explicit attribute values and that have explicit attribute values and that have adjacentadjacent or or overlappingoverlapping time intervals are candidates for time intervals are candidates for folding.folding.

Page 18: Temporal Databases

1818

DefinitionDefinition ( (Fold OperationFold Operation). When an n-ary ). When an n-ary relation R is relation R is folded folded on interval attribute Aon interval attribute Aii, 1 , 1 i i

n, all its tuples whose A n, all its tuples whose Ajj components match components match j j

i and whose A i and whose Aii components can merge, are components can merge, are

replaced in the resulting relation by a single tuple replaced in the resulting relation by a single tuple with the same Awith the same Ajj components, but in its components, but in its iith th

component is formed by a merging of the component is formed by a merging of the iith th component of these tuples.component of these tuples.

Ex: Let R be a table. After folding the table R on the Ex: Let R be a table. After folding the table R on the DurationDuration column, we get the table RF in the following slide. column, we get the table RF in the following slide.

Page 19: Temporal Databases

1919

NameName DosageDosage DurationDurationJohnJohn WadaineWadaine [4,6][4,6]

JohnJohn WadaineWadaine [1,2][1,2]

PaulPaul WadaineWadaine [1,9][1,9]

JohnJohn WadaineWadaine [6,Now][6,Now]

PaulPaul WadaineWadaine [7,12][7,12]

NameName DosageDosage DurationDurationJohnJohn WadaineWadaine [1,2][1,2]

JohnJohn WadaineWadaine [4,Now][4,Now]

PaulPaul WadaineWadaine [1,12][1,12]

R

RF

Page 20: Temporal Databases

2020

Folding the relation R on temporal attribute AFolding the relation R on temporal attribute A ii can be can be defined procedurally as.defined procedurally as.

beginbegin S:= R;S:= R; whilewhile there exist distinct tuples t1 and t2 such that there exist distinct tuples t1 and t2 such that (t1[Ai] MERGES t2[Ai]) and (t1[Aj] = t2[Aj] for all Aj (t1[Ai] MERGES t2[Ai]) and (t1[Aj] = t2[Aj] for all Aj Ai) Ai)

dodo S:= (S –{t1,t2}) S:= (S –{t1,t2}) {(t1[A{(t1[A11],…,t1[A],…,t1[Ai-1i-1], t1[A], t1[Aii] ] t2[A t2[Aii],],

t1[At1[Ai+1i+1],…,t1[A],…,t1[Ann])}])}end.end.

where the operator where the operator is defined so that is defined so that if I1 if I1 I2 = I3 then I2 = I3 then begin(I3) = min(begin(I1), begin(I2)) begin(I3) = min(begin(I1), begin(I2)) end(I3) = max(end(I1), end(I2)).end(I3) = max(end(I1), end(I2)).

Page 21: Temporal Databases

2121

Unfolded relations can arise in many ways, e.g. via a Unfolded relations can arise in many ways, e.g. via a projection or union operator, or on update or projection or union operator, or on update or insertion without enforcing folding.insertion without enforcing folding.

• Some other authors use the term Some other authors use the term packpack ([4]) or ([4]) or coalesecoalese ([2]) for ([2]) for foldfold. .

• Navathe & Admed ([7]) provided the first algorithm: sort Navathe & Admed ([7]) provided the first algorithm: sort the relation on a composite key of explicit attributes and the relation on a composite key of explicit attributes and time start, and then scan the relation, extending the time start, and then scan the relation, extending the period of some tuples and deleting other tuples. period of some tuples and deleting other tuples.

• Lorentzos ([5]) uses a similar algorithm for folding. Lorentzos ([5]) uses a similar algorithm for folding.

• Bohlen et al. ([2]) also suggest some iterative and non-Bohlen et al. ([2]) also suggest some iterative and non-iterative approaches for efficiently performing folding.iterative approaches for efficiently performing folding.

Page 22: Temporal Databases

2222

TIME NORMALIZATIONTIME NORMALIZATION This section defines different types of synchronism This section defines different types of synchronism

among time-varying attributes. It is valid to among time-varying attributes. It is valid to maintain synchronous attributes in a single maintain synchronous attributes in a single relation. relation. • We define the concept of We define the concept of temporal dependencetemporal dependence, which is , which is

used to define the notion of time normalizationused to define the notion of time normalization..

Synchronism and Temporal DependenceSynchronism and Temporal Dependence

A set of time-varying attributes (TAVs) in a given A set of time-varying attributes (TAVs) in a given relation is called relation is called synchronoussynchronous if every TVA can be if every TVA can be uniformly associated with and be directly applied uniformly associated with and be directly applied to the timestamp values in each tuple of the to the timestamp values in each tuple of the relation.relation.

Page 23: Temporal Databases

2323

. . Example 1Example 1: The Employee Relation. Here, an : The Employee Relation. Here, an

employee gets a raise in salary if and only if he or employee gets a raise in salary if and only if he or she gets a promotion, and an employee is never she gets a promotion, and an employee is never demoted. Thus, the Salary and Position form a set demoted. Thus, the Salary and Position form a set of synchronous attributesof synchronous attributes. .

EmpnoEmpno SalarySalary PositionPosition TTSS TTEE

3333 20K20K TypistTypist 1212 2424

3333 25K25K SecretarySecretary 2525 3535

4545 27K27K Jr EngrJr Engr 2828 3737

4545 30K30K Sr EngrSr Engr 3838 4242

Page 24: Temporal Databases

2424

Example 2Example 2: The relation : The relation MaintenanceMaintenance. All time-. All time-varying attributes varying attributes Part, Cond, PlacePart, Cond, Place and and CostCost collectively describe the maintenance event. These collectively describe the maintenance event. These TVAs form a TVAs form a quasi synchronousquasi synchronous set. set.

Plane#Plane# PartPart CondCond PlacePlace CostCost TTSS TTEE

9191 WheelWheel DetachedDetached AtlantaAtlanta 10001000 1010 2020

105105 DoorDoor BrokenBroken N.Y.N.Y. 20002000 3535 4747

105105 DoorDoor UnhingedUnhinged L.A.L.A. 25002500 3535 6262

142142 WingWing CrackedCracked BostonBoston 70007000 6060 7272

Page 25: Temporal Databases

2525

The relation Sal-MgrThe relation Sal-Mgr

EmpnoEmpno SalarySalary ManagerManager TTSS TTEE

5252 18K18K SmithSmith 55 99

5252 20K20K SmithSmith 1010 2020

5252 25K25K SmithSmith 2121 2929

5252 25K25K JonesJones 3030 3838

5252 31K31K JonesJones 3939 4242

5252 31K31K SmithSmith 4343 4747

5252 38K38K SmithSmith 4848 NowNow

9797 30K30K BradfordBradford 1212 1717

9797 35K35K BradfordBradford 1818 NowNow

Page 26: Temporal Databases

2626

Consider the relation Sal-Mgr. The relation shows Consider the relation Sal-Mgr. The relation shows the manager and salary of employees over a period the manager and salary of employees over a period of time. In this relation, the attributes of time. In this relation, the attributes SalarySalary and and ManagerManager form two singleton synchronous. form two singleton synchronous.

They change in an They change in an asynchronousasynchronous fashion. Such fashion. Such asynchronism leads to the fragmentation of the asynchronism leads to the fragmentation of the lifespan information of a TVA over several tuples lifespan information of a TVA over several tuples

and create update and retrieval anomalies.and create update and retrieval anomalies.

Page 27: Temporal Databases

2727

DefinitionDefinition ( (Temporal dependenceTemporal dependence). Let R be a time-). Let R be a time-varying relation, where K is its temporal varying relation, where K is its temporal invariant key, and let Xinvariant key, and let Xii, for i , for i [1,n], be its TVAs [1,n], be its TVAs and Tand TSS and T and TEE be its timestamp attributes. In a be its timestamp attributes. In a relational schema R, for any two TVAs Xrelational schema R, for any two TVAs X ii and X and Xjj (i != j), R is said to have a temporal dependency, (i != j), R is said to have a temporal dependency, XXii T T X Xjj, iff there exists an instance of R such , iff there exists an instance of R such that it contain 2 tuples t1 and t2 such that:that it contain 2 tuples t1 and t2 such that:

• t1(K) = t2(K)t1(K) = t2(K)• t1(Xt1(Xii) = t2(X) = t2(Xii) XOR t1(X) XOR t1(Xjj) = t2(X) = t2(Xjj))• intervals [t1(Tintervals [t1(TSS),t1(T),t1(TEE)] and [t2(T)] and [t2(TSS), t2(T), t2(TEE)] are )] are

adjacent.adjacent.

Page 28: Temporal Databases

2828

In Sal-Mgr, the attributes Salary and Manager, In Sal-Mgr, the attributes Salary and Manager, according to the above definition, have a according to the above definition, have a temporal temporal dependencydependency (consider two tuples (consider two tuples

<52, 18K, Smith, 5, 9> and <52, 20K, Smith, 10, 20><52, 18K, Smith, 5, 9> and <52, 20K, Smith, 10, 20>

or two tuplesor two tuples <52, 25K, Smith, 21, 29> and <52, 25K, Jones, 30, 38>).<52, 25K, Smith, 21, 29> and <52, 25K, Jones, 30, 38>).

Temporal dependency arise when two or more Temporal dependency arise when two or more temporally unrelated facts are mixed in one time-temporally unrelated facts are mixed in one time-varying relation.varying relation.

Page 29: Temporal Databases

2929

Time NormalizationTime Normalization

A relation is in A relation is in time normal formtime normal form (TNF) iff it is in (TNF) iff it is in BCNF and there exists no temporal dependency BCNF and there exists no temporal dependency among non-key attributes.among non-key attributes.

• It is always possible to decompose a relation, if a It is always possible to decompose a relation, if a temporal dependencytemporal dependency exists, into two or more time- exists, into two or more time-normalized relations by partitioning the attributes and normalized relations by partitioning the attributes and merging the relevant time intervals.merging the relevant time intervals.

• EX: EX: Sal-MgrSal-Mgr can be decomposed into two relations, can be decomposed into two relations, ManagerManager and and SalarySalary..

Page 30: Temporal Databases

3030

EmpnoEmpno MgrMgr TTSS TTEE

5252 SmithSmith 55 2929

5252 JonesJones 3030 4242

5252 SmithSmith 4343 NowNow

9797 BradfordBradford 1212 NowNow

EmpnoEmpno SalrSalr TTSS TTEE

5252 18K18K 55 99

5252 20K20K 1010 2020

5252 25K25K 2121 3838

5252 31K31K 3939 4747

5252 38K38K 4848 NowNow

9797 30K30K 1212 1717

9797 35K35K 1818 NowNow

Page 31: Temporal Databases

3131

The Need for Time NormalizationThe Need for Time Normalization The idea for time normalization is the requirement that The idea for time normalization is the requirement that

tuples be semantically tuples be semantically independent ofindependent of one another. By one another. By definition, a relation is a set; hence, its elements (tuples) definition, a relation is a set; hence, its elements (tuples) must be independent of one another. must be independent of one another.

In an unnormalized TVR, every tuple has incomplete In an unnormalized TVR, every tuple has incomplete information about the lifespan of its attributes; therefore, information about the lifespan of its attributes; therefore, it becomes dependent on other tuples for the determination it becomes dependent on other tuples for the determination of such information.of such information.

In In Sal-MgrSal-Mgr relation, the tuple <52, 20K, Smith, 10,20> does relation, the tuple <52, 20K, Smith, 10,20> does not represent the start time and end-time of the attribute not represent the start time and end-time of the attribute value “Smith” for value “Smith” for ManagerManager. This tuple has incomplete . This tuple has incomplete information regarding the lifespan of attribute value information regarding the lifespan of attribute value “Smith”.“Smith”.

An An asynchronousasynchronous change in the value of one attribute splits change in the value of one attribute splits the lifespan information of other attributes over different the lifespan information of other attributes over different tuples.tuples.

Page 32: Temporal Databases

3232

Another anomaly is a simple query may retrieve a Another anomaly is a simple query may retrieve a completely meaningless result.completely meaningless result.

Ex: “Ex: “When did Smith become the manager of employee 52When did Smith become the manager of employee 52?”?”

[Empno, Mgr, Ts][Empno, Mgr, Ts] Empno = 52 AND Mgr = ‘Smith’ Empno = 52 AND Mgr = ‘Smith’ (Sal-Mgr)(Sal-Mgr)

The following result is incorrectThe following result is incorrectEmpnoEmpno ManagerManager TsTs

--------------------------------------------------------------------------------------

5252 SmithSmith 55

5252 SmithSmith 1010

5252 SmithSmith 2121

5252 SmithSmith 4343

5252 SmithSmith 4848

Page 33: Temporal Databases

3333

The correct result should beThe correct result should beEmpnoEmpno ManagerManager TsTs

------------------------------------------------------------------------------

5252 SmithSmith 55

52 Smith52 Smith 43 43

Different time varying attributes may change at Different time varying attributes may change at entirely different rates. Therefore, there is a entirely different rates. Therefore, there is a redundant repetition of the values of a TVA that redundant repetition of the values of a TVA that is varying at a rate is varying at a rate slower thanslower than that of the other. that of the other.

Time normalizationTime normalization avoids these redundancies avoids these redundancies and update and retrieval anomalies.and update and retrieval anomalies.

Page 34: Temporal Databases

3434

3.TEMPORAL QUERY LANGUAGE3.TEMPORAL QUERY LANGUAGE A query language called TSQL, which has been A query language called TSQL, which has been

designed for querying a temporal database. TSQL designed for querying a temporal database. TSQL was proposed by S.B. Navathe and R. Ahmed, 1993. was proposed by S.B. Navathe and R. Ahmed, 1993.

TSQL is a superset of SQL and introduces several TSQL is a superset of SQL and introduces several new semantics and syntactic components.new semantics and syntactic components.

TSQL add the following new constructs to standard TSQL add the following new constructs to standard SQL:SQL:

-Conditional temporal expressions using the WHEN clause-Conditional temporal expressions using the WHEN clause

- Retrieval of timestamp values with or without computation- Retrieval of timestamp values with or without computation

Page 35: Temporal Databases

3535

--Retrieval of temporally ordered informationRetrieval of temporally ordered information

- Specification of time domain using the TIME-SLICE clause- Specification of time domain using the TIME-SLICE clause

- Modified aggregate functions and the GROUP BY clause- Modified aggregate functions and the GROUP BY clause

The formal syntax of a TSQL retrieval statement:The formal syntax of a TSQL retrieval statement:

SELECT [FIRST| SECOND|THIRD| Nth |LAST]SELECT [FIRST| SECOND|THIRD| Nth |LAST]

select_item_listselect_item_list

FROM FROM table_name_listtable_name_list

WHEN WHEN temporal_comparison_listtemporal_comparison_list

WHERE WHERE search_condition_listsearch_condition_list

Page 36: Temporal Databases

3636

Example DatabaseExample Database

TSQL will be illustrated by examples on a database TSQL will be illustrated by examples on a database with the following relational schema:with the following relational schema:

E(eno, name, address, date-of-birth)E(eno, name, address, date-of-birth)

S(eno, salr, TS, TE)S(eno, salr, TS, TE)

M(eno, mgr, TS, TE)M(eno, mgr, TS, TE)

T(eno, city, country, cost, TS, TE)T(eno, city, country, cost, TS, TE)

E stands for E stands for EmployeeEmployee, S for , S for SalarySalary, and , and

M for M for ManagerManager, T for , T for traveltravel..

Page 37: Temporal Databases

3737

Temporal Query SemanticsTemporal Query Semantics Syntax of temporal query language is an Syntax of temporal query language is an

extension of standard SQL syntax. The extension of standard SQL syntax. The semantics of a temporal query are based on semantics of a temporal query are based on the temporal relational model outlined in the temporal relational model outlined in section 1. section 1. • The temporal semantics contained in a temporal query The temporal semantics contained in a temporal query

cannot be translated in to standard relational algebra. cannot be translated in to standard relational algebra.

• We modify and extend standard relational algebra to We modify and extend standard relational algebra to create a version that incorporates create a version that incorporates temporal operationstemporal operations on on timepoints and intervals.timepoints and intervals.

• A set of algebraic operators that support temporal A set of algebraic operators that support temporal querying requirements: querying requirements:

temporal projection, selectiontemporal projection, selection, and , and joinsjoins

Page 38: Temporal Databases

3838

Assumption: well-defined tablesAssumption: well-defined tables Assume that the valid time component in temporal Assume that the valid time component in temporal

table(s) must be table(s) must be well-definedwell-defined before performing the before performing the operation. operation.

That means temporal tables do not contain tuples with That means temporal tables do not contain tuples with the same non-temporal attribute values but the same non-temporal attribute values but overlapping or consecutive time intervals. Such overlapping or consecutive time intervals. Such tuples are automatically tuples are automatically foldedfolded in advance by in advance by merging their time intervals.merging their time intervals.

Page 39: Temporal Databases

3939

Temporal projectionTemporal projection

Temporal projectionTemporal projection is similar to standard is similar to standard projection, except that the restriction applies to projection, except that the restriction applies to only the non-temporal attributes. Both timestamp only the non-temporal attributes. Both timestamp columns cannot be excluded in the resultant columns cannot be excluded in the resultant history. history.

After temporal projection, After temporal projection, foldingfolding is enforced in is enforced in order that adjoining intervals should be merged order that adjoining intervals should be merged into a single interval in the resultant relationinto a single interval in the resultant relation..

Page 40: Temporal Databases

4040

Temporal selectionTemporal selection TSQL adds the following new construct to TSQL adds the following new construct to

standard SQL: selection based on temporal standard SQL: selection based on temporal comparisons of timepoints and intervals using comparisons of timepoints and intervals using terms in a WHEN clause.terms in a WHEN clause.

The WHEN clause is used to express the temporal The WHEN clause is used to express the temporal

part of a query.part of a query. • The The temporal comparisontemporal comparison in the WHEN clause has the in the WHEN clause has the

following form:following form:

WHEN a interval_compare_operator bWHEN a interval_compare_operator b

where where a,ba,b are intervals and are intervals and interval_compare_operatorinterval_compare_operator can be one of the keywords: BEFORE, AFTER, can be one of the keywords: BEFORE, AFTER, DURING, EQUIVALENT, ADJACENT, OVERLAPS, DURING, EQUIVALENT, ADJACENT, OVERLAPS, PRECEDES, and FOLLOWSPRECEDES, and FOLLOWS..

Page 41: Temporal Databases

4141

[a,b] BEFORE [c,d] iff b < c[a,b] BEFORE [c,d] iff b < c [a,b] AFTER [c,d] iff a > d[a,b] AFTER [c,d] iff a > d [a,b] DURING [c,d] iff (a [a,b] DURING [c,d] iff (a c) & (b c) & (b d) d) [a,b] EQUIVALENT [c,d] iff ( a = c ) & (b = d )[a,b] EQUIVALENT [c,d] iff ( a = c ) & (b = d ) [a,b] ADJACENT [c,d] iff (c – b =1) | (a – d = 1)[a,b] ADJACENT [c,d] iff (c – b =1) | (a – d = 1) [a,b] OVELAP [c,d] iff ( a [a,b] OVELAP [c,d] iff ( a d) & (c d) & (c b) b) [a,b] FOLLOWS [c,d] iff (a – d = 1)[a,b] FOLLOWS [c,d] iff (a – d = 1) [a,b] PRECEDES [c,d] iff (c – b = 1)[a,b] PRECEDES [c,d] iff (c – b = 1)

The comparison operators BEFORE, AFTER, DURING, PRECEDES, and FOLLOWS are not commutative, whereas EQUIVALENT, ADJACENT, and OVELAP are.

Page 42: Temporal Databases

4242

Q1: Q1: Find the salary of employee 125 when Smith was his Find the salary of employee 125 when Smith was his managermanager..SELECT salrSELECT salrFROM S, MFROM S, MWHERE S.eno = M.eno and M.eno = 125 and WHERE S.eno = M.eno and M.eno = 125 and M.mgr = ‘Smith’M.mgr = ‘Smith’WHEN S.INTERVAL OVERLAP M.INTERVALWHEN S.INTERVAL OVERLAP M.INTERVAL

Q2: Q2: Find the manager of employee 23 who immediately Find the manager of employee 23 who immediately succeeded manager Jones and also the time of the occurrence succeeded manager Jones and also the time of the occurrence of this eventof this event..SELECT B.mgr, B.TIME-STARTSELECT B.mgr, B.TIME-STARTFROM M A, M BFROM M A, M BWHERE A.eno = B.eno AND A.eno = 23 AND WHERE A.eno = B.eno AND A.eno = 23 AND A.mgr = ‘Jones’A.mgr = ‘Jones’WHEN B.INTERVAL FOLLOWS A.INTERVALWHEN B.INTERVAL FOLLOWS A.INTERVAL

Page 43: Temporal Databases

4343

Temporal joinTemporal join This join has the most special semantics: the valid-This join has the most special semantics: the valid-

time intervals of the resultant table are created time intervals of the resultant table are created from the from the intersectionintersection of the overlapping valid-time of the overlapping valid-time elements of the tables specified in the join. elements of the tables specified in the join.

AssumptionAssumption: The valid time component in each : The valid time component in each temporal table must be temporal table must be well-definedwell-defined before before performing such joins.performing such joins.

Page 44: Temporal Databases

4444

To perform joining two temporal tables, we must first To perform joining two temporal tables, we must first assemble the non-temporal columns. The columns are assemble the non-temporal columns. The columns are assembled by generating the assembled by generating the cross productcross product of the of the non-non-temporal temporal columns from the operand tables, and then columns from the operand tables, and then excluding rows that do not satisfy the conditions in excluding rows that do not satisfy the conditions in the WHERE and WHEN.the WHERE and WHEN.

Then, we must examine the source tuples for each Then, we must examine the source tuples for each candidate tuple in the reduced cross product to see if candidate tuple in the reduced cross product to see if their valid time periods their valid time periods overlapsoverlaps. .

• If they If they overlapoverlap, the candidate tuple is included in the , the candidate tuple is included in the final join and the result of the final join and the result of the intersectionintersection of two valid of two valid time periods is used as the valid time period of the new time periods is used as the valid time period of the new tuple. tuple.

• If they do not ovelap, this tuple is excluded from the If they do not ovelap, this tuple is excluded from the result of the join. result of the join.

Page 45: Temporal Databases

4545

An example of temporal joinAn example of temporal join

PROBLEMLISTPROBLEMLISTPatientPatient ProblemProblem TSTS TETE--------------------------------------------------------------------------------------------------------------------------------------------J. SmithJ. Smith P1P1 14/Feb/199814/Feb/1998 1/Mar/19981/Mar/1998J. SmithJ. Smith P2P2 10/Mar/199810/Mar/1998 NowNowP. JonesP. Jones P3P3 1/Apr/19981/Apr/1998 12/May/199812/May/1998R. FranksR. Franks P3P3 13/Feb/199813/Feb/1998 1/Jun/19981/Jun/1998

DRUGSDRUGSPatientPatient DrugDrug VSVS VEVE----------------------------------------------------------------------------------------------------------------------------------------------J. SmithJ. Smith D1D1 20/Mar/199820/Mar/1998 12/May/199812/May/1998P. JonesP. Jones D1D1 1/Apr/19981/Apr/1998 6/Jun/19986/Jun/1998R. FranksR. Franks D2D2 4/Feb/19984/Feb/1998 14/May/199814/May/1998

““Show all problem and drug comibinations for patient”Show all problem and drug comibinations for patient”

Page 46: Temporal Databases

4646

TEMPORALTEMPORAL

SELECT T1.Patient, T1.Problem, T2.DrugSELECT T1.Patient, T1.Problem, T2.Drug

FROM PROBLEMLIST AS T1, DRUGS AS T2FROM PROBLEMLIST AS T1, DRUGS AS T2

WHERE T1.Patient = T2.PatientWHERE T1.Patient = T2.Patient

The resultant table:The resultant table:

Patient ProblemPatient Problem DrugDrug TSTS TETE------------------------------------------------------------------------------------------------------------------------------------------------------------J. SmithJ. Smith P2P2 D1D1 20/Mar/199820/Mar/1998 12/May/199812/May/1998P. JonesP. Jones P2P2 D1D1 1/Apr/19981/Apr/1998 12/May/199812/May/1998R. FranksR. Franks P3P3 D2D2 13/Feb/199813/Feb/1998 14/May/199814/May/1998

Page 47: Temporal Databases

4747

Retrieval of TimestampsRetrieval of Timestamps We showed how to retrieve data values based on We showed how to retrieve data values based on

temporal conditions. temporal conditions.

Now we show Now we show how to retrieve time points or how to retrieve time points or intervalsintervals that correspond to certain condition. For that correspond to certain condition. For retrieving the timestamp values, the target list of retrieving the timestamp values, the target list of timestamps is specified in the SELECT clause.timestamps is specified in the SELECT clause.

This target list contains the unary postfix This target list contains the unary postfix operators TIME-START or TIME-END, which operators TIME-START or TIME-END, which must be qualified by the relation name if two or must be qualified by the relation name if two or more relations participate in the query; otherwise more relations participate in the query; otherwise the relation name is implicit.the relation name is implicit.

Page 48: Temporal Databases

4848

If more than one relation participates in the If more than one relation participates in the query, however, then new timestamp values may query, however, then new timestamp values may have to be computed from those of the have to be computed from those of the participating tuples. participating tuples.

TSQL allows an operation called TSQL allows an operation called interinter (i.e. (i.e. intersectintersect) to be applied on the timestamps in the ) to be applied on the timestamps in the target list. The operator target list. The operator interinter takes two intervals takes two intervals and returns another interval which is their and returns another interval which is their intersection. intersection.

[a,b] [a,b] interinter [c,d] = [max(a,c), min(b,d)] [c,d] = [max(a,c), min(b,d)]

The underlying condition is that the time intervals must The underlying condition is that the time intervals must overlapoverlap..

Page 49: Temporal Databases

4949

Q1. Q1. List the manager and salary history of List the manager and salary history of all employees while their salary was less all employees while their salary was less than 40K. Retrieve the intersecting than 40K. Retrieve the intersecting (overlapping) time intervals.(overlapping) time intervals.

SELECT M.eno, mgr, sal, SELECT M.eno, mgr, sal,

(M inter S).TIME-START, (M inter S).TIME-START,

(M inter S). TIME-END(M inter S). TIME-END

FROM S, MFROM S, M

WHERE S.eno = M.eno AND salr < 40KWHERE S.eno = M.eno AND salr < 40K

WHEN S.INTERVAL OVERLAP M.INTERVALWHEN S.INTERVAL OVERLAP M.INTERVAL

Page 50: Temporal Databases

5050

Temporal OrderingTemporal Ordering In a temporal database, several versions of an entity are In a temporal database, several versions of an entity are

associated with each time invariant key (TIK). For a associated with each time invariant key (TIK). For a particular TIK, every version has a unique pair of particular TIK, every version has a unique pair of timestamp values associated with it. Temporal versions of timestamp values associated with it. Temporal versions of an entity have an inherent order. This means that queries an entity have an inherent order. This means that queries in a temporal database may need to refer to directly to in a temporal database may need to refer to directly to such an order.such an order.

A temporal relation is said to be A temporal relation is said to be temporally orderedtemporally ordered when when all its tuples with the same TIK are sorted in ascending all its tuples with the same TIK are sorted in ascending order by their timestamp values. order by their timestamp values.

Since no tuples for a given TIK having an overlapping Since no tuples for a given TIK having an overlapping time period and every tuple with the same TIK has a time period and every tuple with the same TIK has a unique pair timestamp, the unique pair timestamp, the sortingsorting can be done on the can be done on the starting timestamp. starting timestamp.

Page 51: Temporal Databases

5151

So a unique ordinal number is associated with the tuples of So a unique ordinal number is associated with the tuples of each TIK in the temporally ordered relation.each TIK in the temporally ordered relation.

Example:Example:enoeno salrsalr TSTS TETE

----------------------------------------------------------------------------------11 2525 16K16K 33 7722 2525 18K18K 88 131333 2525 21K21K 1717 222244 2525 25K25K 2323 262655 2525 31K31K 2727 303066 2525 34K34K 3333 353577 2525 40K40K 3636 383811 6161 17K17K 44 8822 6161 25K25K 99 111133 6161 31K31K 1212 171711 7373 18K18K 1010 161622 7373 24K24K 1717 222233 7373 30K30K 2525 3131

Page 52: Temporal Databases

5252

Temporaly ordered relations can be referred to by Temporaly ordered relations can be referred to by using the ordinal functions FIRST, SECOND, using the ordinal functions FIRST, SECOND, THIRD, Nth, and LAST as keywordsTHIRD, Nth, and LAST as keywords..

Q1: Q1: Find the time-start and salary for employees Find the time-start and salary for employees who started with a salary exceeding 50Kwho started with a salary exceeding 50K..

SELECT FIRST(salr), TIME-STARTSELECT FIRST(salr), TIME-START

FROM SFROM S

WHERE salr > 30KWHERE salr > 30K

Page 53: Temporal Databases

5353

The TIME-SLICE ClauseThe TIME-SLICE Clause The TIME-SLICE clause specifies the time period The TIME-SLICE clause specifies the time period

or time point. These specifications imply that only or time point. These specifications imply that only those tuples are selected from the underlying those tuples are selected from the underlying relations that are (fully or partially) valid for the relations that are (fully or partially) valid for the specified time period or time point.specified time period or time point.

The syntax of this clause requires the keyword The syntax of this clause requires the keyword TIME-SLICE followed by either TIME-SLICE followed by either • an interval expressed by temporal constants enclosed an interval expressed by temporal constants enclosed

within square brackets or within square brackets or

• a time point expressed by a temporal constanta time point expressed by a temporal constant..

Page 54: Temporal Databases

5454

Q1. Q1. List all changes of salary during the years List all changes of salary during the years 1972-1978 for all employees whose manager 1972-1978 for all employees whose manager was Bradfordwas Bradford..

SELECT S.eno, salr, S.TIME-STARTSELECT S.eno, salr, S.TIME-START

FROM S, MFROM S, M

WHERE S.eno = M.eno AND WHERE S.eno = M.eno AND

mgr = ‘Bradford’mgr = ‘Bradford’

WHEN M.INTERVAL OVERLAP S.INTERVALWHEN M.INTERVAL OVERLAP S.INTERVAL

TIME-SLICE year [1972, 1978]TIME-SLICE year [1972, 1978]

Page 55: Temporal Databases

5555

The current values of underlying relations can be The current values of underlying relations can be retrieved by specifying the temporal constant retrieved by specifying the temporal constant NOW in the TIME-SLICE clause.NOW in the TIME-SLICE clause.

Q2. Q2. List the manager history of all employees in the List the manager history of all employees in the last five yearslast five years..

SELECT eno, mgr, TIME-STARTSELECT eno, mgr, TIME-START

FROM MFROM M

TIME-SLICE year [NOW –5, NOW]TIME-SLICE year [NOW –5, NOW]

Page 56: Temporal Databases

5656

Aggregate Functions and GROUP BYAggregate Functions and GROUP BY In a temporal database, only time-start and time-In a temporal database, only time-start and time-

end are recorded for each tuple. However, a query end are recorded for each tuple. However, a query may refer to the length of a time interval given by may refer to the length of a time interval given by [TE – TS]. In TSQL, this is referred to simply as [TE – TS]. In TSQL, this is referred to simply as DURATION. DURATION.

TSQL allows the usual aggregate functions – TSQL allows the usual aggregate functions – max, max, min, count, avgmin, count, avg, and , and sumsum – to be applied on the – to be applied on the duration of time intervals.duration of time intervals.

Q1. Q1. Find the period of time for which employee 45 Find the period of time for which employee 45 worked under manager Jonesworked under manager Jones.. SELECT eno, SUM (DURATION)SELECT eno, SUM (DURATION)

FROM MFROM M WHERE mgr = ‘Jones’ AND eno = 45WHERE mgr = ‘Jones’ AND eno = 45

Page 57: Temporal Databases

5757

GROUP BY clauseGROUP BY clause TSQL’s GROUP BY clause is an extension of the TSQL’s GROUP BY clause is an extension of the

conventional GROUP BY. TSQL allows conventional GROUP BY. TSQL allows timestamps to be used in the GROUP BY clause. timestamps to be used in the GROUP BY clause.

Since in most cases timestamps are a combination Since in most cases timestamps are a combination of several fields (e.g. year, month, date, hour, of several fields (e.g. year, month, date, hour, minute, etc.), one or more of these fields can be minute, etc.), one or more of these fields can be specified in the GROUP BY clausespecified in the GROUP BY clause..

Page 58: Temporal Databases

5858

The following examples assume that the timestamps of The following examples assume that the timestamps of the relations have only these fields: year, month, date.the relations have only these fields: year, month, date.

Q2. Find the calendar years during which an employee Q2. Find the calendar years during which an employee made more foreign (other than U.S.) visits than made more foreign (other than U.S.) visits than domestic visits.domestic visits.

SELECT A.eno, A.TIME-START.yearSELECT A.eno, A.TIME-START.yearFROM T A, T BFROM T A, T BWHERE A.eno = B.eno AND A.country = ‘U.S.’ WHERE A.eno = B.eno AND A.country = ‘U.S.’ AND B.country != ‘U.S.’AND B.country != ‘U.S.’WHEN A.TIME-START.year = B.TIME-START.yearWHEN A.TIME-START.year = B.TIME-START.yearGROUP BY A.eno, A.TIME-START.yearGROUP BY A.eno, A.TIME-START.yearHAVING COUNT(UNIQUE B.TIME-START) > HAVING COUNT(UNIQUE B.TIME-START) >

COUNT(UNIQUE A.TIME-START)COUNT(UNIQUE A.TIME-START)

Page 59: Temporal Databases

5959

Q3. Q3. For every employee and for every year, list the For every employee and for every year, list the city that was visited most often in that year, and the city that was visited most often in that year, and the number of times it was visitednumber of times it was visited..

SELECT eno, TIME-START.year, city, MAX(COUNT(*))SELECT eno, TIME-START.year, city, MAX(COUNT(*))

FROM TFROM T

GROUP BY eno, TIME-START.year, cityGROUP BY eno, TIME-START.year, city

Page 60: Temporal Databases

6060

Insertion of dataInsertion of data A new tuple can be inserted to a temporal table A new tuple can be inserted to a temporal table

with the specified attribute values including an with the specified attribute values including an interval [interval [V_endV_end, , V_beginV_begin] which builds the initial ] which builds the initial tuple lifespan. tuple lifespan.

The syntax of the INSERT statement: The syntax of the INSERT statement:

INSERT INTO <INSERT INTO <table-nametable-name> (<> (<column-name-listcolumn-name-list>) >) VALUES <VALUES <field-name valuesfield-name values>>

When a new tuple is inserted into table, then the When a new tuple is inserted into table, then the FoldFold operator is enforced in order to merge the operator is enforced in order to merge the intervals, if necessaryintervals, if necessary..

Page 61: Temporal Databases

6161

Example:Example: INSERT INTO SALARY(eno, salr, TS, TE)INSERT INTO SALARY(eno, salr, TS, TE)

VALUES (97, 28K, 5, 11)VALUES (97, 28K, 5, 11)

ENOENO SALRSALR TSTS TETE5252 18K18K 55 99

5252 20K20K 1010 2020

5252 25K25K 2121 3838

5252 31K31K 3939 4747

5252 38K38K 4848 NowNow

9797 28K28K 55 1111

9797 30K30K 1212 1717

9797 35K35K 1818 NowNow

SALARYSALARY

Page 62: Temporal Databases

6262

Modification of dataModification of data.. When updating a temporal table, a WHEN clause When updating a temporal table, a WHEN clause

can be used to indicate the valid time associated can be used to indicate the valid time associated with the update. with the update.

The syntax of the UPDATE statementThe syntax of the UPDATE statement::UPDATE <UPDATE <table-nametable-name> >

SET <SET <column-namecolumn-name> = <> = <new value>new value>WHEN <WHEN <valid-timevalid-time>>WHERE <WHERE <condition>condition>

Only tuples that have a valid-time intersecting Only tuples that have a valid-time intersecting with the specified period in the WHEN clause are with the specified period in the WHEN clause are updated by the above commandupdated by the above command. .

Page 63: Temporal Databases

6363

Modification of data (cont.)Modification of data (cont.)

Notice that using UPDATE command may Notice that using UPDATE command may result in a relation with more tuples than result in a relation with more tuples than the original one. The explanation for this is the original one. The explanation for this is as follows.as follows.

• The new value The new value yy is assigned to a specific attribute A of a is assigned to a specific attribute A of a given tuple in an interval [given tuple in an interval [tt1,1,tt2]. Value y is specified in 2]. Value y is specified in SET clause, [SET clause, [tt1, 1, tt2] is given in the WHEN clause. Time 2] is given in the WHEN clause. Time tt1 must be specified. If 1 must be specified. If tt2 is omitted then [2 is omitted then [tt1,1, t2 t2] means ] means [[tt1,1,]. Assuming that, the specified attribute A has the ]. Assuming that, the specified attribute A has the value value xx at the time at the time tt1. Depending on [1. Depending on [tt1,1, t2 t2], there are 4 ], there are 4 cases that may happen. cases that may happen.

Page 64: Temporal Databases

6464

Modification of data (tt.)Modification of data (tt.)

ts _______________________ tets _______________________ te

t1 ___________ t2t1 ___________ t2

ts _______________________ tets _______________________ te

t1 ______________ t2t1 ______________ t2

ts ________________________ tets ________________________ te

t1 ___________ t2t1 ___________ t2

Page 65: Temporal Databases

6565

ExampleExample::UPDATE SALARY SET salr = 34UPDATE SALARY SET salr = 34

WHEN [18,25]WHEN [18,25]

WHERE eno = 97 AND salr = 35WHERE eno = 97 AND salr = 35

ENOENO SALRSALR TSTS TETE5252 18K18K 55 995252 20K20K 1010 20205252 25K25K 2121 38385252 31K31K 3939 47475252 38K38K 4848 NowNow9797 30K30K 1212 17179797 34K34K 1818 25259797 35K35K 2626 NowNow

Page 66: Temporal Databases

6666

Deletion of DataDeletion of Data When deleting data from a temporal table, a When deleting data from a temporal table, a

WHEN clause can be used to indicate the valid WHEN clause can be used to indicate the valid time associated with the deletion. time associated with the deletion.

The syntax of the DELETE statement :The syntax of the DELETE statement :

DELETE FROM <DELETE FROM <table-nametable-name> >

WHEN <WHEN <valid-timevalid-time>>

WHERE < WHERE < conditioncondition>>

Only tuples that have a valid-time intersecting Only tuples that have a valid-time intersecting with the specified period are affected by the above with the specified period are affected by the above

commandcommand

Page 67: Temporal Databases

6767

Deletion of Data (cont.)Deletion of Data (cont.)

NoteNote: Similar to UPDATE, using DELETE command may : Similar to UPDATE, using DELETE command may

result in a relation with more tuples than the original oneresult in a relation with more tuples than the original one..

ts tets te

______________________________________________

t1 __________ t2t1 __________ t2

Page 68: Temporal Databases

6868

ExampleExample::

DELETE FROM SALARYDELETE FROM SALARYWHEN [13,16]WHEN [13,16]WHERE eno = 52 AND salr = 20WHERE eno = 52 AND salr = 20

ENOENO SALRSALR TSTS TETE5252 18K18K 55 995252 20K20K 1010 12125252 20K20K 1717 20205252 25K25K 2121 38385252 31K31K 3939 47475252 38K38K 4848 NowNow9797 30K30K 1212 17179797 34K34K 1818 25259797 35K35K 2626 NowNow

Page 69: Temporal Databases

6969

Algorithm for Fold OperationAlgorithm for Fold Operation

The algorithm of The algorithm of FoldFold operation was first operation was first developed by Lorentzos, 1993.developed by Lorentzos, 1993.

This is a nested-loop algorithm, similar to the This is a nested-loop algorithm, similar to the algorithm used to duplicate elimination in algorithm used to duplicate elimination in conventional databases. conventional databases.

The algorithm of The algorithm of foldfold algorithm for a relation R algorithm for a relation R (A1,…,An, TS, TE) is given below(A1,…,An, TS, TE) is given below::

Page 70: Temporal Databases

7070

R is sorted on all its attribute and written to SR is sorted on all its attribute and written to S whilewhile not eof(S) not eof(S) dodo beginbegin read(S, a, TS, TE);read(S, a, TS, TE); whilewhile not eof(S) not eof(S) dodo beginbegin read(S, a1, T1S, T1E);read(S, a1, T1S, T1E); ifif a1 = a a1 = a thenthen if if [TS,TE] OVERLAPS [T1S,T1E] [TS,TE] OVERLAPS [T1S,T1E] then then TE := T1ETE := T1E elseelse beginbegin write(T, a, TS, TE); TS := T1S; TE := T1Ewrite(T, a, TS, TE); TS := T1S; TE := T1E endend else // else // a1 a1 a a // // beginbegin write(T, a, TS, TE); a:= a1; TS := T1S; TE := T1Ewrite(T, a, TS, TE); a:= a1; TS := T1S; TE := T1E end;end; write(T, a, TS, TE)write(T, a, TS, TE) endend endend // // T keeps the resultant relationT keeps the resultant relation////

Page 71: Temporal Databases

7171

CONCEPTUAL DESIGN AND LOGICAL CONCEPTUAL DESIGN AND LOGICAL DESIGN FOR TEMPORAL DATABASESDESIGN FOR TEMPORAL DATABASES

Conceptual Design

There’re some approaches in conceptual design for temporal DB.

Snodgrass advocates the following approach:

“ Conceptual design initially ignores the time-varying nature of the application. We focus on capturing the currently reality and temporarily ignore any history that may be useful. Only after the full design is complete, we augment the ER schema with the time-varying semantics of the application. We consider each component of ER schema in turn, annotating that component with its temporal semantics, if any. Entity types, relationship types, attributes, and keys are each individually considered.”

Page 72: Temporal Databases

7272

Nontemporal ER SchemaNontemporal ER Schema

       Strong Entity Types

     Weak Entity Types

     Entity Type Identifiers (Key Attributes)

     Attributes

     Relationship Types

Integrity Constraints

Page 73: Temporal Databases

7373

Adding Temporal AnnotationsAdding Temporal Annotations

Entity Lifespans

• Entities have a lifespan denoting when they existed. Entities are instantaneous or have a lifespan with a duration.

• If the entities of an entity type exist for all of time, there may be no need to record the lifespan explicitly. (They are non-temporal).

• Otherwise, the entity types are temporal. In this case, the designer should also specify the granularity of the lifespan.

Page 74: Temporal Databases

7474

Adding Temporal Annotations (cont.)Adding Temporal Annotations (cont.)

Relationship Valid Time

A relationship type can either model instantaneous or it can model relationships that have a duration.

The valid time for any specific relationship must be a subset of the intersection of the lifespans of the associated entities.

Page 75: Temporal Databases

7575

Adding Temporal Annotations (cont.)Adding Temporal Annotations (cont.)

Valid Time of Attributes 

• The value of an attribute may change over the lifespan of the associated entity or the valid time of the associated relationship, or may not vary over time.

• The valid time of an attribute’s value for any specific entity (or relationship) must be a subset of the lifespan of that entity (relationship).

Key Attributes 

• A time-varying key uniquely identifies a particular entity at each point in time.

• A nontemporal key (time-invariant key) identifies a particular entity over all time.

Page 76: Temporal Databases

7676

Logical Design for Temporal DBLogical Design for Temporal DB

Logical Design proceeds in two stages.

• First, the nontemporal ER schema is mapped to a nontemporal relation schema, a collection of tables. Here again we ignore the temporal aspects of the application.

• In the second stage, each of the annotations is applied to the logical schema, modifying the tables (or the integrity constraints) to accommodate that temporal aspect. We proceed in a disciplined fashion, dealing with each annotation in turn.

Mapping to Relational Schema.

 The nontemporal ER schema is mapped to a nontemporal relation schema, a collection of tables.

Page 77: Temporal Databases

7777

Applying Temporal AnnotationsApplying Temporal Annotations User-Defined-Time Attributes 

Each attribute is mapped to a column in the associated table. Attributes that record user-defined-time values can be of type: an instant, and an interval or a period. All temporal values have a granularity. 

Entity Lifespan 

To each table corresponding to an entity type for which the lifespan or valid time of an associated attribute is captured, there are two alternatives for timestamps:

-   instant

-   period (represented with two instants) 

Page 78: Temporal Databases

7878

Applying Temporal Annotations (cont.)Applying Temporal Annotations (cont.)

Relationship Valid Time 

To each table corresponding to a relationship type with a recorded valid-time extent or having attribute(s) whose valid time is recorded, we add either instant or period timestamps.

 

In short, for tables corresponding to entity and relationship types for which valid time is to be recorded, add either

- a single instant timestamp column or

- a period timestamp, represented with two instant timestamp columns.

Page 79: Temporal Databases

7979

Applying Temporal Annotations (cont.)Applying Temporal Annotations (cont.)Valid Time of Attributes

If some attributes have a valid time and if the lifespan of the associated entity or the valid time of the associated relationship is not recorded, the time-varying columns should be placed in a separate table, along with the primary key of the original table, which also serves as a foreign key to that table. 

This task is termed temporal support decomposition. 

Example: EMPLOYEE(empno, sal, sex, addr, birth-of-date,…) in which sal is an attribute with valid time, should be separated into two tables:

EMPLOYEE(empno, sex, addr, birth-of-date,…)

SALARY(empno, sal, ts, te)

Page 80: Temporal Databases

8080

Applying Temporal Annotations (cont.)Applying Temporal Annotations (cont.)

Note: When the granularity of the attribute is finer than that of the entity or relationship type to which the attribute is attached, there are two possible ways:

1. We change the granularity of the associated table (entity type) to that of the column (attribute).

2. We can break off those columns into a separate table, termed precision decomposition.

 

In short, we should decompose tables so that all attributes of a table have an identical temporal support and precision.

Page 81: Temporal Databases

8181

Applying Temporal Annotations (cont.)Applying Temporal Annotations (cont.)Temporal Keys 

The first consequence of adding valid-time support to a table is that the primary key of such tables needs to take the timestamp into consideration. Remember that the primary key of a table must be unique.

Some times, we should add one or both of the new temporal columns to the key. 

Example 1: POSITION(empno, pos, ts, te)

The key can be (empno, pos)

Example 2: SALARY(empno, sal, ts, te)

The key can be (empno, ts).

Page 82: Temporal Databases

8282

ExamplesExamples

TEACHER COURSE

RESEARCH PROJECT

participate

teach

THESIS

supervise

Example 1: teacher career management database.

Page 83: Temporal Databases

8383

Example 1: Teacher career management Example 1: Teacher career management databasedatabase

- TEACHER, THESIS, COURSE: nontemporal entity types

- RESEARCH_PROJ: an entity type with a lifespan.

- TEACH: a relationship type with valid-time.

-  -   PARTICIPATE: a relationship type with valid-time.

-   -  SUPERVISE: a relationship type with valid-time.

- Position: an attribute of TEACHER , this attibute is with a valid time.

- Trainning: an attribute of TEACHER , this attibute is with a valid time.

- Publication: an attribute of TEACHER , this attibute is with a valid time.

- Working_exp: an attribute of TEACHER , this attibute is with a valid time. 

Page 84: Temporal Databases

8484

Example 1 (cont.)Example 1 (cont.)Relational Schema 

TEACHER(empno, name, addr,….)

COURSE(course_no, title, credits,…)

RESEARCH_PROJ(proj_no, proj_name, budget, time_start, time_end, chief)

THESIS(thesisno, title, student,…)

TEACH(empno, course_no, ts, te)

PARTICIPATE(empno, proj_no, role, ts, te)

SUPERVISE(empno, thesisno, role, ts, te)

TEACHER_POSTION(empno, pos, ts, te, result)

TEACHER_TRAINING(empno, training_course, ts, te)

TEACHER_PUBLICATION(empo, title, type, detail, timeofprint)

WORKING_EXPERIENCE(empno, job-title, orgnization, ts, te)

Page 85: Temporal Databases

8585

Example 2: Banking databaseExample 2: Banking database

ACCOUNT INTEREST_TYPE

TRANSACTION

from

belongs_to

to

- ACCOUNT: nontemporal entity type

- INTEREST_TYPE: entity type with valid time

- TRANSACTION: event entity type. 

- BELONGS_TO: relationship type with valid time

- Balance: an attribute (of ACCOUNT) with valid time

Page 86: Temporal Databases

8686

Example 2 (cont.)Example 2 (cont.)

Relational Schema:

 

ACCOUNT(acc-no, date_opened,…)

INTEREST_TYPE(int-type, int_rate, ts, te)

TRANSACTION(transaction-no, event-time, event_date, type, amount, from-acc-no, to-acc-no, …)

BELONGS_TO(acc_no, int_type, ts, te)

BALANCE(acc-no, ts, te, balance)

Page 87: Temporal Databases

8787

Example 3. Clinical Temporal DatabaseExample 3. Clinical Temporal Database

PATIENT

TREATMENT

SURGERY LAB_TEST

DRUG_ MEDICATION

Page 88: Temporal Databases

8888

Example 3. (cont.)Example 3. (cont.)- PATIENT: non-temporal entity type

- TREATMENT: an entity type with a lifespan

- DRUG_MEDICATION: an entity type with a lifespan

- SURGERY an entity type with a lifespan

- LAB_TEST: an event entity type.

Note: SURGERY, DRUG_MEDICATION, LAB_TEST are weak entity types dependent on the TREATMENT entity type.

- Bed-no: an attribute (of TREATMENT) with valid time.

Page 89: Temporal Databases

8989

Example 3. (cont.)Example 3. (cont.)

Relational Schema:

 

PATIENT(p-id, name, addr, sex, age,…)

TREATMENT(file-no, p-id, disease, ts, te)

DRUD_MEDICATION(file-no, drug, dosage, ts, te)

SURGERY(file-no, op-name, op-room, ts, te)

LAB_TEST(file-no, test-name, event-time,…)

BED(file-no, bed-number, ts, te)

Page 90: Temporal Databases

9090

HOW TO IMPLEMENT A TEMPORAL HOW TO IMPLEMENT A TEMPORAL

DATABASE APPLICATIONDATABASE APPLICATION There are two commonly used methods to implement a temporal database application: 

Method 1: Firstly, build a software layer which support the temporal data model and its temporal query language on top of a RDBMS (That means the layer can analyze and process the temporal queries. The main advantage of this method is the possibility of reusing the services of the RDBMS). Then develop the database application by utilizing the layer.

Method 2: After understanding all the complexity of temporal databases, the developer develops the application using directly the SQL language and the host language supplied by RDBMS. That means he has to deal with all the complexity of temporal databases in the application programs.