SQL reference (MySQL and MariaDB dialects)

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#

SQL
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#

NameMeaningDefault
LOW_PRIORITY, IGNOREAccepted.off
SET col = exprNew value. DEFAULT sets the column default; for a generated column it recomputes it.none
WHERERows to change. Without it every row changes.all rows
ORDER BY, LIMITOnly 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 the SELECT executor and each target row is changed once, with the values of the first joined row that reached it.
  • Assigning a value other than DEFAULT to a generated column is error 3105. A CHECK that fails is error 3819. A unique key that is taken is 1062. A referenced key with RESTRICT is 1451; CASCADE and SET NULL follow to the referring rows.
  • Subqueries in the SET values and the WHERE are supported; correlated ones take the join path. A subquery on the table being changed is error 1093.
  • UPDATE ... LIMIT has no offset. LIMIT 1, 2 is a syntax error.
  • Privileges: UPDATE on the table (or columns); SELECT on the columns that WHERE, ORDER BY and the expressions read.
  • BEFORE UPDATE triggers may change NEW.col; AFTER UPDATE triggers see OLD and NEW. Multi-table UPDATE runs 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:

SQL
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;

See also#