1UPDATE(7) SQL Commands UPDATE(7)
2
3
4
6 UPDATE - update rows of a table
7
8
10 UPDATE [ ONLY ] table [ * ] [ [ AS ] alias ]
11 SET { column = { expression | DEFAULT } |
12 ( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
13 [ FROM fromlist ]
14 [ WHERE condition | WHERE CURRENT OF cursor_name ]
15 [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
16
17
19 UPDATE changes the values of the specified columns in all rows that
20 satisfy the condition. Only the columns to be modified need be men‐
21 tioned in the SET clause; columns not explicitly modified retain their
22 previous values.
23
24 There are two ways to modify a table using information contained in
25 other tables in the database: using sub-selects, or specifying addi‐
26 tional tables in the FROM clause. Which technique is more appropriate
27 depends on the specific circumstances.
28
29 The optional RETURNING clause causes UPDATE to compute and return
30 value(s) based on each row actually updated. Any expression using the
31 table's columns, and/or columns of other tables mentioned in FROM, can
32 be computed. The new (post-update) values of the table's columns are
33 used. The syntax of the RETURNING list is identical to that of the
34 output list of SELECT.
35
36 You must have the UPDATE privilege on the table, or at least on the
37 column(s) that are listed to be updated. You must also have the SELECT
38 privilege on any column whose values are read in the expressions or
39 condition.
40
42 table The name (optionally schema-qualified) of the table to update.
43 If ONLY is specified before the table name, matching rows are
44 updated in the named table only. If ONLY is not specified,
45 matching rows are also updated in any tables inheriting from the
46 named table. Optionally, * can be specified after the table name
47 to explicitly indicate that descendant tables are included.
48
49 alias A substitute name for the target table. When an alias is pro‐
50 vided, it completely hides the actual name of the table. For
51 example, given UPDATE foo AS f, the remainder of the UPDATE
52 statement must refer to this table as f not foo.
53
54 column The name of a column in table. The column name can be qualified
55 with a subfield name or array subscript, if needed. Do not
56 include the table's name in the specification of a target column
57 — for example, UPDATE tab SET tab.col = 1 is invalid.
58
59 expression
60 An expression to assign to the column. The expression can use
61 the old values of this and other columns in the table.
62
63 DEFAULT
64 Set the column to its default value (which will be NULL if no
65 specific default expression has been assigned to it).
66
67 fromlist
68 A list of table expressions, allowing columns from other tables
69 to appear in the WHERE condition and the update expressions.
70 This is similar to the list of tables that can be specified in
71 the FROM Clause [select(7)] of a SELECT statement. Note that the
72 target table must not appear in the fromlist, unless you intend
73 a self-join (in which case it must appear with an alias in the
74 fromlist).
75
76 condition
77 An expression that returns a value of type boolean. Only rows
78 for which this expression returns true will be updated.
79
80 cursor_name
81 The name of the cursor to use in a WHERE CURRENT OF condition.
82 The row to be updated is the one most recently fetched from this
83 cursor. The cursor must be a non-grouping query on the UPDATE's
84 target table. Note that WHERE CURRENT OF cannot be specified
85 together with a Boolean condition. See DECLARE [declare(7)] for
86 more information about using cursors with WHERE CURRENT OF.
87
88 output_expression
89 An expression to be computed and returned by the UPDATE command
90 after each row is updated. The expression can use any column
91 names of the table or table(s) listed in FROM. Write * to
92 return all columns.
93
94 output_name
95 A name to use for a returned column.
96
98 On successful completion, an UPDATE command returns a command tag of
99 the form
100
101 UPDATE count
102
103 The count is the number of rows updated. If count is 0, no rows matched
104 the condition (this is not considered an error).
105
106 If the UPDATE command contains a RETURNING clause, the result will be
107 similar to that of a SELECT statement containing the columns and values
108 defined in the RETURNING list, computed over the row(s) updated by the
109 command.
110
112 When a FROM clause is present, what essentially happens is that the
113 target table is joined to the tables mentioned in the fromlist, and
114 each output row of the join represents an update operation for the tar‐
115 get table. When using FROM you should ensure that the join produces at
116 most one output row for each row to be modified. In other words, a tar‐
117 get row shouldn't join to more than one row from the other table(s). If
118 it does, then only one of the join rows will be used to update the tar‐
119 get row, but which one will be used is not readily predictable.
120
121 Because of this indeterminacy, referencing other tables only within
122 sub-selects is safer, though often harder to read and slower than using
123 a join.
124
126 Change the word Drama to Dramatic in the column kind of the table
127 films:
128
129 UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
130
131
132 Adjust temperature entries and reset precipitation to its default value
133 in one row of the table weather:
134
135 UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
136 WHERE city = 'San Francisco' AND date = '2003-07-03';
137
138
139 Perform the same operation and return the updated entries:
140
141 UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
142 WHERE city = 'San Francisco' AND date = '2003-07-03'
143 RETURNING temp_lo, temp_hi, prcp;
144
145
146 Use the alternative column-list syntax to do the same update:
147
148 UPDATE weather SET (temp_lo, temp_hi, prcp) = (temp_lo+1, temp_lo+15, DEFAULT)
149 WHERE city = 'San Francisco' AND date = '2003-07-03';
150
151
152 Increment the sales count of the salesperson who manages the account
153 for Acme Corporation, using the FROM clause syntax:
154
155 UPDATE employees SET sales_count = sales_count + 1 FROM accounts
156 WHERE accounts.name = 'Acme Corporation'
157 AND employees.id = accounts.sales_person;
158
159
160 Perform the same operation, using a sub-select in the WHERE clause:
161
162 UPDATE employees SET sales_count = sales_count + 1 WHERE id =
163 (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
164
165
166 Attempt to insert a new stock item along with the quantity of stock. If
167 the item already exists, instead update the stock count of the existing
168 item. To do this without failing the entire transaction, use save‐
169 points:
170
171 BEGIN;
172 -- other operations
173 SAVEPOINT sp1;
174 INSERT INTO wines VALUES('Chateau Lafite 2003', '24');
175 -- Assume the above fails because of a unique key violation,
176 -- so now we issue these commands:
177 ROLLBACK TO sp1;
178 UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';
179 -- continue with other operations, and eventually
180 COMMIT;
181
182
183 Change the kind column of the table films in the row on which the cur‐
184 sor c_films is currently positioned:
185
186 UPDATE films SET kind = 'Dramatic' WHERE CURRENT OF c_films;
187
188
190 This command conforms to the SQL standard, except that the FROM and
191 RETURNING clauses are PostgreSQL extensions.
192
193 According to the standard, the column-list syntax should allow a list
194 of columns to be assigned from a single row-valued expression, such as
195 a sub-select:
196
197 UPDATE accounts SET (contact_last_name, contact_first_name) =
198 (SELECT last_name, first_name FROM salesmen
199 WHERE salesmen.id = accounts.sales_id);
200
201 This is not currently implemented — the source must be a list of inde‐
202 pendent expressions.
203
204 Some other database systems offer a FROM option in which the target ta‐
205 ble is supposed to be listed again within FROM. That is not how Post‐
206 greSQL interprets FROM. Be careful when porting applications that use
207 this extension.
208
209
210
211SQL - Language Statements 2014-02-17 UPDATE(7)