Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Update (SQL)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Statement in Structured Query Language)}} 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]]. 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> :'''<code>UPDATE</code>''' ''table_name'' '''<code>SET</code>''' ''column_name'' = ''value'' [, ''column_name'' = ''value ...''] ['''<code>WHERE</code>''' ''condition''] For the <code>UPDATE</code> to be successful, the user must have data manipulation privileges (<code>UPDATE</code> privilege) on the table or [[Column (database)|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). 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>{{Cite web|url=http://www.postgresql.org/docs/8.1/static/sql-update.html|title=UPDATE|date=January 2012}}</ref> Because of this indeterminacy, referencing other tables only within sub-selects is safer, though often harder to read and slower than using a join. MySQL does not conform to ANSI standard.<ref>{{Cite web|url=https://stackoverflow.com/q/40562962|title=SQL - Update a table column, then the other column with updated value of the former. MySQL / PostgreSQL differ}}</ref> ==Examples== Set the value of column ''C1'' in table ''T'' to 1, only in those rows where the value of column ''C2'' is "a". <syntaxhighlight lang="sql"> UPDATE T SET C1 = 1 WHERE C2 = 'a' </syntaxhighlight> 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". <syntaxhighlight lang="sql"> UPDATE T SET C1 = 9, C3 = 4 WHERE C2 = 'a'</syntaxhighlight> Increase value of column ''C1'' by 1 if the value in column ''C2'' is "a". <syntaxhighlight lang="sql"> UPDATE T SET C1 = C1 + 1 WHERE C2 = 'a'</syntaxhighlight> Prepend the value in column ''C1'' with the string "text" if the value in column ''C2'' is "a". <syntaxhighlight lang="sql"> UPDATE T SET C1 = 'text' || C1 WHERE C2 = 'a'</syntaxhighlight> 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. <syntaxhighlight lang="sql"> UPDATE T1 SET C1 = 2 WHERE C2 IN ( SELECT C3 FROM T2 WHERE C4 = 0) </syntaxhighlight> One may also update multiple columns in a single update statement: <syntaxhighlight lang="sql"> UPDATE T SET C1 = 1, C2 = 2</syntaxhighlight> Complex conditions and JOINs are also possible: <syntaxhighlight lang="sql"> UPDATE T SET A = 1 WHERE C1 = 1 AND C2 = 2</syntaxhighlight> Some databases allow the non-standard use of the FROM clause: <syntaxhighlight lang="sql"> UPDATE a SET a.[updated_column] = updatevalue FROM articles a JOIN classification c ON a.articleID = c.articleID WHERE c.classID = 1 </syntaxhighlight> Or on Oracle systems (assuming there is an index on classification.articleID): <syntaxhighlight lang="sql"> UPDATE ( SELECT * FROM articles JOIN classification ON articles.articleID = classification.articleID WHERE classification.classID = 1 ) SET [updated_column] = updatevalue </syntaxhighlight> With long name of table: <syntaxhighlight lang="sql"> UPDATE MyMainTable AS a SET a.LName = Smith WHERE a.PeopleID = 1235 </syntaxhighlight> ==Potential issues== * See [[Halloween Problem]]. It is possible for certain kinds of <code>UPDATE</code> statements to become an [[infinite loop]] when the <code>WHERE</code> clause and one or more <code>SET</code> clauses may utilize an intertwined [[Index (database)|index]]. ==References== {{Reflist}} {{SQL}} {{DEFAULTSORT:Update (Sql)}} [[Category:SQL keywords]] [[Category:Articles with example SQL code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:Reflist
(
edit
)
Template:SQL
(
edit
)
Template:Short description
(
edit
)