Temporal Databases

A conventional database stores only the current state. A temporal database stores time-varying data and its history. Two kinds of time are important:

  • Valid time: when a fact is true in the real world (a salary was 30,000 from 1 Jan 2022 to 31 Dec 2023). It can be changed by corrections.
  • Transaction time: when the fact was recorded in the database; it is added by the system and cannot be changed.

A bitemporal database keeps both. Other concepts are the time point (instant), the period (interval) with a start and an end, granularity (day, month), and a special end value (such as 9999-12-31 or NOW) for facts that are still true.

Temporal Queries

Rows have columns for the start and the end of validity. Typical queries: as-of (timeslice) queries (the value on a date), history queries (all values of an employee), overlap and period comparison (Allen's interval relations: before, meets, overlaps, during), and temporal aggregates. SQL:2011 added period definitions and temporal tables.

Deductive Databases

A deductive database stores facts (the extensional database) and rules (the intensional database) from which new facts are derived by logical inference. Rules are written in Datalog, a language based on logic:

  • Fact: parent(asha, bimal).
  • Rules: ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

The second rule is recursive. Evaluation is bottom-up (start from the facts and apply rules until no new fact appears, the fixpoint) or top-down (start from the query, as in Prolog). Recursive queries are written in SQL with a recursive common table expression. Uses: genealogy, bill of materials, network reachability and knowledge bases.

Definition: Valid time. The time period during which a fact is true in the real world, as opposed to the time at which it was stored in the database.

Table 4.2: Temporal and deductive databases

Type Stores Key concept
Conventional Current state Update in place
Temporal Current and past states Valid and transaction time
Deductive Facts and rules Recursion, fixpoint

Example 4.2: An as-of query on salary history

Problem: Employee E1 has the salary history (30,000 from 2022-01-01 to 2023-12-31) and (35,000 from 2024-01-01 to 9999-12-31). Find the salary on 2023-06-01 and on 2024-06-01, and the percentage raise.

Solution:

  1. A row is valid on a date d if start <= d <= end.
  2. On 2023-06-01 the first row is valid, so the salary is 30,000.
  3. On 2024-06-01 the second row is valid, so the salary is 35,000.
  4. Raise = (35,000 - 30,000) / 30,000 = 16.7 percent.

Result: 30,000 on 2023-06-01 and 35,000 on 2024-06-01, a raise of 16.7 percent.

import sqlite3
db = sqlite3.connect(":memory:")
db.executescript("""
CREATE TABLE sal(emp TEXT, amount INTEGER, vfrom TEXT, vto TEXT);
INSERT INTO sal VALUES ('E1', 30000, '2022-01-01', '2023-12-31'),
  ('E1', 35000, '2024-01-01', '9999-12-31');
CREATE TABLE parent(p TEXT, c TEXT);
INSERT INTO parent VALUES ('A', 'B'), ('B', 'C'), ('C', 'D');
""")
q = "SELECT amount FROM sal WHERE vfrom <= ? AND ? <= vto"
for d in ("2023-06-01", "2024-06-01"):
    print(d, "->", db.execute(q, (d, d)).fetchone()[0])
rec = """WITH RECURSIVE anc(x, y) AS (
  SELECT p, c FROM parent
  UNION SELECT parent.p, anc.y FROM parent
  JOIN anc ON parent.c = anc.x)
  SELECT x, y FROM anc ORDER BY x, y"""
rows = db.execute(rec).fetchall()
print("Ancestor facts:", len(rows))
print(" ".join(x + "-" + y for x, y in rows))

Output

2023-06-01 -> 30000
2024-06-01 -> 35000
Ancestor facts: 6
A-B A-C A-D B-C B-D C-D

Common mistakes

  • Overwriting old values and losing the history.
  • Confusing valid time (real world) with transaction time (database).
  • Forgetting an end value for facts that are still true.
  • Writing recursive rules without a base case or a termination condition.

Key takeaways

  • Valid time is when the fact is true; transaction time is when it was recorded.
  • As-of queries test start <= date <= end.
  • Deductive databases hold facts and rules and derive new facts.
  • Recursion is evaluated to a fixpoint.

Practice questions

  1. What is transaction time?
  2. What is a Datalog rule?
  3. What is a bitemporal database?

Answers to the practice questions

  1. The time at which a fact was stored in the database, set by the system.
  2. A logical rule such as ancestor(X, Y) :- parent(X, Y) that derives new facts.
  3. One that stores both valid time and transaction time.

Sources

  • Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit IV