The IDs that hold tables together: primary and foreign keys
Database management, lesson two. Yesterday schemas organized tables into folders; today’s keys organize rows inside them. A primary key gives every row in a table a unique identity — no duplicates, no NULLs. A foreign key links one table’s column to another table’s primary key, and the database enforces the link: you can’t insert a reading for a station that doesn’t exist. That enforcement is called referential integrity.
The code
CREATE TABLE weather_stations ( station_id INTEGER PRIMARY KEY, name TEXT, lat DOUBLE, lon DOUBLE ); CREATE TABLE temperature_readings ( reading_id INTEGER PRIMARY KEY, station_id INTEGER REFERENCES weather_stations(station_id), measured_at TIMESTAMP, temp_c DOUBLE );
Reading it piece by piece:
station_id INTEGER PRIMARY KEY— this column is the table’s unique row ID.station_id = 7always means exactly one weather station, never two, never none.station_id INTEGER REFERENCES weather_stations(station_id)— this column is a foreign key: every value must already exist as astation_idinweather_stations. It’s the column you’d join on, but the database now guards the join contract for you.- Insert a reading with
station_id = 9999when no such station exists, and the insert is rejected — the database fails your write loudly instead of letting an orphan row rot silently and break a dashboard join later.
In practice
A data engineer uses keys for three things. Documentation: anyone opening a new schema sees exactly which columns connect the tables — the join contract is written into the tables themselves. Data quality: bad pipeline writes get rejected at insert time, so a late-night ingestion bug shows up as a clear constraint error instead of a mysterious NULL in someone’s map the next morning. Speed: the database builds an index behind every primary key automatically, which also makes joins on that key fast.
Python bridge: a primary key is the database’s version of a pandas Index — the unique label you merge on. A foreign key is like a pandas merge column with an extra guarantee: the database checks that every label actually exists in the other table before the write lands, something pandas only discovers when the merge silently drops rows.
Further reading
Primary Key vs Foreign Key in SQL: How Tables Connect — dev.to (short comparison of the two keys with a side-by-side table: uniqueness, nullability, and the identity-vs-reference mental model)
Try it
Practice question: your hurricane team adds a table storm_sensors tracking sensor devices, with columns sensor_id (INT), station_id (INT), and battery_pct (DOUBLE). Write the CREATE TABLE so that sensor_id is the primary key and station_id references weather_stations(station_id).
Show answer
CREATE TABLE storm_sensors ( sensor_id INT PRIMARY KEY, station_id INT REFERENCES weather_stations(station_id), battery_pct DOUBLE );
Result: Query OK — the table is created (0 rows affected, since DDL statements don’t return rows). From now on the database rejects any sensor row whose station_id isn’t already in weather_stations — a foreign-key violation, caught at write time.