1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2003-08-31 19:32:24 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.24 2003/08/31 17:32:24 petere 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
|
2003-07-03 18:34:26 +02:00
|
|
|
be modified need be mentioned in the statement; columns not explicitly
|
|
|
|
<literal>SET</> 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
|
|
|
|
|
|
|
<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>
|
|
|
|
The name of a column in <replaceable class="PARAMETER">table</replaceable>.
|
|
|
|
</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
|
2003-07-03 18:34:26 +02:00
|
|
|
to appear in the <literal>WHERE</> condition and the update expressions.
|
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>
|
|
|
|
<title>Diagnostics</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>UPDATE <replaceable class="parameter">count</replaceable></computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Message returned if successful. The value <replaceable
|
|
|
|
class="parameter">count</replaceable> is the number of rows
|
|
|
|
updated. If <replaceable class="parameter">count</replaceable>
|
|
|
|
is 0, no rows were updated.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<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
|
|
|
|
column <structfield>kind</> of the table <literal>films</literal>:
|
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
|
|
|
|
value in one row of the table <literal>weather</literal>:
|
|
|
|
|
|
|
|
<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>
|
|
|
|
|
1999-07-14 22:32:59 +02:00
|
|
|
</refsect1>
|
|
|
|
|
2003-04-27 01:56:51 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
This command conforms to the SQL standard. The
|
|
|
|
<literal>FROM</literal> clause is a PostgreSQL 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:
|
|
|
|
-->
|