Sub topic 3
Keeping your own primary key next to the external ID
This chapter assumes you know basic SQL (INSERT, UPDATE, SELECT), primary keys, and foreign keys. It also assumes you know that data is often copied between separate systems.
Two keys, two jobs
A record that arrives from another system already has an identifier there, such as C-1001 in a CRM. That value is the external ID. It is tempting to use it as your primary key and skip a column. Don't. Give each table its own internal key and store the external ID beside it.
The two keys answer different questions:
- The internal key says which row this is in your database. You control it, and your foreign keys point at it.
- The external ID says which record this is in the source system. You don't control it, and you use it to match incoming data.
Why not use the external ID as the primary key
- The source can change its format. After a migration,
C-1001may become1001. - Two source systems can use the same value for different people. A billing system and a CRM may both have a customer
1001. - Text keys are wider than a
bigint. They are copied into every foreign key column and every index on the child tables. - The source may correct or reassign an ID. If that value is your primary key, the change touches every child row.
The table
This table has its own id. The pair (source_system, external_id) is unique, so the same external value from two systems doesn't collide.
CREATE TABLE customers (
id bigserial PRIMARY KEY,
source_system text NOT NULL,
external_id text NOT NULL,
name text NOT NULL,
email text,
UNIQUE (source_system, external_id)
);The UNIQUE constraint is what makes matching reliable. The upsert in a later chapter depends on it.
Pointing child tables at the internal key
Child rows reference customers.id, never the external ID.
CREATE TABLE orders (
id bigserial PRIMARY KEY,
customer_id bigint NOT NULL REFERENCES customers (id),
total numeric(10, 2) NOT NULL
);When an order arrives carrying only the customer's external ID, translate it to the internal key during the load:
INSERT INTO orders (customer_id, total)
SELECT id, 49.90
FROM customers
WHERE source_system = 'crm'
AND external_id = 'C-1001';If the customer isn't loaded yet, the SELECT returns no rows and nothing is inserted. Load parents before children, or check the inserted row count.
What you gain
Suppose the CRM renumbers C-1001 to 1001. You run one UPDATE on one customers row. Every order still points at the same id, so nothing else changes.
Your own key also stays stable when you add a second source system. A new source_system value is just new data, not a schema change.
Common mistakes
- Making
external_idthe primary key because it looks unique today. - Making
external_idunique on its own when more than one source system feeds the table. - Leaving
external_idnullable. A null never equals another null in a unique index, so rows without an ID can duplicate silently. - Exposing the internal
idto the source system as if it were shared. It means nothing outside your database.
Exercise
A billing system and a CRM both send you a customer with external ID 1001. Your table has external_id text UNIQUE and no source_system column. What goes wrong, and how do you fix it?
Show answer
The second system's customer either fails to insert because of the unique violation or, in an upsert, overwrites the first customer's row. Add a source_system column and make the constraint UNIQUE (source_system, external_id). Keep a separate internal id as the primary key.