2 doc/src/sgml/ref/update.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-UPDATE">
8 <refentrytitle>UPDATE</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>UPDATE</refname>
15 <refpurpose>update rows of a table</refpurpose>
18 <indexterm zone="sql-update">
19 <primary>UPDATE</primary>
24 [ WITH [ RECURSIVE ] <replaceable class="parameter">with_query</replaceable> [, ...] ]
25 UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ [ AS ] <replaceable class="parameter">alias</replaceable> ]
26 SET { <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } |
27 ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) = ( { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...] ) } [, ...]
28 [ FROM <replaceable class="PARAMETER">from_list</replaceable> ]
29 [ WHERE <replaceable class="PARAMETER">condition</replaceable> | WHERE CURRENT OF <replaceable class="PARAMETER">cursor_name</replaceable> ]
30 [ RETURNING * | <replaceable class="parameter">output_expression</replaceable> [ [ AS ] <replaceable class="parameter">output_name</replaceable> ] [, ...] ]
35 <title>Description</title>
38 <command>UPDATE</command> changes the values of the specified
39 columns in all rows that satisfy the condition. Only the columns to
40 be modified need be mentioned in the <literal>SET</literal> clause;
41 columns not explicitly modified retain their previous values.
45 By default, <command>UPDATE</command> will update rows in the
46 specified table and all its subtables. If you wish to only update
47 the specific table mentioned, you must use the <literal>ONLY</>
52 There are two ways to modify a table using information contained in
53 other tables in the database: using sub-selects, or specifying
54 additional tables in the <literal>FROM</literal> clause. Which
55 technique is more appropriate depends on the specific
60 The optional <literal>RETURNING</> clause causes <command>UPDATE</>
61 to compute and return value(s) based on each row actually updated.
62 Any expression using the table's columns, and/or columns of other
63 tables mentioned in <literal>FROM</literal>, can be computed.
64 The new (post-update) values of the table's columns are used.
65 The syntax of the <literal>RETURNING</> list is identical to that of the
66 output list of <command>SELECT</>.
70 You must have the <literal>UPDATE</literal> privilege on the table,
71 or at least on the column(s) that are listed to be updated.
72 You must also have the <literal>SELECT</literal>
73 privilege on any column whose values are read in the
74 <replaceable class="parameter">expressions</replaceable> or
75 <replaceable class="parameter">condition</replaceable>.
80 <title>Parameters</title>
84 <term><replaceable class="parameter">with_query</replaceable></term>
87 The <literal>WITH</literal> clause allows you to specify one or more
88 subqueries that can be referenced by name in the <command>UPDATE</>
89 query. See <xref linkend="queries-with"> and <xref linkend="sql-select">
96 <term><replaceable class="PARAMETER">table</replaceable></term>
99 The name (optionally schema-qualified) of the table to update.
105 <term><replaceable class="parameter">alias</replaceable></term>
108 A substitute name for the target table. When an alias is
109 provided, it completely hides the actual name of the table. For
110 example, given <literal>UPDATE foo AS f</>, the remainder of the
111 <command>UPDATE</command> statement must refer to this table as
112 <literal>f</> not <literal>foo</>.
118 <term><replaceable class="PARAMETER">column</replaceable></term>
121 The name of a column in <replaceable
122 class="PARAMETER">table</replaceable>.
123 The column name can be qualified with a subfield name or array
124 subscript, if needed. Do not include the table's name in the
125 specification of a target column — for example,
126 <literal>UPDATE tab SET tab.col = 1</> is invalid.
132 <term><replaceable class="PARAMETER">expression</replaceable></term>
135 An expression to assign to the column. The expression can use the
136 old values of this and other columns in the table.
142 <term><literal>DEFAULT</literal></term>
145 Set the column to its default value (which will be NULL if no
146 specific default expression has been assigned to it).
152 <term><replaceable class="PARAMETER">from_list</replaceable></term>
155 A list of table expressions, allowing columns from other tables
156 to appear in the <literal>WHERE</> condition and the update
157 expressions. This is similar to the list of tables that can be
158 specified in the <xref linkend="sql-from"
159 endterm="sql-from-title"> of a <command>SELECT</command>
160 statement. Note that the target table must not appear in the
161 <replaceable>from_list</>, unless you intend a self-join (in which
162 case it must appear with an alias in the <replaceable>from_list</>).
168 <term><replaceable class="PARAMETER">condition</replaceable></term>
171 An expression that returns a value of type <type>boolean</type>.
172 Only rows for which this expression returns <literal>true</>
179 <term><replaceable class="PARAMETER">cursor_name</replaceable></term>
182 The name of the cursor to use in a <literal>WHERE CURRENT OF</>
183 condition. The row to be updated is the one most recently fetched
184 from this cursor. The cursor must be a non-grouping
185 query on the <command>UPDATE</>'s target table.
186 Note that <literal>WHERE CURRENT OF</> cannot be
187 specified together with a Boolean condition. See
188 <xref linkend="sql-declare">
189 for more information about using cursors with
190 <literal>WHERE CURRENT OF</>.
196 <term><replaceable class="PARAMETER">output_expression</replaceable></term>
199 An expression to be computed and returned by the <command>UPDATE</>
200 command after each row is updated. The expression can use any
201 column names of the <replaceable class="PARAMETER">table</replaceable>
202 or table(s) listed in <literal>FROM</>.
203 Write <literal>*</> to return all columns.
209 <term><replaceable class="PARAMETER">output_name</replaceable></term>
212 A name to use for a returned column.
220 <title>Outputs</title>
223 On successful completion, an <command>UPDATE</> command returns a command
226 UPDATE <replaceable class="parameter">count</replaceable>
228 The <replaceable class="parameter">count</replaceable> is the number
229 of rows updated, including matched rows whose values did not change.
230 Note that the number may be less than the number of rows that matched
231 the <replaceable class="parameter">condition</replaceable> when
232 updates were suppressed by a <literal>BEFORE UPDATE</> trigger. If
233 <replaceable class="parameter">count</replaceable> is 0, no rows were
234 updated by the query (this is not considered an error).
238 If the <command>UPDATE</> command contains a <literal>RETURNING</>
239 clause, the result will be similar to that of a <command>SELECT</>
240 statement containing the columns and values defined in the
241 <literal>RETURNING</> list, computed over the row(s) updated by the
250 When a <literal>FROM</> clause is present, what essentially happens
251 is that the target table is joined to the tables mentioned in the
252 <replaceable>from_list</replaceable>, and each output row of the join
253 represents an update operation for the target table. When using
254 <literal>FROM</> you should ensure that the join
255 produces at most one output row for each row to be modified. In
256 other words, a target row shouldn't join to more than one row from
257 the other table(s). If it does, then only one of the join rows
258 will be used to update the target row, but which one will be used
259 is not readily predictable.
263 Because of this indeterminacy, referencing other tables only within
264 sub-selects is safer, though often harder to read and slower than
270 <title>Examples</title>
273 Change the word <literal>Drama</> to <literal>Dramatic</> in the
274 column <structfield>kind</> of the table <structname>films</structname>:
277 UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
282 Adjust temperature entries and reset precipitation to its default
283 value in one row of the table <structname>weather</structname>:
286 UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
287 WHERE city = 'San Francisco' AND date = '2003-07-03';
292 Perform the same operation and return the updated entries:
295 UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
296 WHERE city = 'San Francisco' AND date = '2003-07-03'
297 RETURNING temp_lo, temp_hi, prcp;
302 Use the alternative column-list syntax to do the same update:
304 UPDATE weather SET (temp_lo, temp_hi, prcp) = (temp_lo+1, temp_lo+15, DEFAULT)
305 WHERE city = 'San Francisco' AND date = '2003-07-03';
310 Increment the sales count of the salesperson who manages the
311 account for Acme Corporation, using the <literal>FROM</literal>
314 UPDATE employees SET sales_count = sales_count + 1 FROM accounts
315 WHERE accounts.name = 'Acme Corporation'
316 AND employees.id = accounts.sales_person;
321 Perform the same operation, using a sub-select in the
322 <literal>WHERE</literal> clause:
324 UPDATE employees SET sales_count = sales_count + 1 WHERE id =
325 (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
330 Attempt to insert a new stock item along with the quantity of stock. If
331 the item already exists, instead update the stock count of the existing
332 item. To do this without failing the entire transaction, use savepoints:
337 INSERT INTO wines VALUES('Chateau Lafite 2003', '24');
338 -- Assume the above fails because of a unique key violation,
339 -- so now we issue these commands:
341 UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';
342 -- continue with other operations, and eventually
348 Change the <structfield>kind</> column of the table
349 <structname>films</structname> in the row on which the cursor
350 <literal>c_films</> is currently positioned:
352 UPDATE films SET kind = 'Dramatic' WHERE CURRENT OF c_films;
353 </programlisting></para>
357 <title>Compatibility</title>
360 This command conforms to the <acronym>SQL</acronym> standard, except
361 that the <literal>FROM</literal> and <literal>RETURNING</> clauses
362 are <productname>PostgreSQL</productname> extensions, as is the ability
363 to use <literal>WITH</> with <command>UPDATE</>.
367 According to the standard, the column-list syntax should allow a list
368 of columns to be assigned from a single row-valued expression, such
371 UPDATE accounts SET (contact_last_name, contact_first_name) =
372 (SELECT last_name, first_name FROM salesmen
373 WHERE salesmen.id = accounts.sales_id);
375 This is not currently implemented — the source must be a list
376 of independent expressions.
380 Some other database systems offer a <literal>FROM</> option in which
381 the target table is supposed to be listed again within <literal>FROM</>.
382 That is not how <productname>PostgreSQL</productname> interprets
383 <literal>FROM</>. Be careful when porting applications that use this