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. If <replaceable class="parameter">count</replaceable> is
230 0, no rows matched the <replaceable
231 class="parameter">condition</replaceable> (this is not considered
236 If the <command>UPDATE</> command contains a <literal>RETURNING</>
237 clause, the result will be similar to that of a <command>SELECT</>
238 statement containing the columns and values defined in the
239 <literal>RETURNING</> list, computed over the row(s) updated by the
248 When a <literal>FROM</> clause is present, what essentially happens
249 is that the target table is joined to the tables mentioned in the
250 <replaceable>from_list</replaceable>, and each output row of the join
251 represents an update operation for the target table. When using
252 <literal>FROM</> you should ensure that the join
253 produces at most one output row for each row to be modified. In
254 other words, a target row shouldn't join to more than one row from
255 the other table(s). If it does, then only one of the join rows
256 will be used to update the target row, but which one will be used
257 is not readily predictable.
261 Because of this indeterminacy, referencing other tables only within
262 sub-selects is safer, though often harder to read and slower than
268 <title>Examples</title>
271 Change the word <literal>Drama</> to <literal>Dramatic</> in the
272 column <structfield>kind</> of the table <structname>films</structname>:
275 UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
280 Adjust temperature entries and reset precipitation to its default
281 value in one row of the table <structname>weather</structname>:
284 UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
285 WHERE city = 'San Francisco' AND date = '2003-07-03';
290 Perform the same operation and return the updated entries:
293 UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
294 WHERE city = 'San Francisco' AND date = '2003-07-03'
295 RETURNING temp_lo, temp_hi, prcp;
300 Use the alternative column-list syntax to do the same update:
302 UPDATE weather SET (temp_lo, temp_hi, prcp) = (temp_lo+1, temp_lo+15, DEFAULT)
303 WHERE city = 'San Francisco' AND date = '2003-07-03';
308 Increment the sales count of the salesperson who manages the
309 account for Acme Corporation, using the <literal>FROM</literal>
312 UPDATE employees SET sales_count = sales_count + 1 FROM accounts
313 WHERE accounts.name = 'Acme Corporation'
314 AND employees.id = accounts.sales_person;
319 Perform the same operation, using a sub-select in the
320 <literal>WHERE</literal> clause:
322 UPDATE employees SET sales_count = sales_count + 1 WHERE id =
323 (SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
328 Attempt to insert a new stock item along with the quantity of stock. If
329 the item already exists, instead update the stock count of the existing
330 item. To do this without failing the entire transaction, use savepoints:
335 INSERT INTO wines VALUES('Chateau Lafite 2003', '24');
336 -- Assume the above fails because of a unique key violation,
337 -- so now we issue these commands:
339 UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';
340 -- continue with other operations, and eventually
346 Change the <structfield>kind</> column of the table
347 <structname>films</structname> in the row on which the cursor
348 <literal>c_films</> is currently positioned:
350 UPDATE films SET kind = 'Dramatic' WHERE CURRENT OF c_films;
356 <title>Compatibility</title>
359 This command conforms to the <acronym>SQL</acronym> standard, except
360 that the <literal>FROM</literal> and <literal>RETURNING</> clauses
361 are <productname>PostgreSQL</productname> extensions, as is the ability
362 to use <literal>WITH</> with <command>UPDATE</>.
366 According to the standard, the column-list syntax should allow a list
367 of columns to be assigned from a single row-valued expression, such
370 UPDATE accounts SET (contact_last_name, contact_first_name) =
371 (SELECT last_name, first_name FROM salesmen
372 WHERE salesmen.id = accounts.sales_id);
374 This is not currently implemented — the source must be a list
375 of independent expressions.
379 Some other database systems offer a <literal>FROM</> option in which
380 the target table is supposed to be listed again within <literal>FROM</>.
381 That is not how <productname>PostgreSQL</productname> interprets
382 <literal>FROM</>. Be careful when porting applications that use this