← Complete program

Chapter 12 · Course 3 c12-m03

How do we keep older versions?

Your goal Keep two successive versions of a label without gaps or overlaps and find the one valid at a chosen time.

The lesson, the vocabulary, the worked example and the mission are separate blocks. You can follow them in order or return to the one you need.

Video explanation

This English explanation has English subtitles. Its examples may use a separate practice dataset; use the data stated in the written mission for your answers.

01

Story · optional context

The story so far

You are Aster Motors' new data recruit. Luca was injured when a car resembling Noctis 01 crashed; the prototype vanished and Voss tries to blame you with an authorization bearing your name. Mara Sol, your data lead, has shown that the NOCTIS-01 label referred to the prototype before 20:00 and a test shell afterward in a copy of the registry. Imani worries that viewing only the current record will erase this distinction. Build a readable history that lets the team check what the registry said at each time.

This lesson stands alone. A dimension is a description, for example “this label identifies this asset.” If that description changes, replacing the old value loses the past. The method called SCD type 2 preserves the old row with an end time and creates a new row with a start time. You will try it in a fictional table and check its boundaries. Registry history remains a source to corroborate; by itself it does not show who was driving.

02

Before the lesson

Purpose of this course

Keep two successive versions of a label without gaps or overlaps and find the one valid at a chosen time.

Topics covered

Explain the difference between replacing a value and keeping SCD type 2 history.; Read inclusive valid_from and exclusive valid_to, using NULL for the current version.; Close the older version and open the new one in a practice transaction.; Check that a label has one current version and its periods do not overlap.

What you will learn

  • Explain the difference between replacing a value and keeping SCD type 2 history.
  • Read inclusive valid_from and exclusive valid_to, using NULL for the current version.
  • Close the older version and open the new one in a practice transaction.
  • Check that a label has one current version and its periods do not overlap.

PART 1 · THE LESSON

Understand step by step

1

Lesson · explanation

Today's record cannot tell the whole past

You are Aster Motors' new data recruit. Luca was hurt in a crash involving a car resembling Noctis 01, and the prototype vanished. Voss tries to blame you with an authorization bearing your name. In a registry copy, Mara finds that label NOCTIS-01 first identified PROTOTYPE-01, then SHELL-02, a test shell. Voss displays only today's entry to deny the earlier version.

A dimension is a description used to understand other facts. Here it answers 'which physical asset carried label NOCTIS-01 at this time?' Picture a box keeping its label while its contents change. A photograph taken today cannot reveal what was in the box last week. You need to keep successive descriptions rather than a single current answer.

If you simply replace PROTOTYPE-01 with SHELL-02 in one row, a search at 19:30 will show the shell although the copy named the prototype then. That paints the past in today's colors. SCD type 2, a historical dimension method, adds a row for each meaningful version and leaves the earlier row available.

Counterexample: conclude Voss is right because the current screen says SHELL-02. It displays one version, not the whole history. Conversely, finding an older row does not prove who drove the crashed car. Imani must compare the registry with independent evidence. This lesson builds a fictional practice table without altering the original.

Pause and think

Why not replace PROTOTYPE-01 with SHELL-02 in the only old row?

See the explanation

It would erase the earlier description and return a false answer for a 19:30 search.

2

Lesson · explanation

Two identities: the label and the version

The practice table is label_history. Each row has version_id, vehicle_label, physical_asset, valid_from, and valid_to. vehicle_label is the visible label, NOCTIS-01. physical_asset is the object it named: first PROTOTYPE-01, later SHELL-02. version_id identifies one particular history row, L1 for the first version and L2 for the next.

Think of a book that keeps its title through several editions. The first and second editions have the same title but may contain different text. The title resembles vehicle_label, while the edition number resembles version_id. L1 and L2 therefore both have vehicle_label = 'NOCTIS-01'. They are intentional historical versions, not accidental duplicate rows.

valid_from says when a description begins to apply. valid_to says when it stops, if an end is known. That lets you ask which version was valid for an event at 19:30 or 21:25. Current version L2 has valid_to = NULL, meaning no end is recorded yet. NULL does not mean zero o'clock or automatically verified forever.

Counterexample: make vehicle_label unique across the whole table. A second NOCTIS-01 row would be forbidden, making this history impossible. version_id is the unique row key; another rule prevents two versions of the same label from starting at exactly the same instant. Even that does not check every possible clash between periods.

Pause and think

Why may L1 and L2 both carry vehicle_label = 'NOCTIS-01'?

See the explanation

They describe one label during different periods, with different version IDs and physical assets.

3

Lesson · explanation

[start, end) periods without a gap or double answer

The practice source says the effective change occurred April 12, 2026 at 20:00 UTC. L1 says NOCTIS-01 → PROTOTYPE-01 from April 1 at 00:00 UTC until April 12 at 20:00, excluding that end. L2 says NOCTIS-01 → SHELL-02 from that exact 20:00, including its start. The two rows use one identical boundary.

[start, end) includes the start and excludes the end. Picture two chapters in a film: the first stops just before a turning-point frame, and the second starts with that frame. The frame belongs only to chapter two. At 19:59:59, only L1 applies; at 20:00:00, only L2 applies.

To find a version for instant t, use valid_from <= t AND (valid_to IS NULL OR t < valid_to). L1 fails at 20:00 because t < valid_to is false when they are equal. L2 passes because valid_from <= t is true and its end remains open. This test provides a precise time-based answer.

Counterexample: end L1 at 19:59 and start L2 at 20:00. At 19:59:30 no row applies: a gap. Start L2 at 19:59:30 while L1 ends at 20:00 and both rows apply at 19:59:45: an overlap. A reliable history checks for both situations rather than assuming adjacent versions are well formed.

Pause and think

Which version applies exactly on April 12 at 20:00:00 UTC?

See the explanation

L2. Its start is included, while L1's end is excluded.

4

Lesson · explanation

Close L1 and open L2 in one transaction

Initially, practice-copy L1 starts April 1 and has valid_to = NULL: it is open. To record the change, the program runs BEGIN;, then UPDATE label_history SET valid_to = '2026-04-12T20:00:00Z' WHERE version_id = 'L1' AND valid_to IS NULL;. This closes L1 only if it is still the open row.

Next the program inserts L2 with the same vehicle_label, physical_asset = 'SHELL-02', valid_from = '2026-04-12T20:00:00Z', and valid_to = NULL. After checking, COMMIT; keeps both writes. A transaction is like an envelope containing two pages of a case file: the old version must close as the new version opens, or the story is incomplete.

If the L2 insert fails before COMMIT, handle the error and issue ROLLBACK; so you do not leave a closed L1 without a replacement. A real service should also verify that the UPDATE changed exactly one open row and that the source supports this effective time. The small SQL example does not automate all those checks.

Counterexample: close L1 and commit immediately, then discover that L2 could not be created. The registry would have a gap from 20:00 onward. Grouping both operations protects the practice write. It cannot prove the source authentic: a transaction controls how the copy is written, not whether the supplied time is true.

Pause and think

Why put L1's UPDATE and L2's INSERT between BEGIN and COMMIT?

See the explanation

To keep both changes together or cancel the attempt if creating the replacement fails.

5

Lesson · explanation

Find the version for an earlier event

Suppose a garage check happened April 12 at 19:30 UTC. Compare it with both periods. It follows L1's start and precedes its end at 20:00, so the applicable description is PROTOTYPE-01. It is earlier than L2's start, so the shell cannot be this small history's answer for 19:30.

Now consider a check at 21:25 UTC the same day. L1 has ended; L2 started at 20:00 and remains open. The applicable description is SHELL-02. This is sometimes called an 'as of' lookup. Like replaying an earlier film scene, it asks which version applied at that scene's time, not what the current screen displays.

At exactly 20:00, the answer switches: L1 excludes its end and L2 includes its start. The example's SQL times use a consistent UTC text format ending in Z. In a real system, instants need careful normalization; mixed formats or zones could fool a naive text comparison. All practice values here use the same form.

Counterexample: use a current photograph of SHELL-02 to say the registry already named it at 19:30. That confuses current and historical states. Conversely, preserved L1 does not automatically identify the crashed vehicle. Match the event time to the right version, then check the event's provenance and the registry's provenance.

Pause and think

What does the history return at 19:30 and then 21:25 UTC on April 12?

See the explanation

At 19:30, L1 / PROTOTYPE-01; at 21:25, L2 / SHELL-02.

6

Lesson · explanation

Check current rows, gaps, and overlaps

The practice table has CHECK (valid_to IS NULL OR valid_from < valid_to). This rejects a recorded end before or equal to the start. It also has UNIQUE (vehicle_label, valid_from), which prevents two rows of one label from starting at exactly the same instant. Helpful as these rules are, they do not guarantee a complete history without overlapping periods.

Count current NOCTIS-01 versions with WHERE vehicle_label = 'NOCTIS-01' AND valid_to IS NULL: expect 1, L2. Picture one door open toward the present. A result of zero means no row remains open; two means two descriptions claim to be current. Either result requires Imani to inspect the rows before publishing a conclusion.

Also look for intersecting periods and gaps between successive versions. Two rows can start at different times yet overlap for ten minutes; UNIQUE permits that. Two rows can leave a minute with no version; CHECK permits that. For L1 and L2, confirm L1's valid_to exactly equals L2's valid_from.

Counterexample: see one current version and declare the entire history perfect. Older rows might still intersect. The simple schema illustrates SCD type 2, but a real system needs a non-overlap test or suitable database constraint, source checks, and confirmation of how many rows the update touched.

Pause and think

Do CHECK and UNIQUE(vehicle_label, valid_from) prevent every overlap?

See the explanation

No. Periods starting at different times can still intersect; add a period-overlap check.

7

Lesson · explanation

Effective time, recording time, and evidence limits

The change is effective at 20:00 UTC: according to the practice source, SHELL-02 starts applying then. Someone might have entered that update later, perhaps the next day. Effective time and recording time answer different questions. A retroactive correction may require an investigation to preserve both clocks and the reason for the change.

Imagine a museum correcting a label today to say a painting moved rooms yesterday at 20:00. The correction was typed today, while the move is said to apply from yesterday. Keeping only one time could create a false sequence. In our simple table, valid_from and valid_to describe effective time, not the full history of edits.

For the mission, Mara keeps L1 and L2 with exact periods, results at 19:30 and 21:25, a count of one current version, and the boundary checks. She preserves the original registry and notes the source of the 20:00 time. This can challenge Voss's claim that the registry 'never named the prototype' within the limits of this copy.

Counterexample: say the history proves who drove or who changed the entry. Two versions describe what a source says in two periods; they do not identify the editor or the crash vehicle. Imani must compare access records, originals, and other traces. The scene's deadline is fictional; no real timer blocks your learning.

Pause and think

Does L2's valid_from prove when someone typed the change?

See the explanation

No. It is the effective time according to the source; recording could happen later.

Visual guide

Two successive NOCTIS-01 versions

  1. 1L1: PROTOTYPE-01, April 1 to April 12 20:00 exclusive
  2. 2Boundary: April 12 20:00 UTC
  3. 3L2: SHELL-02, from the boundary with an open end

L1 remains queryable after L2 arrives; periods must not cross or leave an unexplained gap.

SQL · query or table structure

Close L1 then open L2 in a copy

CREATE TABLE label_history (
    version_id TEXT PRIMARY KEY,
    vehicle_label TEXT NOT NULL,
    physical_asset TEXT NOT NULL,
    valid_from TEXT NOT NULL,
    valid_to TEXT,
    CHECK (valid_to IS NULL OR valid_from < valid_to),
    UNIQUE (vehicle_label, valid_from)
);
INSERT INTO label_history VALUES
    ('L1', 'NOCTIS-01', 'PROTOTYPE-01', '2026-04-01T00:00:00Z', NULL);

BEGIN;
UPDATE label_history
SET valid_to = '2026-04-12T20:00:00Z'
WHERE version_id = 'L1' AND valid_to IS NULL;
INSERT INTO label_history VALUES
    ('L2', 'NOCTIS-01', 'SHELL-02', '2026-04-12T20:00:00Z', NULL);
COMMIT;

SELECT version_id, physical_asset, valid_from, valid_to
FROM label_history
ORDER BY valid_from;
SELECT COUNT(*) AS current_versions
FROM label_history
WHERE vehicle_label = 'NOCTIS-01' AND valid_to IS NULL;

The script creates a practice table, inserts L1, then groups the closure of L1 and insertion of L2 in a transaction. The first final query returns both versions and dates; the second gives one current version. Real systems need an additional no-overlap check and source verification.

PART 2 · WATCH IT WORK

A complete worked example

Follow each step, then the result

Worked example: the changeover instant

NOCTIS-01 identifies PROTOTYPE-01 before 20:00 UTC on April 12; the practice source says SHELL-02 from 20:00.

  1. Keep L1 with valid_from on April 1 and set its valid_to to 20:00; do not replace its physical_asset.
  2. Create L2 with the same vehicle_label, physical_asset SHELL-02, valid_from at 20:00, and valid_to NULL.
  3. At 19:59:59 only L1 applies; at 20:00:00 only L2 applies.
  4. Count current rows: only L2. Also check no other interval crosses these periods.
What this shows

SCD type 2 keeps one answer for the past and another for the present, with a recorded boundary.

How this helps with your mission

Voss shows only the current SHELL-02 record, but Mara retrieved L1. On a practice copy, keep L1 NOCTIS-01 → PROTOTYPE-01 for [2026-04-01T00:00:00Z, 2026-04-12T20:00:00Z) and add L2 NOCTIS-01 → SHELL-02 from 2026-04-12T20:00:00Z, with valid_to = NULL. Group the closure and opening between BEGIN and COMMIT, then verify one current version and adjacent periods without a gap or overlap. At 19:30 the copy answers prototype; at 21:25 it answers shell. Imani can seek the change's provenance and compare the original registry before drawing conclusions about the crash.

PART 3 · YOUR TURN

Use the lesson to solve the mission

Story scene · practical task

Mission: save the erased record

Voss shows a screenshot of the current entry, only SHELL-02, and claims the registry never named the prototype. Mara retrieves the earlier L1 version. Imani asks you to prepare a practice chronology without altering this evidence: L1 starts April 1 and the change to L2 applies April 12 at 20:00 UTC. The dossier deadline is fictional; no countdown blocks this lesson.

Your task

  1. Write both complete history rows with their [start, end) periods.
  2. Explain what an update that overwrites L1 would do instead of creating L2.
  3. Explain BEGIN and COMMIT around closing one version and opening the next.
  4. Name two checks needed before using this history in a report.

How to know you have succeeded

  • L1 ends at 20:00 exclusive; L2 begins at 20:00 inclusive and remains open.
  • Overwriting L1 loses the old description.
  • The two writes should be committed together.
  • One current version and no overlapping period or unexplained gap.
Open this mission in the browser workspace

Try code or SQL and keep a local draft. The mission questions are checked separately below.

Check the facts in your mission

Answer each factual question, then write your report in your own words. The program checks the choices; compare the report with the worked answer below.

1. Which action preserves what `NOCTIS-01` identified before and after 20:00?
2. Which version is valid exactly on April 12 at 20:00:00 UTC?
3. What does the registry copy return at 19:30 and then 21:25 UTC on April 12?
4. Why group L1's `UPDATE` and L2's `INSERT` in one transaction?
5. What additional check is needed despite `CHECK` and `UNIQUE(vehicle_label, valid_from)`?
See the worked answer
  1. L1: NOCTIS-01 → PROTOTYPE-01, [2026-04-01 00:00 UTC, 2026-04-12 20:00 UTC). L2: NOCTIS-01 → SHELL-02, [2026-04-12 20:00 UTC, open end).
  2. An UPDATE of physical_asset in L1 would make the shell appear to have been recorded even at 19:30. A new L2 row preserves the earlier claim.
  3. BEGIN opens the group; UPDATE closes L1, INSERT opens L2; COMMIT keeps both if correct. An error before commit calls for ROLLBACK.
  4. Check one valid_to NULL row for the label and no overlapping period pair. Also verify provenance and effective change time before drawing a conclusion.

You can now preserve versions and check their periods. The last lesson in this chapter assembles a reproducible report using a tested dbt model.

Before you leave

Remember this

  • SCD type 2 adds a row for each meaningful version instead of overwriting a past description.
  • version_id distinguishes L1 and L2; vehicle_label remains NOCTIS-01 in both.
  • valid_from is inclusive and valid_to exclusive; NULL marks the still-open version here.
  • L1 names PROTOTYPE-01 before April 12 at 20:00 UTC; L2 names SHELL-02 from that instant.
  • A transaction groups closing L1 and opening L2; an error before commit calls for rollback.
  • Check one current version and no gaps or overlaps; CHECK and UNIQUE alone are insufficient.
  • Effective time can differ from recording time; corroborate registry history, which does not prove who drove.
A–Z

Part of the lesson · keep nearby

New words, explained

These are the names of ideas used in the lesson above. Return here whenever a word feels unfamiliar.

History tracking
Keeping earlier versions instead of overwriting them.
SCD type 2
Method for a historical dimension: add a new row for each meaningful version.
Current version
Row whose valid period has not ended; valid_to is NULL here.
Validity period
Interval during which a description applies.
Overlap
Two versions of the same label apply at the same instant.
Transaction
A group of writes committed together or rolled back together.
Effective time
Time when a change is supposed to apply; it may differ from its recording time.

Official references