Database Temporal Data and Versions
What you will learn
- Why some facts need history, not just a current value
- The valid_from / valid_to pattern
- How to ask "what was true on this day?"
- How to change a value by closing a version and opening another
- How to find overlapping versions
An audit log (lesson 10.4) records edits. Sometimes the history is part of the business data: the price list, a person's address, an employee's salary, a tax rate. You must answer "what was the price on 15 February?" as easily as "what is it now?"
1. The pattern
This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It adds a small table of its own to your browser's copy; run the examples from top to bottom, and Reset data starts again.
Give each fact a period of validity:
The Shop keeps the price of product 1 over time:
The Bank keeps the monthly fee of branch 1 over time:
The School keeps the fee of course 1 over time:
CREATE TABLE price_history (
id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL, price_cents INTEGER NOT NULL,
valid_from TEXT NOT NULL, valid_to TEXT,
CHECK (valid_to IS NULL OR valid_to > valid_from));
INSERT INTO price_history (product_id, price_cents, valid_from, valid_to) VALUES
(1, 1000, '2026-01-01', '2026-03-01'),
(1, 1200, '2026-03-01', '2026-06-01'),
(1, 1100, '2026-06-01', NULL);
SELECT price_cents, valid_from, valid_to FROM price_history ORDER BY valid_from;
price_cents valid_from valid_to
----------- ---------- ----------
1000 2026-01-01 2026-03-01
1200 2026-03-01 2026-06-01
1100 2026-06-01 NULL
(3 rows)
CREATE TABLE fee_history (
id INTEGER PRIMARY KEY, branch_id INTEGER NOT NULL, fee_cents INTEGER NOT NULL,
valid_from TEXT NOT NULL, valid_to TEXT,
CHECK (valid_to IS NULL OR valid_to > valid_from));
INSERT INTO fee_history (branch_id, fee_cents, valid_from, valid_to) VALUES
(1, 1000, '2026-01-01', '2026-03-01'),
(1, 1200, '2026-03-01', '2026-06-01'),
(1, 1100, '2026-06-01', NULL);
SELECT fee_cents, valid_from, valid_to FROM fee_history ORDER BY valid_from;
fee_cents valid_from valid_to
--------- ---------- ----------
1000 2026-01-01 2026-03-01
1200 2026-03-01 2026-06-01
1100 2026-06-01 NULL
(3 rows)
CREATE TABLE fee_schedule (
id INTEGER PRIMARY KEY, course_id INTEGER NOT NULL, fee_cents INTEGER NOT NULL,
valid_from TEXT NOT NULL, valid_to TEXT,
CHECK (valid_to IS NULL OR valid_to > valid_from));
INSERT INTO fee_schedule (course_id, fee_cents, valid_from, valid_to) VALUES
(1, 1000, '2026-01-01', '2026-03-01'),
(1, 1200, '2026-03-01', '2026-06-01'),
(1, 1100, '2026-06-01', NULL);
SELECT fee_cents, valid_from, valid_to FROM fee_schedule ORDER BY valid_from;
fee_cents valid_from valid_to
--------- ---------- ----------
1000 2026-01-01 2026-03-01
1200 2026-03-01 2026-06-01
1100 2026-06-01 NULL
(3 rows)
Convention: the period is half-open: it includes valid_from and excludes valid_to. So a version ending on 1 March and the next starting on 1 March do not both claim that day. A NULL valid_to means "still valid". Dates use the ISO text form (lesson 10.2).
Quick check: In a table that keeps versions with valid_from and valid_to, what question can it answer that a table holding only the current value cannot?
What was true on a given past day, for example which value applied last March.
2. What was true on this day?
One query answers it for any day. Here it is for six days at once, by joining to a small list of days:
WITH days(day) AS (
VALUES ('2026-02-15'), ('2026-03-01'), ('2026-05-31'), ('2026-06-01'), ('2025-12-31'), ('2027-01-01')
)
SELECT d.day, (SELECT price_cents FROM price_history
WHERE product_id = 1 AND valid_from <= d.day AND (valid_to IS NULL OR d.day < valid_to)) AS value_on_that_day
FROM days d;
day value_on_that_day
---------- -----------------
2026-02-15 1000
2026-03-01 1200
2026-05-31 1200
2026-06-01 1100
2025-12-31 NULL
2027-01-01 1100
(6 rows)
WITH days(day) AS (
VALUES ('2026-02-15'), ('2026-03-01'), ('2026-05-31'), ('2026-06-01'), ('2025-12-31'), ('2027-01-01')
)
SELECT d.day, (SELECT fee_cents FROM fee_history
WHERE branch_id = 1 AND valid_from <= d.day AND (valid_to IS NULL OR d.day < valid_to)) AS value_on_that_day
FROM days d;
day value_on_that_day
---------- -----------------
2026-02-15 1000
2026-03-01 1200
2026-05-31 1200
2026-06-01 1100
2025-12-31 NULL
2027-01-01 1100
(6 rows)
WITH days(day) AS (
VALUES ('2026-02-15'), ('2026-03-01'), ('2026-05-31'), ('2026-06-01'), ('2025-12-31'), ('2027-01-01')
)
SELECT d.day, (SELECT fee_cents FROM fee_schedule
WHERE course_id = 1 AND valid_from <= d.day AND (valid_to IS NULL OR d.day < valid_to)) AS value_on_that_day
FROM days d;
day value_on_that_day
---------- -----------------
2026-02-15 1000
2026-03-01 1200
2026-05-31 1200
2026-06-01 1100
2025-12-31 NULL
2027-01-01 1100
(6 rows)
The boundary days land in the new version (2026-03-01 gives 1200), and a day before any version gives NULL. "Current value" is the same query with today's date.
3. Changing the value
Never overwrite the old row. Close the open version and insert a new one, in one transaction (lesson 6.2):
BEGIN;
UPDATE price_history SET valid_to = '2026-09-01' WHERE product_id = 1 AND valid_to IS NULL;
INSERT INTO price_history (product_id, price_cents, valid_from) VALUES (1, 1300, '2026-09-01');
COMMIT;
SELECT price_cents, valid_from, valid_to FROM price_history ORDER BY valid_from;
price_cents valid_from valid_to
----------- ---------- ----------
1000 2026-01-01 2026-03-01
1200 2026-03-01 2026-06-01
1100 2026-06-01 2026-09-01
1300 2026-09-01 NULL
(4 rows)
BEGIN;
UPDATE fee_history SET valid_to = '2026-09-01' WHERE branch_id = 1 AND valid_to IS NULL;
INSERT INTO fee_history (branch_id, fee_cents, valid_from) VALUES (1, 1300, '2026-09-01');
COMMIT;
SELECT fee_cents, valid_from, valid_to FROM fee_history ORDER BY valid_from;
fee_cents valid_from valid_to
--------- ---------- ----------
1000 2026-01-01 2026-03-01
1200 2026-03-01 2026-06-01
1100 2026-06-01 2026-09-01
1300 2026-09-01 NULL
(4 rows)
BEGIN;
UPDATE fee_schedule SET valid_to = '2026-09-01' WHERE course_id = 1 AND valid_to IS NULL;
INSERT INTO fee_schedule (course_id, fee_cents, valid_from) VALUES (1, 1300, '2026-09-01');
COMMIT;
SELECT fee_cents, valid_from, valid_to FROM fee_schedule ORDER BY valid_from;
fee_cents valid_from valid_to
--------- ---------- ----------
1000 2026-01-01 2026-03-01
1200 2026-03-01 2026-06-01
1100 2026-06-01 2026-09-01
1300 2026-09-01 NULL
(4 rows)
The value on 31 August is still 1100, and on 1 September it is 1300.
4. Catching overlaps
Nothing above stops two versions from covering the same day, which would make the "value on this day" query ambiguous. Two periods overlap when each starts before the other ends. This query finds such pairs (a NULL end is treated as "far future"):
SELECT a.id, b.id FROM price_history a JOIN price_history b
ON a.product_id = b.product_id AND a.id < b.id
AND a.valid_from < COALESCE(b.valid_to, '9999-12-31') AND b.valid_from < COALESCE(a.valid_to, '9999-12-31');
id id
-- --
(0 rows)
A mistaken row for 1 to 10 February overlaps the first version. Insert it and run the check again:
INSERT INTO price_history (product_id, price_cents, valid_from, valid_to) VALUES (1, 999, '2026-02-01', '2026-02-10');
SELECT a.id, b.id FROM price_history a JOIN price_history b
ON a.product_id = b.product_id AND a.id < b.id
AND a.valid_from < COALESCE(b.valid_to, '9999-12-31') AND b.valid_from < COALESCE(a.valid_to, '9999-12-31');
id id
-- --
1 5
(1 row)
SELECT a.id, b.id FROM fee_history a JOIN fee_history b
ON a.branch_id = b.branch_id AND a.id < b.id
AND a.valid_from < COALESCE(b.valid_to, '9999-12-31') AND b.valid_from < COALESCE(a.valid_to, '9999-12-31');
id id
-- --
(0 rows)
A mistaken row for 1 to 10 February overlaps the first version. Insert it and run the check again:
INSERT INTO fee_history (branch_id, fee_cents, valid_from, valid_to) VALUES (1, 999, '2026-02-01', '2026-02-10');
SELECT a.id, b.id FROM fee_history a JOIN fee_history b
ON a.branch_id = b.branch_id AND a.id < b.id
AND a.valid_from < COALESCE(b.valid_to, '9999-12-31') AND b.valid_from < COALESCE(a.valid_to, '9999-12-31');
id id
-- --
1 5
(1 row)
SELECT a.id, b.id FROM fee_schedule a JOIN fee_schedule b
ON a.course_id = b.course_id AND a.id < b.id
AND a.valid_from < COALESCE(b.valid_to, '9999-12-31') AND b.valid_from < COALESCE(a.valid_to, '9999-12-31');
id id
-- --
(0 rows)
A mistaken row for 1 to 10 February overlaps the first version. Insert it and run the check again:
INSERT INTO fee_schedule (course_id, fee_cents, valid_from, valid_to) VALUES (1, 999, '2026-02-01', '2026-02-10');
SELECT a.id, b.id FROM fee_schedule a JOIN fee_schedule b
ON a.course_id = b.course_id AND a.id < b.id
AND a.valid_from < COALESCE(b.valid_to, '9999-12-31') AND b.valid_from < COALESCE(a.valid_to, '9999-12-31');
id id
-- --
1 5
(1 row)
The mistaken row 5 (1 to 10 February) overlaps row 1. Run such a check regularly, or enforce it where the system allows: SQLite has no built-in rule for it, and some other systems have special "exclusion" constraints for ranges, so check your own system's documentation.
5. Two kinds of time
| Valid time | Transaction time | |
|---|---|---|
| Question | when was it true in the real world? | when did the database learn it? |
| Example | the price applied from 1 March | we typed it in on 3 March |
| Where | valid_from, valid_to |
an audit log (lesson 10.4) |
A table with both is called bitemporal. Many applications need only valid time, and add an audit log for the second.
Worked example: an invoice must use the price of its day
An invoice dated 20 February must be reproducible next year. Two choices:
- Store only the product id and look up
price_on('2026-02-20')whenever the invoice is shown (works because history is kept). - Copy the price into the invoice line when it is created (a snapshot), so it never depends on later changes.
Snapshot is simpler and safer for legal documents; the history table is still useful for reports. Data is made up.
Common mistakes
- Overwriting the old value and losing the past.
- Inclusive on both ends of the period, so boundary days belong to two versions.
- Forgetting to close the old version, leaving two "current" rows.
- Closing and inserting in separate transactions.
- Never checking for overlaps.
Key takeaways
- Keep versions of facts that change, each with a validity period.
- Use half-open periods: include
valid_from, excludevalid_to;NULLend means current. - "As of a day" is a simple range query.
- Change by closing the old version and inserting the new, in one transaction.
- Check for overlapping versions; valid time and transaction time are different things.
Practice
1. What does valid_to IS NULL mean?
Show answer
The version is still current: it has no end date.
2. Why half-open periods?
Show answer
So the end day of one version is the start day of the next without both claiming it.
3. Which price applies on 2026-06-01 in the lesson's data, and why?
Show answer
1100, because the 1200 version ends on 2026-06-01 (excluded) and the 1100 version starts that day (included).
4. Why change the price with UPDATE ... valid_to plus an INSERT in one transaction?
Show answer
So there is never a moment with zero or two current versions.
5. Difference between valid time and transaction time?
Show answer
Valid time is when the fact was true in the real world; transaction time is when the database recorded it.
6. Invoices: look up the historical price, or copy the price into the invoice? Argue one side.
Show answer
Copying (a snapshot) is safer for legal documents because later data changes or mistakes cannot alter an issued invoice; looking up works if the history is trusted and complete.