https://de.wikipedia.org/w/api.php?action=feedcontributions&feedformat=atom&user=46.13.21.155Wikipedia - Benutzerbeiträge [de]2025-06-26T22:59:01ZBenutzerbeiträgeMediaWiki 1.45.0-wmf.7https://de.wikipedia.org/w/index.php?title=Update_(SQL)&diff=179502716Update (SQL)2016-11-12T15:53:51Z<p>46.13.21.155: </p>
<hr />
<div>An [[SQL]] '''UPDATE''' statement changes the data of one or more records in a [[Table (database)|table]]. Either all the rows can be updated, or a subset may be chosen using a [[Condition (SQL)|condition]].<br />
<br />
The <code>UPDATE</code> statement has the following form:<ref>http://dev.mysql.com/doc/refman/5.0/en/update.html simplified from this page</ref><br />
<br />
:'''<code>UPDATE</code>''' ''table_name'' '''<code>SET</code>''' ''column_name'' = ''value'' [, ''column_name'' = ''value ...''] ['''<code>WHERE</code>''' ''condition'']<br />
<br />
For the <code>UPDATE</code> to be successful, the user must have data manipulation privileges (<code>UPDATE</code> privilege) on the table or column and the updated value must not conflict with all the applicable constraints (such as [[primary key]]s, unique indexes, [[Check Constraint|<code>CHECK</code> constraints]], and [[Null (SQL)|<code>NOT NULL</code>]] constraints).<br />
<br />
In some databases, such as [[PostgreSQL]], when a [[From (SQL)|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.<ref>http://www.postgresql.org/docs/8.1/static/sql-update.html</ref><br />
<br />
Because of this indeterminacy, referencing other tables only within sub-selects is safer, though often harder to read and slower than using a join.<br />
<br />
MySQL does not conform to ANSI standard. [http://stackoverflow.com/questions/40562962/update-a-table-column-then-the-other-column-with-updated-value-of-the-former-m]<br />
<br />
==Examples==<br />
Set the value of column ''C1'' in table ''T'' to 1, only in those rows where the value of column ''C2'' is "a".<br />
<br />
<source lang="sql"><br />
UPDATE T<br />
SET C1 = 1<br />
WHERE C2 = 'a'<br />
</source><br />
<br />
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".<br />
<br />
<source lang="sql"><br />
UPDATE T<br />
SET C1 = 9,<br />
C3 = 4<br />
WHERE C2 = 'a'</source><br />
<br />
Increase value of column ''C1'' by 1 if the value in column ''C2'' is "a".<br />
<br />
<source lang="sql"><br />
UPDATE T<br />
SET C1 = C1 + 1<br />
WHERE C2 = 'a'</source><br />
<br />
Prepend the value in column ''C1'' with the string "text" if the value in column ''C2'' is "a".<br />
<br />
<source lang="sql"><br />
UPDATE T<br />
SET C1 = 'text' || C1<br />
WHERE C2 = 'a'</source><br />
<br />
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.<br />
<source lang="sql"><br />
UPDATE T1<br />
SET C1 = 2<br />
WHERE C2 IN ( SELECT C3<br />
FROM T2<br />
WHERE C4 = 0)<br />
</source><br />
One may also update multiple columns in a single update statement:<br />
<br />
<source lang="sql"><br />
UPDATE T<br />
SET C1 = 1,<br />
C2 = 2</source><br />
<br />
Complex conditions and JOINs are also possible:<br />
<br />
<source lang="sql"><br />
UPDATE T<br />
SET A = 1<br />
WHERE C1 = 1<br />
AND C2 = 2</source><br />
<br />
Some databases allow the non-standard use of the FROM clause:<br />
<source lang="sql"><br />
UPDATE a<br />
SET a.[updated_column] = updatevalue<br />
FROM articles a<br />
JOIN classification c<br />
ON a.articleID = c.articleID<br />
WHERE c.classID = 1<br />
</source><br />
<br />
Or on Oracle systems (assuming there is an index on classification.articleID):<br />
<source lang="oracle11"><br />
UPDATE<br />
(<br />
SELECT *<br />
FROM articles<br />
JOIN classification<br />
ON articles.articleID = classification.articleID<br />
WHERE classification.classID = 1<br />
)<br />
SET [updated_column] = updatevalue<br />
</source><br />
<br />
==Potential issues==<br />
* See [[Halloween Problem]]. It is possible for certain kinds of <code>UPDATE</code> statements to become an [[infinite loop]] when the <code>[[where (SQL)|WHERE]]</code> clause and one or more <code>SET</code> clauses may utilize an intertwined [[Index (database)|index]].<br />
* Forgetting to specify the <code>WHERE</code> clause leads for all records to be affected by the update.<br />
<br />
==References==<br />
{{reflist}}<br />
<br />
{{SQL}}<br />
<br />
{{DEFAULTSORT:Update (Sql)}}<br />
[[Category:SQL keywords]]<br />
[[Category:Articles with example SQL code]]<br />
<br />
<br />
{{database-stub}}</div>46.13.21.155