Primary keys and foreign keys
A primary key uniquely identifies each row. No two rows can share the same primary key value, and it cannot be empty.
In IMDb,
tconstis the primary key for bothtitle_basicsandtitle_ratings.A foreign key is a column in one table that points to the primary key of another table. It creates the connection between tables.
CREATE TABLE IF NOT EXISTS title_ratings (
tconst TEXT PRIMARY KEY,
average_rating REAL,
num_votes INTEGER,
FOREIGN KEY (tconst)
REFERENCES title_basics(tconst)
);👉 The FOREIGN KEY line is super important because it ensures that the data in the title_ratings table is consistent with the data in the title_basics table. It says: “the tconst column in this table MUST match a tconst that exists in title_basics.”
It will throw an error if you try to insert a tconst that does not exist in title_basics.