UPDATE statement
Single-table and multi-table UPDATE with ORDER BY and LIMIT, as the Raft DB parser accepts it.
UPDATE changes columns of existing rows. A statement is all or nothing.
Syntax#
UPDATE [LOW_PRIORITY] [IGNORE] [db_name.]tbl_name
SET [tbl_name.]col_name = { expr | DEFAULT } [, [tbl_name.]col_name = { expr | DEFAULT }] ...
[WHERE condition]
[ORDER BY order_expr [ASC | DESC] [, ...]]
[LIMIT row_count]
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET [tbl_name.]col_name = { expr | DEFAULT } [, ...]
[WHERE condition]table_references are the joins of SELECT: a JOIN b ON ..., a, b, or one table with an alias.
Arguments#
| Name | Meaning | Default |
|---|---|---|
LOW_PRIORITY, IGNORE | Accepted. | off |
SET col = expr | New value. DEFAULT sets the column default; for a generated column it recomputes it. | none |
WHERE | Rows to change. Without it every row changes. | all rows |
ORDER BY, LIMIT | Only for a single table without alias or join. LIMIT takes one number. | none |
Remarks#
- A statement with several tables, or one table with an alias, or a qualifier other than the table's own name in
SET, is run by the join path: the rows are found by theSELECTexecutor and each target row is changed once, with the values of the first joined row that reached it. - Assigning a value other than
DEFAULTto a generated column is error 3105. ACHECKthat fails is error 3819. A unique key that is taken is 1062. A referenced key withRESTRICTis 1451;CASCADEandSET NULLfollow to the referring rows. - Subqueries in the
SETvalues and theWHEREare supported; correlated ones take the join path. A subquery on the table being changed is error 1093. UPDATE ... LIMIThas no offset.LIMIT 1, 2is a syntax error.- Privileges:
UPDATEon the table (or columns);SELECTon the columns thatWHERE,ORDER BYand the expressions read. BEFORE UPDATEtriggers may changeNEW.col;AFTER UPDATEtriggers seeOLDandNEW. Multi-tableUPDATEruns the triggers too.- Updating a view of one table with plain columns is done on its table (error 1288 / 1471 for other views).
- A transaction can roll it back; see transactions.
Examples#
Example:
UPDATE t SET qty = -2 WHERE id = 1; -- error 3819 when a CHECK refuses it
UPDATE t SET s = DEFAULT, b = 0 WHERE id = 2;
UPDATE a JOIN b ON a.id = b.id SET a.x = b.y;
UPDATE t SET name = 'x' ORDER BY id LIMIT 2;