]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/update.sgml
ff668a6254f3d930aec2d650dce5ca76dec301fd
[postgresql] / doc / src / sgml / ref / update.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.9 2000/04/11 05:39:15 thomas Exp $
3 Postgres documentation
4 -->
5
6 <refentry id="SQL-UPDATE">
7  <refmeta>
8   <refentrytitle id="SQL-UPDATE-TITLE">
9    UPDATE
10   </refentrytitle>
11   <refmiscinfo>SQL - Language Statements</refmiscinfo>
12  </refmeta>
13  <refnamediv>
14   <refname>
15    UPDATE
16   </refname>
17   <refpurpose>
18    Replaces values of columns in a table
19   </refpurpose>
20  </refnamediv>
21  <refsynopsisdiv>
22   <refsynopsisdivinfo>
23    <date>1999-07-20</date>
24   </refsynopsisdivinfo>
25   <synopsis>
26 UPDATE <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">col</replaceable> = <replaceable class="PARAMETER">expression</replaceable> [, ...]
27     [ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
28     [ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
29   </synopsis>
30
31   <refsect2 id="R2-SQL-UPDATE-1">
32    <refsect2info>
33     <date>1998-09-24</date>
34    </refsect2info>
35    <title>
36     Inputs
37    </title>
38    <para>
39
40     <variablelist>
41      <varlistentry>
42       <term><replaceable class="PARAMETER">table</replaceable></term>
43       <listitem>
44        <para>
45         The name of an existing table.
46        </para>
47       </listitem>
48      </varlistentry>
49      <varlistentry>
50       <term><replaceable class="PARAMETER">column</replaceable></term>
51       <listitem>
52        <para>
53         The name of a column in <replaceable class="PARAMETER">table</replaceable>.
54        </para>
55       </listitem>
56      </varlistentry>
57      <varlistentry>
58       <term><replaceable class="PARAMETER">expression</replaceable></term>
59       <listitem>
60        <para>
61         A valid expression or value to assign to column.
62        </para>
63       </listitem>
64      </varlistentry>
65      <varlistentry>
66       <term><replaceable class="PARAMETER">fromlist</replaceable></term>
67       <listitem>
68        <para>
69         A <productname>Postgres</productname>
70         non-standard extension to allow columns
71         from other tables to appear in the WHERE condition.
72        </para>
73       </listitem>
74      </varlistentry>
75      <varlistentry>
76       <term><replaceable class="PARAMETER">condition</replaceable></term>
77       <listitem>
78        <para>
79         Refer to the SELECT statement for a further description
80         of the WHERE clause.
81        </para>
82       </listitem>
83      </varlistentry>
84     </variablelist>
85    </para>
86   </refsect2>
87
88   <refsect2 id="R2-SQL-UPDATE-2">
89    <refsect2info>
90     <date>1998-09-24</date>
91    </refsect2info>
92    <title>
93     Outputs
94    </title>
95
96    <para>
97     <variablelist>
98      <varlistentry>
99       <term><computeroutput>
100 UPDATE <replaceable class="parameter">#</replaceable>
101       </computeroutput></term>
102       <listitem>
103        <para>
104         Message returned if successful.
105         The <replaceable class="parameter">#</replaceable>
106         means the number of rows updated.
107         If <replaceable class="parameter">#</replaceable>
108         is equal 0 no rows are updated.
109        </para>
110       </listitem>
111      </varlistentry>
112     </variablelist>
113    </para>
114   </refsect2>
115  </refsynopsisdiv>
116
117  <refsect1 id="R1-SQL-UPDATE-1">
118   <refsect1info>
119    <date>1998-09-24</date>
120   </refsect1info>
121   <title>
122    Description
123   </title>
124   <para>
125    <command>UPDATE</command> changes the values of the columns specified for
126    all rows which satisfy condition. Only the columns 
127    to be modified need appear as columns in the statement.
128   </para>
129
130   <para>
131    Array references use the same syntax found in
132    <xref linkend="sql-select-title" endterm="sql-select-title">.
133    That is, either single array elements, a range of array
134    elements or the entire array may be replaced with a single
135    query.
136   </para>
137
138   <para>
139    You must have write access to the table in order to modify
140    it, as well as read access to any table whose values are
141    mentioned in the WHERE condition.
142   </para>
143  </refsect1>
144
145  <refsect1 id="R1-SQL-UPDATE-2">
146   <title>
147    Usage
148   </title>
149
150   <para>
151    Change word "Drama" with "Dramatic" on column kind:
152
153 <programlisting>
154 UPDATE films SET kind = 'Dramatic' WHERE kind = 'Drama';
155 SELECT * FROM films WHERE kind = 'Dramatic' OR kind = 'Drama';
156
157  code  |     title     | did | date_prod  |   kind   | len
158 -------+---------------+-----+------------+----------+-------
159  BL101 | The Third Man | 101 | 1949-12-23 | Dramatic | 01:44
160  P_302 | Becket        | 103 | 1964-02-03 | Dramatic | 02:28
161  M_401 | War and Peace | 104 | 1967-02-12 | Dramatic | 05:57
162  T_601 | Yojimbo       | 106 | 1961-06-16 | Dramatic | 01:50
163  DA101 | Das Boot      | 110 | 1981-11-11 | Dramatic | 02:29
164 </programlisting>
165   </para>
166  </refsect1>
167
168  <refsect1 id="R1-SQL-UPDATE-3">
169   <title>
170    Compatibility
171   </title>
172
173   <refsect2 id="R2-SQL-UPDATE-4">
174    <refsect2info>
175     <date>1998-09-24</date>
176    </refsect2info>
177    <title>
178     SQL92
179    </title>
180    <para>
181     <acronym>SQL92</acronym> defines a different syntax for
182     the positioned UPDATE statement:
183
184     <synopsis>
185 UPDATE <replaceable>table</replaceable> SET <replaceable>column</replaceable> = <replaceable>expression</replaceable> [, ...]
186     WHERE CURRENT OF <replaceable class="parameter">cursor</replaceable>
187     </synopsis>
188
189     where <replaceable class="parameter">cursor</replaceable>
190     identifies an open cursor.
191    </para>
192   </refsect2>
193  </refsect1>
194 </refentry>
195
196 <!-- Keep this comment at the end of the file
197 Local variables:
198 mode: sgml
199 sgml-omittag:nil
200 sgml-shorttag:t
201 sgml-minimize-attributes:nil
202 sgml-always-quote-attributes:t
203 sgml-indent-step:1
204 sgml-indent-data:t
205 sgml-parent-document:nil
206 sgml-default-dtd-file:"../reference.ced"
207 sgml-exposed-tags:nil
208 sgml-local-catalogs:"/usr/lib/sgml/catalog"
209 sgml-local-ecat-files:nil
210 End:
211 -->