1
1
<!--
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 $
3
3
PostgreSQL documentation
4
4
-->
5
5
@@ -114,8 +114,9 @@ UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replacea
114
114
expressions. This is similar to the list of tables that can be
115
115
specified in the <xref linkend="sql-from"
116
116
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</>).
119
120
</para>
120
121
</listitem>
121
122
</varlistentry>
@@ -154,10 +155,13 @@ UPDATE <replaceable class="parameter">count</replaceable>
154
155
<title>Notes</title>
155
156
156
157
<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
159
163
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
161
165
the other table(s). If it does, then only one of the join rows
162
166
will be used to update the target row, but which one will be used
163
167
is not readily predictable.
@@ -210,15 +214,18 @@ UPDATE employees SET sales_count = sales_count + 1 WHERE id =
210
214
</programlisting>
211
215
212
216
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.
215
219
<programlisting>
216
220
BEGIN;
221
+ -- other operations
217
222
SAVEPOINT sp1;
218
223
INSERT 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:
220
226
ROLLBACK 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
222
229
COMMIT;
223
230
</programlisting>
224
231
</para>
@@ -228,10 +235,18 @@ COMMIT;
228
235
<title>Compatibility</title>
229
236
230
237
<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
233
240
<productname>PostgreSQL</productname> extension.
234
241
</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>
235
250
</refsect1>
236
251
</refentry>
237
252