- Update (SQL)
A
SQL UPDATE statement that changes the data of one or more records in a table. Either all the rows can be updated, or a subset may be chosen using a condition.The
UPDATE
statement has the following form [http://dev.mysql.com/doc/refman/5.0/en/update.html simplified from this page] ::
UPDATE
"table_name"SET
"column_name" = "value" [, "column_name" = "value ..."] [WHERE
"condition"]For the
UPDATE
to be successful, the user must have data manipulation privileges (UPDATE
privilege) on the table or column, the updated value must not conflict with all the applicable constraints (such asprimary key s, unique indexes,CHECK
constraints, andNOT NULL
constraints).Examples
Set the value of column "C1" in table "T" to 1, only in those rows where the value of column "C2" is "a".
Increase value of column "C1" by 1 if the value in column "C2" is "a".
Prepend the value in column "C1" with the string "text" if the value in column "C2" is "a".
Set the value of column "C1" in table "T1" to 2, only if the value of column "C2" is found in the sub list of values in column "C3" in table "T2" having the column "C4" equal to 0. You may also update multiple columns in a single update statement:
Complex conditions are also possible:
The standard does not support updates of a joined table. Therefore, the following method needs to be used. Note that the subselect in the SET clause must be a scalar subselect, i.e. it can return at most a single row.
Wikimedia Foundation. 2010.