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

SELECT INTO

SELECT INTO <refpurpose>define a new table from the results of a query</refpurpose> — 問い合わせの結果からの新しいテーブルを定義する

概要

[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ [ AS ] output_name ] [, ...]
    INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition ]
    [ WINDOW window_name AS ( window_definition ) [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start [ ROW | ROWS ] ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
    [ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]

説明

<title>Description</title>

<command>SELECT INTO</command> creates a new table and fills it with data computed by a query. The data is not returned to the client, as it is with a normal <command>SELECT</command>. The new table's columns have the names and data types associated with the output columns of the <command>SELECT</command>. SELECT INTOは新しいテーブルを作成し、そこに問い合わせによって計算したデータを格納します。 このデータは通常のSELECTのようにはクライアントに返されません。 新しいテーブルの列はSELECTの出力列に関連するデータ型と名前を持ちます。

パラメータ

<title>Parameters</title>
TEMPORARYまたはTEMP

If specified, the table is created as a temporary table. Refer to <xref linkend="sql-createtable"/> for details. このオプションが指定された場合、テーブルは一時テーブルとして作成されます。 詳細はCREATE TABLEを参照してください。

UNLOGGED

If specified, the table is created as an unlogged table. Refer to <xref linkend="sql-createtable"/> for details. 指定された場合、テーブルはログをとらないテーブルとして作成されます。 詳細はCREATE TABLEを参照してください。

new_table

The name (optionally schema-qualified) of the table to be created. 作成するテーブルの名前です(スキーマ修飾名も可)。

All other parameters are described in detail under <xref linkend="sql-select"/>. その他のパラメータについては、SELECTで詳細に説明されています。

注釈

<title>Notes</title>

<link linkend="sql-createtableas"><command>CREATE TABLE AS</command></link> is functionally similar to <command>SELECT INTO</command>. <command>CREATE TABLE AS</command> is the recommended syntax, since this form of <command>SELECT INTO</command> is not available in <application>ECPG</application> or <application>PL/pgSQL</application>, because they interpret the <literal>INTO</literal> clause differently. Furthermore, <command>CREATE TABLE AS</command> offers a superset of the functionality provided by <command>SELECT INTO</command>. CREATE TABLE ASは機能的にはSELECT INTOと同等です。 ECPGPL/pgSQLではINTO句の解釈が異なるため、SELECT INTOという形式は使用できません。 そのため、CREATE TABLE AS構文を使用することをお勧めします。 さらに、CREATE TABLE ASは、SELECT INTOの機能に加え、さらに多くの機能を提供します。

In contrast to <command>CREATE TABLE AS</command>, <command>SELECT INTO</command> does not allow specifying properties like a table's access method with <xref linkend="sql-createtable-method" /> or the table's tablespace with <xref linkend="sql-createtable-tablespace" />. Use <command>CREATE TABLE AS</command> if necessary. Therefore, the default table access method is chosen for the new table. See <xref linkend="guc-default-table-access-method"/> for more information. CREATE TABLE ASとは対照的に、SELECT INTOではUSING methodでのテーブルアクセスメソッドやTABLESPACE tablespace_nameでのテーブルのテーブル空間のような属性を指定できません。 必要ならCREATE TABLE ASを使ってください。 そのため、新しいテーブルにはデフォルトテーブルアクセスメソッドが選ばれます。 より詳細な情報はdefault_table_access_methodを参照してください。

<title>Examples</title>

Create a new table <literal>films_recent</literal> consisting of only recent entries from the table <literal>films</literal>: テーブルfilmsの最近の項目のみから構成される、新しいテーブルfilms_recentを作成します。

SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';

互換性

<title>Compatibility</title>

The SQL standard uses <command>SELECT INTO</command> to represent selecting values into scalar variables of a host program, rather than creating a new table. This indeed is the usage found in <application>ECPG</application> (see <xref linkend="ecpg"/>) and <application>PL/pgSQL</application> (see <xref linkend="plpgsql"/>). The <productname>PostgreSQL</productname> usage of <command>SELECT INTO</command> to represent table creation is historical. Some other SQL implementations also use <command>SELECT INTO</command> in this way (but most SQL implementations support <command>CREATE TABLE AS</command> instead). Apart from such compatibility considerations, it is best to use <command>CREATE TABLE AS</command> for this purpose in new code. 標準SQLでは、SELECT INTOは新しいテーブルの作成ではなく、選択した値をホストプログラムのスカラ変数とするために使われます。 これは実際、ECPG第36章を参照)やPL/pgSQL第43章を参照)で見られる使用方法です。 PostgreSQLにおいて、テーブルを作成するSELECT INTOの用法は歴史的なものです。 他のSQL実装でもSELECT INTOをこのように使っているものがあります(が、ほとんどのSQL実装は、その代わりにCREATE TABLE ASをサポートしています)。 そのような互換性の考慮を除けば、新しいコードでは、この目的のためにはCREATE TABLE ASを使うのが最善です。

関連項目

<title>See Also</title> CREATE TABLE AS