Primary Key
ELI5 — The Vibe Check
A primary key is the unique ID that every row in a table must have. Like a social security number for your data — no two rows can have the same one. It is how the database knows exactly which row you mean when you say 'give me user 42'.
Real Talk
A primary key is a column or set of columns that uniquely identifies each row in a table. It enforces the UNIQUE and NOT NULL constraints automatically. Every table should have a primary key. Common types are auto-incrementing integers (SERIAL) and UUIDs.
Show Me The Code
CREATE TABLE orders (
id SERIAL PRIMARY KEY, -- auto-increments
user_id INT NOT NULL,
total DECIMAL(10,2)
);
When You'll Hear This
"Always include a primary key on every table." / "The id column is the primary key."
Related Terms
Auto Increment
Auto increment means the database assigns the next ID number automatically every time you insert a row.
Foreign Key
A foreign key is how you link two tables together. If an 'orders' table has a 'user_id' column pointing to the 'users' table, that is a foreign key.
Index
A database index is like the index in the back of a book. Without it, the database reads every single row to find what you want.
Table
A database table is exactly like a spreadsheet tab. It has columns across the top (name, email, age) and rows going down (one per person).
UUID (Universally Unique Identifier)
A UUID is a randomly generated ID that looks like 'a3b4c5d6-...' and is practically guaranteed to be unique across the entire universe.