runtime
83 / 88

lesson 83 Warehouse & reporting projects · advanced

Durable surrogate keys across loads

Keep generated keys stable across repeated loads.

the idea

A surrogate key identifies a row inside the warehouse and must never change once assigned, even when the natural key’s attributes change. Let the database assign new keys (INTEGER PRIMARY KEY) on insert, and update attributes in place without touching the key.

Dataset: Retail

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

current_people4 columns · 40 rows
customer_keynumber · unique idcustomer_idtextcontact_nametextupdated_atdate & time
112347Arthur Turner2011-11-30 18:00:00
212353Harry Turner2011-11-30 18:00:00
312359Theo Turner2011-11-30 18:00:00
412373Harry Hill2011-11-30 18:00:00
512374Emily Hill2011-11-30 18:00:00
612384Ava Moore2011-11-30 18:00:00
customer_keynumber · unique id
1
customer_idtext
12347
contact_nametext
Arthur Turner
updated_atdate & time
2011-11-30 18:00:00
customer_keynumber · unique id
2
customer_idtext
12353
contact_nametext
Harry Turner
updated_atdate & time
2011-11-30 18:00:00
customer_keynumber · unique id
3
customer_idtext
12359
contact_nametext
Theo Turner
updated_atdate & time
2011-11-30 18:00:00
customer_keynumber · unique id
4
customer_idtext
12373
contact_nametext
Harry Hill
updated_atdate & time
2011-11-30 18:00:00
customer_keynumber · unique id
5
customer_idtext
12374
contact_nametext
Emily Hill
updated_atdate & time
2011-11-30 18:00:00
customer_keynumber · unique id
6
customer_idtext
12384
contact_nametext
Ava Moore
updated_atdate & time
2011-11-30 18:00:00

first 6 of 40 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Browse assigned surrogate keys. customer_key is stable warehouse identity; customer_id is the business natural key you load and update.the pattern

    example.sql
    SELECT customer_key, customer_id, contact_name
    FROM current_people
    ORDER BY customer_key
    LIMIT 10;
  2. Insert without supplying a key. INTEGER PRIMARY KEY assigns the next customer_key automatically when you omit it from INSERT.

    step-2.sql
    INSERT INTO current_people (customer_id, contact_name, updated_at) VALUES ('N0009', 'Test Contact', '2011-12-08 09:00:00');
    SELECT customer_key, customer_id
    FROM current_people
    ORDER BY customer_key DESC
    LIMIT 3;
  3. Update a name, keep the key. Changing contact_name does not change customer_key—facts that stored the old key still point at the same customer.

    step-3.sql
    UPDATE current_people
    SET contact_name = contact_name || ' (verified)'
    WHERE customer_key = 1;
    SELECT customer_key, customer_id, contact_name
    FROM current_people
    WHERE customer_key = 1;

common mistake

Rebuilding the table with ROW_NUMBER() renumbers existing customers whenever a new one sorts before them, breaking every fact row that stored the old key.

challenge 1

Insert customer 'N0001' named 'Ines Duarte' updated '2011-12-08 09:00:00' into current_people without supplying customer_key.

Return customer_id and customer_key for all rows.

what to return

Columns
customer_idcustomer_key in this order
Rows
any order
Also checked
TEMP / table changes from your script
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Durable surrogate keys across loads — Warehouse & reporting projects · Runtime