UPDATE

Description

The UPDATE statement is used to modify the existing records in a table.

Syntax

Single-table Syntax

  1. UPDATE table_reference
  2. SET assignment_list
  3. [WHERE where_condition]
  4. [ORDER BY ...]
  5. [LIMIT row_count]

Explanations

  • The UPDATE statement updates columns of existing rows in the named table with new values.
  • The SET clause indicates which columns to modify and the values they should be given. Each value can be given as an expression, or the keyword DEFAULT to set a column explicitly to its default value.
  • The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated.
  • If the ORDER BY clause is specified, the rows are updated in the order that is specified.
  • The LIMIT clause places a limit on the number of rows that can be updated.

Examples

  • Single-table Examples
  1. > CREATE TABLE t1 (a bigint(3), b bigint(5) primary key);
  2. > insert INTO t1 VALUES (1,1),(1,2);
  3. > update t1 set a=2 where a=1 limit 1;
  4. > select * from t1;
  5. +------+------+
  6. | a | b |
  7. +------+------+
  8. | 2 | 1 |
  9. | 1 | 2 |
  10. +------+------+

Constraints

Multiple-table Syntax is not supported for now.