1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2005-10-13 01:19:22 +02:00
|
|
|
$PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.33 2005/10/12 23:19:22 tgl Exp $
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
1999-07-14 22:32:59 +02:00
|
|
|
<refentry id="SQL-UPDATE">
|
|
|
|
<refmeta>
|
2001-05-27 11:59:30 +02:00
|
|
|
<refentrytitle id="SQL-UPDATE-TITLE">UPDATE</refentrytitle>
|
1999-07-14 22:32:59 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-04-27 01:56:51 +02:00
|
|
|
|
1999-07-14 22:32:59 +02:00
|
|
|
<refnamediv>
|
2003-04-27 01:56:51 +02:00
|
|
|
<refname>UPDATE</refname>
|
|
|
|
<refpurpose>update rows of a table</refpurpose>
|
1999-07-14 22:32:59 +02:00
|
|
|
</refnamediv>
|
2003-04-27 01:56:51 +02:00
|
|
|
|
2003-08-31 19:32:24 +02:00
|
|
|
<indexterm zone="sql-update">
|
|
|
|
<primary>UPDATE</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
1999-07-14 22:32:59 +02:00
|
|
|
<refsynopsisdiv>
|
2003-04-27 01:56:51 +02:00
|
|
|
<synopsis>
|
2003-06-25 06:19:24 +02:00
|
|
|
UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...]
|
1999-07-14 22:32:59 +02:00
|
|
|
[ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
|
|
|
|
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
|
2003-04-27 01:56:51 +02:00
|
|
|
</synopsis>
|
1999-07-14 22:32:59 +02:00
|
|
|
</refsynopsisdiv>
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
1999-07-14 22:32:59 +02:00
|
|
|
|
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
<command>UPDATE</command> changes the values of the specified
|
|
|
|
columns in all rows that satisfy the condition. Only the columns to
|
2004-03-03 23:22:24 +01:00
|
|
|
be modified need be mentioned in the <literal>SET</literal> clause;
|
|
|
|
columns not explicitly modified retain their previous values.
|
1999-07-14 22:32:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
By default, <command>UPDATE</command> will update rows in the
|
|
|
|
specified table and all its subtables. If you wish to only update
|
|
|
|
the specific table mentioned, you must use the <literal>ONLY</>
|
|
|
|
clause.
|
1999-07-14 22:32:59 +02:00
|
|
|
</para>
|
2000-06-09 03:44:34 +02:00
|
|
|
|
2004-03-03 23:22:24 +01:00
|
|
|
<para>
|
|
|
|
There are two ways to modify a table using information contained in
|
|
|
|
other tables in the database: using sub-selects, or specifying
|
|
|
|
additional tables in the <literal>FROM</literal> clause. Which
|
|
|
|
technique is more appropriate depends on the specific
|
|
|
|
circumstances.
|
|
|
|
</para>
|
|
|
|
|
2000-06-09 03:44:34 +02:00
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
You must have the <literal>UPDATE</literal> privilege on the table
|
|
|
|
to update it, as well as the <literal>SELECT</literal>
|
2003-07-03 18:34:26 +02:00
|
|
|
privilege to any table whose values are read in the
|
|
|
|
<replaceable class="parameter">expression</replaceable>s or
|
|
|
|
<replaceable class="parameter">condition</replaceable>.
|
2000-06-09 03:44:34 +02:00
|
|
|
</para>
|
1999-07-14 22:32:59 +02:00
|
|
|
</refsect1>
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">table</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name (optionally schema-qualified) of the table to update.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">column</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-09 21:08:20 +02:00
|
|
|
The name of a column in <replaceable
|
|
|
|
class="PARAMETER">table</replaceable>.
|
|
|
|
The column name can be qualified with a subfield name or array
|
|
|
|
subscript, if needed.
|
2003-04-27 01:56:51 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">expression</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-07-03 18:34:26 +02:00
|
|
|
An expression to assign to the column. The expression may use the
|
|
|
|
old values of this and other columns in the table.
|
2003-04-27 01:56:51 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
2003-06-25 06:19:24 +02:00
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>DEFAULT</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-07-03 18:34:26 +02:00
|
|
|
Set the column to its default value (which will be NULL if no
|
|
|
|
specific default expression has been assigned to it).
|
2003-06-25 06:19:24 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
2003-04-27 01:56:51 +02:00
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">fromlist</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A list of table expressions, allowing columns from other tables
|
2004-03-03 23:22:24 +01:00
|
|
|
to appear in the <literal>WHERE</> condition and the update
|
|
|
|
expressions. This is similar to the list of tables that can be
|
|
|
|
specified in the <xref linkend="sql-from"
|
|
|
|
endterm="sql-from-title"> of a <command>SELECT</command>
|
2005-01-09 06:57:45 +01:00
|
|
|
statement. Note that the target table must not appear in the
|
|
|
|
<replaceable>fromlist</>, unless you intend a self-join (in which
|
|
|
|
case it must appear with an alias in the <replaceable>fromlist</>).
|
2003-04-27 01:56:51 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="PARAMETER">condition</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2003-07-03 18:34:26 +02:00
|
|
|
An expression that returns a value of type <type>boolean</type>.
|
|
|
|
Only rows for which this expression returns <literal>true</>
|
|
|
|
will be updated.
|
2003-04-27 01:56:51 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
2003-09-12 02:12:47 +02:00
|
|
|
<title>Outputs</title>
|
2003-04-27 01:56:51 +02:00
|
|
|
|
2003-09-12 02:12:47 +02:00
|
|
|
<para>
|
|
|
|
On successful completion, an <command>UPDATE</> command returns a command
|
|
|
|
tag of the form
|
|
|
|
<screen>
|
|
|
|
UPDATE <replaceable class="parameter">count</replaceable>
|
|
|
|
</screen>
|
|
|
|
The <replaceable class="parameter">count</replaceable> is the number
|
|
|
|
of rows updated. If <replaceable class="parameter">count</replaceable> is
|
|
|
|
0, no rows matched the <replaceable
|
|
|
|
class="parameter">condition</replaceable> (this is not considered
|
|
|
|
an error).
|
|
|
|
</para>
|
2003-04-27 01:56:51 +02:00
|
|
|
</refsect1>
|
|
|
|
|
2005-01-04 04:58:16 +01:00
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
|
|
|
|
|
|
|
<para>
|
2005-01-09 06:57:45 +01:00
|
|
|
When a <literal>FROM</> clause is present, what essentially happens
|
|
|
|
is that the target table is joined to the tables mentioned in the
|
|
|
|
<replaceable>fromlist</replaceable>, and each output row of the join
|
|
|
|
represents an update operation for the target table. When using
|
|
|
|
<literal>FROM</> you should ensure that the join
|
2005-01-04 04:58:16 +01:00
|
|
|
produces at most one output row for each row to be modified. In
|
2005-01-09 06:57:45 +01:00
|
|
|
other words, a target row shouldn't join to more than one row from
|
2005-01-04 04:58:16 +01:00
|
|
|
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.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2005-10-13 01:19:22 +02:00
|
|
|
Because of this indeterminacy, referencing other tables only within
|
2005-01-04 04:58:16 +01:00
|
|
|
sub-selects is safer, though often harder to read and slower than
|
|
|
|
using a join.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
1999-07-14 22:32:59 +02:00
|
|
|
|
|
|
|
<para>
|
2003-04-27 01:56:51 +02:00
|
|
|
Change the word <literal>Drama</> to <literal>Dramatic</> in the
|
2004-03-03 23:22:24 +01:00
|
|
|
column <structfield>kind</> of the table <structname>films</structname>:
|
1999-07-14 22:32:59 +02:00
|
|
|
|
2000-03-26 20:32:30 +02:00
|
|
|
<programlisting>
|
2003-07-03 18:34:26 +02:00
|
|
|
UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
|
2000-03-26 20:32:30 +02:00
|
|
|
</programlisting>
|
1999-07-14 22:32:59 +02:00
|
|
|
</para>
|
2003-07-03 18:34:26 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Adjust temperature entries and reset precipitation to its default
|
2004-03-03 23:22:24 +01:00
|
|
|
value in one row of the table <structname>weather</structname>:
|
2003-07-03 18:34:26 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
UPDATE weather SET temp_lo = temp_lo+1, temp_hi = temp_lo+15, prcp = DEFAULT
|
|
|
|
WHERE city = 'San Francisco' AND date = '2003-07-03';
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
2004-03-03 23:22:24 +01:00
|
|
|
<para>
|
|
|
|
Increment the sales count of the salesperson who manages the
|
|
|
|
account for Acme Corporation, using the <literal>FROM</literal>
|
|
|
|
clause syntax:
|
|
|
|
<programlisting>
|
|
|
|
UPDATE employees SET sales_count = sales_count + 1 FROM accounts
|
|
|
|
WHERE accounts.name = 'Acme Corporation'
|
|
|
|
AND employees.id = accounts.sales_person;
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
Perform the same operation, using a sub-select in the
|
|
|
|
<literal>WHERE</literal> clause:
|
|
|
|
<programlisting>
|
|
|
|
UPDATE employees SET sales_count = sales_count + 1 WHERE id =
|
|
|
|
(SELECT sales_person FROM accounts WHERE name = 'Acme Corporation');
|
2004-08-08 03:48:31 +02:00
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
Attempt to insert a new stock item along with the quantity of stock. If
|
2005-01-09 06:57:45 +01:00
|
|
|
the item already exists, instead update the stock count of the existing
|
|
|
|
item. To do this without failing the entire transaction, use savepoints.
|
2004-08-08 03:48:31 +02:00
|
|
|
<programlisting>
|
|
|
|
BEGIN;
|
2005-01-09 06:57:45 +01:00
|
|
|
-- other operations
|
2004-08-08 03:48:31 +02:00
|
|
|
SAVEPOINT sp1;
|
|
|
|
INSERT INTO wines VALUES('Chateau Lafite 2003', '24');
|
2005-01-09 06:57:45 +01:00
|
|
|
-- Assume the above fails because of a unique key violation,
|
|
|
|
-- so now we issue these commands:
|
2004-08-08 03:48:31 +02:00
|
|
|
ROLLBACK TO sp1;
|
2005-01-09 06:57:45 +01:00
|
|
|
UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';
|
|
|
|
-- continue with other operations, and eventually
|
2004-08-08 03:48:31 +02:00
|
|
|
COMMIT;
|
2004-03-03 23:22:24 +01:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
1999-07-14 22:32:59 +02:00
|
|
|
</refsect1>
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
2005-01-09 06:57:45 +01:00
|
|
|
This command conforms to the <acronym>SQL</acronym> standard, except
|
|
|
|
that the <literal>FROM</literal> clause is a
|
2003-09-11 23:42:20 +02:00
|
|
|
<productname>PostgreSQL</productname> extension.
|
2003-04-27 01:56:51 +02:00
|
|
|
</para>
|
2005-01-09 06:57:45 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Some other database systems offer a <literal>FROM</> option in which
|
|
|
|
the target table is supposed to be listed again within <literal>FROM</>.
|
|
|
|
That is not how <productname>PostgreSQL</productname> interprets
|
|
|
|
<literal>FROM</>. Be careful when porting applications that use this
|
|
|
|
extension.
|
|
|
|
</para>
|
1999-07-14 22:32:59 +02:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:nil
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:"/usr/lib/sgml/catalog"
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|