Update - UPDATE
UPDATE modifies existing rows in a table. Always pair it with WHERE — leave that off and it changes every row in the table.
Basic syntax
UPDATE table_name
SET column = value
WHERE condition;UPDATE— which table to modifySET— the new value(s)WHERE— which rows to apply it to
Updating one row
UPDATE books
SET price = 11.99
WHERE title = 'Dune';Output:
# Before update:
+----+-----------------+---------------+-------+----------+---------------------+
| id | title | author | price | in_stock | created_at |
+----+-----------------+---------------+-------+----------+---------------------+
| 1 | Dune | Frank Herbert | 15.50 | 1 | 2026-07-15 16:34:45 |
| 2 | 1984 | George Orwell | 9.99 | 1 | 2026-07-15 16:34:45 |
| 3 | Brave New World | Aldous Huxley | 10.25 | 1 | 2026-07-15 16:34:45 |
+----+-----------------+---------------+-------+----------+---------------------+
# After update:
+----+-----------------+---------------+-------+----------+---------------------+
| id | title | author | price | in_stock | created_at |
+----+-----------------+---------------+-------+----------+---------------------+
| 1 | Dune | Frank Herbert | 11.99 | 1 | 2026-07-15 16:34:45 |
| 2 | 1984 | George Orwell | 9.99 | 1 | 2026-07-15 16:34:45 |
| 3 | Brave New World | Aldous Huxley | 10.25 | 1 | 2026-07-15 16:34:45 |
+----+-----------------+---------------+-------+----------+---------------------+Updating multiple columns at once
Separate each column with a comma:
UPDATE books
SET price = 8.99, in_stock = FALSE
WHERE title = '1984';Updating multiple rows
WHERE can match more than one row — every matching row gets updated:
UPDATE books
SET in_stock = FALSE
WHERE price > 15;The most important rule
Forgetting WHERE updates the entire table:
-- Danger: this sets EVERY book's price to 0
UPDATE books
SET price = 0;There's no built-in undo once this runs (unless you're inside a transaction — covered in Module 03).
Preview before you update
Run the same condition as a SELECT first, to see exactly which rows will be affected:
SELECT * FROM books WHERE title = 'Dune';If that returns the rows you expect, swap it for the UPDATE with confidence.
MySQL's safety net
MySQL's "safe update mode" can block an UPDATE that doesn't use a key column in WHERE, throwing Error 1175. If you hit this, it's protecting you from an accidental mass update — not a bug.
Quick Reference
| Goal | Example |
|---|---|
| Update one row | UPDATE books SET price = 11.99 WHERE title = 'Dune'; |
| Update multiple columns | SET price = 8.99, in_stock = FALSE |
| Update multiple rows | WHERE price > 15 |
| Preview before updating | Run the same WHERE as a SELECT first |
-- Updating one row
UPDATE books
SET price = 11.99
WHERE title = 'Dune';
-- Updating multiple columns at once
UPDATE books
SET price = 8.99, in_stock = FALSE
WHERE title = '1984';
-- Updating multiple rows
UPDATE books
SET in_stock = FALSE
WHERE price > 15;