An
SQL UPDATE statement changes the data of one or more records in a
table
Table may refer to:
* Table (furniture), a piece of furniture with a flat surface and one or more legs
* Table (landform), a flat area of land
* Table (information), a data arrangement with rows and columns
* Table (database), how the table data ...
. Either all the rows can be updated, or a subset may be chosen using a
condition.
The
UPDATE
statement has the following form:
:
UPDATE
''table_name''
SET
''column_name'' = ''value''
''column_name'' = ''value ...'' WHERE ''condition''">''WHERE
''condition''
For the
UPDATE
to be successful, the user must have data manipulation privileges (
UPDATE
privilege) on the table or
column
A column or pillar in architecture and structural engineering is a structural element that transmits, through compression, the weight of the structure above to other structural elements below. In other words, a column is a compression member. ...
and the updated value must not conflict with all the applicable constraints (such as
primary key
In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes (Column (database), columns) that uniquely specify a tuple (Row (database), row) in a Relation (database), relation (Table (database), t ...
s, unique indexes,
CHECK
constraints, and
NOT NULL
constraints).
In some databases, such as
PostgreSQL
PostgreSQL (, ), also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. It was originally named POSTGRES, referring to its origins as a successor to the In ...
, 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.
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.
Examples
Set the value of column ''C1'' in table ''T'' to 1, only in those rows where the value of column ''C2'' is "a".
UPDATE T
SET C1 = 1
WHERE C2 = 'a'
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".
UPDATE T
SET C1 = 9,
C3 = 4
WHERE C2 = 'a'
Increase value of column ''C1'' by 1 if the value in column ''C2'' is "a".
UPDATE T
SET C1 = C1 + 1
WHERE C2 = 'a'
Prepend the value in column ''C1'' with the string "text" if the value in column ''C2'' is "a".
UPDATE T
SET C1 = 'text' , , C1
WHERE C2 = 'a'
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.
UPDATE T1
SET C1 = 2
WHERE C2 IN ( SELECT C3
FROM T2
WHERE C4 = 0)
One may also update multiple columns in a single update statement:
UPDATE T
SET C1 = 1,
C2 = 2
Complex conditions and JOINs are also possible:
UPDATE T
SET A = 1
WHERE C1 = 1
AND C2 = 2
Some databases allow the non-standard use of the FROM clause:
UPDATE a
SET a. pdated_column= updatevalue
FROM articles a
JOIN classification c
ON a.articleID = c.articleID
WHERE c.classID = 1
Or on Oracle systems (assuming there is an index on classification.articleID):
UPDATE
(
SELECT *
FROM articles
JOIN classification
ON articles.articleID = classification.articleID
WHERE classification.classID = 1
)
SET pdated_column= updatevalue
With long name of table:
UPDATE MyMainTable AS a
SET a.LName = Smith
WHERE a.PeopleID = 1235
Potential issues
* See
Halloween Problem
In computing, the Halloween Problem refers to a phenomenon in database
In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are h ...
. It is possible for certain kinds of
UPDATE
statements to become an
infinite loop
In computer programming, an infinite loop (or endless loop) is a sequence of instructions that, as written, will continue endlessly, unless an external intervention occurs ("pull the plug"). It may be intentional.
Overview
This differs from:
* ...
when the
WHERE
Where may refer to:
* Where?, one of the Five Ws in journalism
* where (command), a shell command
* Where (SQL), a database language clause
* Where.com
Where, Inc. was a location-based media company in North America. Their main products were ...
clause and one or more
SET
clauses may utilize an intertwined
index
Index (or its plural form indices) may refer to:
Arts, entertainment, and media Fictional entities
* Index (''A Certain Magical Index''), a character in the light novel series ''A Certain Magical Index''
* The Index, an item on a Halo megastru ...
.
References
{{DEFAULTSORT:Update (Sql)
SQL keywords
Articles with example SQL code