バージョンごとのドキュメント一覧

6.4. 更新された行のデータを返す #

<title>Returning Data from Modified Rows</title>

Sometimes it is useful to obtain data from modified rows while they are being manipulated. The <command>INSERT</command>, <command>UPDATE</command>, and <command>DELETE</command> commands all have an optional <literal>RETURNING</literal> clause that supports this. Use of <literal>RETURNING</literal> avoids performing an extra database query to collect the data, and is especially valuable when it would otherwise be difficult to identify the modified rows reliably. 行が更新されるときに、その行のデータを取得できると便利なことがあります。 INSERTUPDATEDELETEの各コマンドは、いずれもオプションのRETURNING句によりそれが可能となっています。 RETURNINGを使うことで、行を取得するために余分なデータベースへの問い合わせを行うことを避けられ、それ以外の方法で更新された行を確実に特定するのが難しい場合には、これは特に貴重です。

The allowed contents of a <literal>RETURNING</literal> clause are the same as a <command>SELECT</command> command's output list (see <xref linkend="queries-select-lists"/>). It can contain column names of the command's target table, or value expressions using those columns. A common shorthand is <literal>RETURNING *</literal>, which selects all columns of the target table in order. RETURNING句で使用できる項目はSELECTコマンドの出力リスト(7.3参照)と同じです。 コマンドの対象となっているテーブルの列名、あるいはそれらの列を使った値の式を入れることができます。 よく使われる省略記法はRETURNING *で、これは対象テーブルのすべての列を順に返します。

In an <command>INSERT</command>, the data available to <literal>RETURNING</literal> is the row as it was inserted. This is not so useful in trivial inserts, since it would just repeat the data provided by the client. But it can be very handy when relying on computed default values. For example, when using a <link linkend="datatype-serial"><type>serial</type></link> column to provide unique identifiers, <literal>RETURNING</literal> can return the ID assigned to a new row: INSERTでは、RETURNINGで利用できるデータは、挿入された通りの行です。 単純な挿入では、クライアントが提供したデータを単に繰り返すだけになりますから、あまり役には立ちません。 しかし、計算されたデフォルト値に依存しているときは、これは非常に便利なことがあります。 例えばserialの列を使って一意識別子を提供している場合、以下のようにRETURNINGによって、新しい行に割り当てられたIDを返すことができます。

CREATE TABLE users (firstname text, lastname text, id serial primary key);

INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id;

The <literal>RETURNING</literal> clause is also very useful with <literal>INSERT ... SELECT</literal>. また、RETURNING句はINSERT ... SELECTでも非常に役に立ちます。

In an <command>UPDATE</command>, the data available to <literal>RETURNING</literal> is the new content of the modified row. For example: UPDATEでは、RETURNINGで利用できるデータは、更新された行の新しい内容です。 例を示します。

UPDATE products SET price = price * 1.10
  WHERE price <= 99.99
  RETURNING name, price AS new_price;

In a <command>DELETE</command>, the data available to <literal>RETURNING</literal> is the content of the deleted row. For example: DELETEでは、RETURNINGで利用できるデータは、削除された行の内容です。 例を示します。

DELETE FROM products
  WHERE obsoletion_date = 'today'
  RETURNING *;

If there are triggers (<xref linkend="triggers"/>) on the target table, the data available to <literal>RETURNING</literal> is the row as modified by the triggers. Thus, inspecting columns computed by triggers is another common use-case for <literal>RETURNING</literal>. 対象のテーブルにトリガー(第39章)がある場合、RETURNINGで利用できるデータは、トリガーで更新された行です。 従って、トリガーによって計算された列を検査するのもRETURNINGの一般的な利用方法の一つです。