<!--
$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.23 2003/07/03 16:32:12 tgl Exp $
PostgreSQL documentation
-->

<refentry id="SQL-UPDATE">
 <refmeta>
  <refentrytitle id="SQL-UPDATE-TITLE">UPDATE</refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>UPDATE</refname>
  <refpurpose>update rows of a table</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...]
    [ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
    [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>UPDATE</command> changes the values of the specified
   columns in all rows that satisfy the condition. Only the columns to
   be modified need be mentioned in the statement; columns not explicitly
   <literal>SET</> retain their previous values.
  </para>

  <para>
   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.
  </para>

  <para>
   You must have the <literal>UPDATE</literal> privilege on the table
   to update it, as well as the <literal>SELECT</literal>
   privilege to any table whose values are read in the
   <replaceable class="parameter">expression</replaceable>s or
   <replaceable class="parameter">condition</replaceable>.
  </para>
 </refsect1>

 <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>
      An expression to assign to the column.  The expression may use the
      old values of this and other columns in the table.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>DEFAULT</literal></term>
    <listitem>
     <para>
      Set the column to its default value (which will be NULL if no
      specific default expression has been assigned to it).
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="PARAMETER">fromlist</replaceable></term>
    <listitem>
     <para>
      A list of table expressions, allowing columns from other tables
      to appear in the <literal>WHERE</> condition and the update expressions.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="PARAMETER">condition</replaceable></term>
    <listitem>
     <para>
      An expression that returns a value of type <type>boolean</type>.
      Only rows for which this expression returns <literal>true</>
      will be updated.
     </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>

  <para>
   Change the word <literal>Drama</> to <literal>Dramatic</> in the
   column <structfield>kind</> of the table <literal>films</literal>:

<programlisting>
UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
</programlisting>
  </para>

  <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>

 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   This command conforms to the SQL standard.  The
   <literal>FROM</literal> clause is a PostgreSQL extension.
  </para>
 </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:
-->
