VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
UPDATE changes rows that are already there. The WHERE decides which ones,
and it is the only thing standing between 1 row and the whole table.
These examples use the sakila_practice database from
INSERT. Run the setup block there again before you start,
then open the client with mysql -u root -p sakila_practice.
How UPDATE works
TABLE_NAME with the table, COLUMN_NAME with the column to change,
NEW_VALUE with what to put in it, and CONDITION with the test that picks
the rows.
The WHERE works exactly as it does in a SELECT, and that is the safest way
to write an UPDATE: run it as a SELECT first, look at the rows, then change
SELECT ... to UPDATE ... SET ... and leave the WHERE alone.
Examples
1) Change 1 row
2) Matched and Changed count different things
Run the same statement a second time.WHERE still found the row, so Rows matched is 1. The row already held
0, so nothing was written and Changed is 0. Read the 2 numbers together:
matched 0 means the WHERE found nothing, which is a different problem from
changed 0, which means you asked for a value the rows already have.
3) Change several columns at once
Separate the assignments with commas, not withAND.
An UPDATE meets the same constraints an INSERT does
Moving a value into aUNIQUE column that another row holds fails the same
way, and the row keeps what it had.
Without a WHERE it changes every row
There is no confirmation step. The statement runs and every row in the table takes the new value. A session setting can refuse that shape for you. Safe update mode rejects anUPDATE or a DELETE that neither tests a key column in its WHERE nor
carries a LIMIT, and it is off unless you ask for it.
mysql -u root -p --safe-updates sakila_practice to get
the guard from the first statement. That flag does more than set this 1
variable: it also caps a SELECT at 1,000 rows through sql_select_limit and
bounds a join through max_join_size, so a read against a large table comes
back short without saying so. ERROR 1175 means the guard stopped you.
Turn it off again when the statement really is meant to touch every row.
Rows matched: 3 Changed: 2 because 1 member was already active.
A whole-table update is sometimes exactly right. Raising every balance by 5%
is 1 statement, and the new value is worked out from the old one.
balance = balance * 1.05 reads each row’s current value, so every row gets
its own answer.
Summary
- Write
UPDATE table SET column = value WHERE conditionto change existing rows. - Run the
WHEREas aSELECTfirst, then change only the front of the statement. - Read
Rows matchedandChangedas 2 separate facts. - Separate several assignments with commas.
- Expect an
UPDATEto hit the sameUNIQUEandNOT NULLrules anINSERTdoes. - Turn on safe update mode, and read
ERROR 1175as the guard doing its job.
See also
- WHERE — the clause that decides which rows change
- DELETE — removing rows rather than changing them
- Upsert — update an existing row, or insert one if there is none
- Transactions — undoing an UPDATE that was wrong

