1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-29 13:56:47 +03:00

doc: Make UPDATE FROM examples consistent

The original first half of the example used an employees table and an
accounts.sales_person foreign key column, while the second half (added
in commit 8f889b1083f) used a salesmen table and accounts.sales_id
for the foreign key.  This makes everything use the original names.

Author: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Discussion: https://postgr.es/m/87o81vqjw0.fsf@wibble.ilmari.org
This commit is contained in:
Daniel Gustafsson 2022-03-29 14:53:20 +02:00
parent ebc8b7d441
commit 3785d8e98b

View File

@ -387,23 +387,23 @@ UPDATE employees SET sales_count = sales_count + 1 WHERE id =
<para> <para>
Update contact names in an accounts table to match the currently assigned Update contact names in an accounts table to match the currently assigned
salesmen: salespeople:
<programlisting> <programlisting>
UPDATE accounts SET (contact_first_name, contact_last_name) = UPDATE accounts SET (contact_first_name, contact_last_name) =
(SELECT first_name, last_name FROM salesmen (SELECT first_name, last_name FROM employees
WHERE salesmen.id = accounts.sales_id); WHERE employees.id = accounts.sales_person);
</programlisting> </programlisting>
A similar result could be accomplished with a join: A similar result could be accomplished with a join:
<programlisting> <programlisting>
UPDATE accounts SET contact_first_name = first_name, UPDATE accounts SET contact_first_name = first_name,
contact_last_name = last_name contact_last_name = last_name
FROM salesmen WHERE salesmen.id = accounts.sales_id; FROM employees WHERE employees.id = accounts.sales_person;
</programlisting> </programlisting>
However, the second query may give unexpected results However, the second query may give unexpected results
if <structname>salesmen</structname>.<structfield>id</structfield> is not a unique key, whereas if <structname>employees</structname>.<structfield>id</structfield> is not a unique key, whereas
the first query is guaranteed to raise an error if there are multiple the first query is guaranteed to raise an error if there are multiple
<structfield>id</structfield> matches. Also, if there is no match for a particular <structfield>id</structfield> matches. Also, if there is no match for a particular
<structname>accounts</structname>.<structfield>sales_id</structfield> entry, the first query <structname>accounts</structname>.<structfield>sales_person</structfield> entry, the first query
will set the corresponding name fields to NULL, whereas the second query will set the corresponding name fields to NULL, whereas the second query
will not update that row at all. will not update that row at all.
</para> </para>