Welcome back! You've seen primary keys before—as unique identifiers for table rows.
Now, let's look at primary keys from a new angle: integrity constraints. Beyond just identifying rows, primary keys enforce strict rules that keep your data accurate and consistent.
Engagement Message
Why does every table need a way to uniquely identify each row?
Primary keys act as your table's unique fingerprint. They ensure that no two rows are the same and provide a dependable way to find any specific record.
Think of them like social security numbers—each person gets exactly one, and duplicates aren't allowed.
Engagement Message
Does this sound familiar?
Here's the key point: PRIMARY KEY automatically applies both UNIQUE and NOT NULL constraints. You get both protections without needing to specify them separately.
This means primary key values can never be empty and can never repeat.
Engagement Message
If a column is a PRIMARY KEY, can it ever contain NULL values?
Creating a primary key is straightforward. Add PRIMARY KEY after the column definition:
Now customer_id uniquely identifies each customer.
Engagement Message
What happens if you try to insert two customers with the same customer_id?
Every table should have exactly one primary key. You can't have multiple primary keys, but you can have a primary key spanning multiple columns.
Multi-column primary keys work when no single column is unique enough by itself.
Engagement Message
Can you think of a situation where you'd need multiple columns to uniquely identify a row?
