Lesson 14 of 30
47%
UPDATE
UPDATE changes values in rows that already exist.
SET says the new value. WHERE says which rows.
Change one student
sql
Result
Query OK, 1 row affected
| id | name | age | city |
|---|---|---|---|
| 1 | Ada | 12 | Paris |
1 row
Change more than one column
sql
Result
Query OK, 1 row affected
| id | name | age | city |
|---|---|---|---|
| 2 | Sam | 13 | Nairobi |
1 row
The scary version
If you skip WHERE, every row changes.
That is almost never what you meant.
sql
Result
Not run. Without WHERE this would change every city to Moon.
Check first
Run a SELECT with the same WHERE. Count the rows. Then UPDATE.
sql
Result
| id | name | age | city |
|---|---|---|---|
| 1 | Ada | 12 | London |
1 row
Query OK, 1 row affected
| id | name | age | city |
|---|---|---|---|
| 1 | Ada | 12 | Paris |
1 row
Tip: Prefer WHERE id = ... when you can. Names can repeat. Ids should not.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.