SQL UPDATE Statement

From GM-RKB
Jump to navigation Jump to search

A SQL UPDATE Statement is an Update Data Manipulation Statement that is a SQL data manipulation statement (in the SQL language).



References

2013

  • http://en.wikipedia.org/wiki/Update_(SQL)
    • An SQL UPDATE statement 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:[1]

      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 and the updated value must not conflict with all the applicable constraints (such as primary keys, unique indexes, CHECK constraints, and NOT NULL constraints).

      In some databases, such as PostgreSQL, when a FROM clause is present, what essentially happens is that the target table is joined to the tables mentioned in the fromlist, and each output row of the join represents an update operation for the target table. When using FROM, one should ensure that the join produces at most one output row for each row to be modified. In other words, a target row shouldn't join to more than one row from the other table(s). If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable.[2]

      Because of this indeterminacy, referencing other tables only within sub-selects is safer, though often harder to read and slower than using a join.


<source lang="sql">
UPDATE T
   SET C1 = 1
 WHERE C2 = 'a'
</source>

In table T, set the value of column C1 to 9 and the value of C3 to 4 for all rows for which the value of column C2 is "a".

<source lang="sql">
UPDATE T
  SET C1 = 9,
      C3 = 4
WHERE C2 = 'a'</source>

Increase value of column C1 by 1 if the value in column C2 is "a".

<source lang="sql">
UPDATE T
  SET C1 = C1 + 1
WHERE C2 = 'a'</source>

Prepend the value in column C1 with the string "text" if the value in column C2 is "a".

<source lang="sql">
UPDATE T
  SET C1 = 'text' || C1
WHERE C2 = 'a'</source>

Set the value of column C1 in table T1 to 2, only if the value of column C2 is found in the sublist of values in column C3 in table T2 having the column C4 equal to 0.

<source lang="sql">
UPDATE T1
  SET C1 = 2
WHERE C2 IN (SELECT C3
                FROM T2
               WHERE C4 = 0)
</source>

One may also update multiple columns in a single update statement:

<source lang="sql">
UPDATE T
  SET C1 = 1,
      C2 = 2</source>
  • Complex conditions and JOINs are also possible:
<source lang="sql">
UPDATE T
  SET A = 1
WHERE C1 = 1
  AND C2 = 2</source>

Some databases allow the non-standard use of the FROM clause:

<source lang="sql">
UPDATE a
  SET a.[updated_column] = updatevalue
 FROM articles a
      JOIN classification c
        ON a.articleID = c.articleID
WHERE c.classID = 1
</source>