11<!--
2- $PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.31 2005/01/04 03:58:16 tgl Exp $
2+ $PostgreSQL: pgsql/doc/src/sgml/ref/update.sgml,v 1.32 2005/01/09 05:57:45 tgl Exp $
33PostgreSQL documentation
44-->
55
@@ -114,8 +114,9 @@ UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replacea
114114 expressions. This is similar to the list of tables that can be
115115 specified in the <xref linkend="sql-from"
116116 endterm="sql-from-title"> of a <command>SELECT</command>
117- statement; for example, an alias for the table name can be
118- specified.
117+ statement. Note that the target table must not appear in the
118+ <replaceable>fromlist</>, unless you intend a self-join (in which
119+ case it must appear with an alias in the <replaceable>fromlist</>).
119120 </para>
120121 </listitem>
121122 </varlistentry>
@@ -154,10 +155,13 @@ UPDATE <replaceable class="parameter">count</replaceable>
154155 <title>Notes</title>
155156
156157 <para>
157- When joining the target table to other tables using a <replaceable
158- class="PARAMETER">fromlist</replaceable>, be careful that the join
158+ When a <literal>FROM</> clause is present, what essentially happens
159+ is that the target table is joined to the tables mentioned in the
160+ <replaceable>fromlist</replaceable>, and each output row of the join
161+ represents an update operation for the target table. When using
162+ <literal>FROM</> you should ensure that the join
159163 produces at most one output row for each row to be modified. In
160- other words, a target rowmustn 't join to more than one row from
164+ other words, a target rowshouldn 't join to more than one row from
161165 the other table(s). If it does, then only one of the join rows
162166 will be used to update the target row, but which one will be used
163167 is not readily predictable.
@@ -210,15 +214,18 @@ UPDATE employees SET sales_count = sales_count + 1 WHERE id =
210214</programlisting>
211215
212216 Attempt to insert a new stock item along with the quantity of stock. If
213- the item exists, update the stock count of the existing item. To do this,
214- use savepoints.
217+ the itemalready exists,instead update the stock count of the existing
218+ item. To do this without failing the entire transaction, use savepoints.
215219<programlisting>
216220BEGIN;
221+ -- other operations
217222SAVEPOINT sp1;
218223INSERT INTO wines VALUES('Chateau Lafite 2003', '24');
219- -- Check for unique violation on name
224+ -- Assume the above fails because of a unique key violation,
225+ -- so now we issue these commands:
220226ROLLBACK TO sp1;
221- UPDATE wines SET stock = stock + 24 WHERE winename='Chateau Lafite 2003';
227+ UPDATE wines SET stock = stock + 24 WHERE winename = 'Chateau Lafite 2003';
228+ -- continue with other operations, and eventually
222229COMMIT;
223230</programlisting>
224231 </para>
@@ -228,10 +235,18 @@ COMMIT;
228235 <title>Compatibility</title>
229236
230237 <para>
231- This command conforms to the <acronym>SQL</acronym> standard. The
232- <literal>FROM</literal> clause is a
238+ This command conforms to the <acronym>SQL</acronym> standard, except
239+ that the <literal>FROM</literal> clause is a
233240 <productname>PostgreSQL</productname> extension.
234241 </para>
242+
243+ <para>
244+ Some other database systems offer a <literal>FROM</> option in which
245+ the target table is supposed to be listed again within <literal>FROM</>.
246+ That is not how <productname>PostgreSQL</productname> interprets
247+ <literal>FROM</>. Be careful when porting applications that use this
248+ extension.
249+ </para>
235250 </refsect1>
236251</refentry>
237252