]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/update.sgml
Final proofreader changes to docs.
[postgresql] / doc / src / sgml / ref / update.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.12 2000/10/05 19:57:23 momjian 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 [ ONLY ] <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 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
144   <para>
145   By default UPDATE will update tuples in the table specified 
146   and all its sub-classes. If you wish to only update the
147   specific table mentioned, you should use the ONLY clause.
148   </para>
149  </refsect1>
150
151  <refsect1 id="R1-SQL-UPDATE-2">
152   <title>
153    Usage
154   </title>
155
156   <para>
157    Change word "Drama" with "Dramatic" on column kind:
158
159 <programlisting>
160 UPDATE films 
161 SET kind = 'Dramatic' 
162 WHERE kind = 'Drama';
163 SELECT * 
164 FROM films 
165 WHERE kind = 'Dramatic' OR kind = 'Drama';
166
167  code  |     title     | did | date_prod  |   kind   | len
168 -------+---------------+-----+------------+----------+-------
169  BL101 | The Third Man | 101 | 1949-12-23 | Dramatic | 01:44
170  P_302 | Becket        | 103 | 1964-02-03 | Dramatic | 02:28
171  M_401 | War and Peace | 104 | 1967-02-12 | Dramatic | 05:57
172  T_601 | Yojimbo       | 106 | 1961-06-16 | Dramatic | 01:50
173  DA101 | Das Boot      | 110 | 1981-11-11 | Dramatic | 02:29
174 </programlisting>
175   </para>
176  </refsect1>
177
178  <refsect1 id="R1-SQL-UPDATE-3">
179   <title>
180    Compatibility
181   </title>
182
183   <refsect2 id="R2-SQL-UPDATE-4">
184    <refsect2info>
185     <date>1998-09-24</date>
186    </refsect2info>
187    <title>
188     SQL92
189    </title>
190    <para>
191     <acronym>SQL92</acronym> defines a different syntax for
192     the positioned UPDATE statement:
193
194     <synopsis>
195 UPDATE <replaceable>table</replaceable> SET <replaceable>column</replaceable> = <replaceable>expression</replaceable> [, ...]
196     WHERE CURRENT OF <replaceable class="parameter">cursor</replaceable>
197     </synopsis>
198
199     where <replaceable class="parameter">cursor</replaceable>
200     identifies an open cursor.
201    </para>
202   </refsect2>
203  </refsect1>
204 </refentry>
205
206 <!-- Keep this comment at the end of the file
207 Local variables:
208 mode: sgml
209 sgml-omittag:nil
210 sgml-shorttag:t
211 sgml-minimize-attributes:nil
212 sgml-always-quote-attributes:t
213 sgml-indent-step:1
214 sgml-indent-data:t
215 sgml-parent-document:nil
216 sgml-default-dtd-file:"../reference.ced"
217 sgml-exposed-tags:nil
218 sgml-local-catalogs:"/usr/lib/sgml/catalog"
219 sgml-local-ecat-files:nil
220 End:
221 -->