Lesson 19 of 30
63%
Primary Keys
A primary key uniquely names a row.
No two rows share that value. It is never NULL.
Think of a locker number. Each locker has one. No two share it.
A simple key
AUTO_INCREMENT lets MySQL pick the next integer for you.
sql
Result
Query OK, 0 rows affected
Insert without an id
Skip the id column. MySQL fills it.
sql
Result
Query OK, 2 rows affected
| id | name | age |
|---|---|---|
| 1 | Ada | 12 |
| 2 | Sam | 11 |
2 rows
Why it matters
1
UPDATE and DELETE can target one row
2
Other tables can point here
3
Lookups by id stay fast
One primary key per table
You can use two columns together as a composite key, but one integer id is the usual start.
Tip: Do not reuse old ids for new people. Let AUTO_INCREMENT keep climbing.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.