{"Entry":{"collection":"sql","key":"select-into","name":"SELECT INTO","aliases":["selectinto"],"metadata":{"aliases":["selectinto"],"changed_in":["6.5","7.0","7.1","7.2","7.3","8.1","8.2","8.3","8.4","9.1","9.5","12","19"],"changes":[{"from":"6.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["INTO [TEMP] [ TABLE ] new_table ]","[ { UNION [ALL] | INTERSECT | EXCEPT } select]","[ FOR UPDATE [OF class_name...]]","[ LIMIT count [OFFSET|, count]]"],"removed":[]},"to":"6.5"},{"from":"6.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]","[ INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table ]","[ ORDER BY column [ ASC | DESC | USING operator ] [, ...] ]","[ FOR UPDATE [ OF class_name [, ...] ] ]","LIMIT { count | ALL } [ { OFFSET | , } start ]"],"removed":["[ LIMIT count [OFFSET|, count]]"]},"to":"7.0"},{"from":"7.0","purpose_changed":true,"renamed":{"from_file":"sql-selectinto.htm","to_file":"sql-selectinto.html"},"sections":{"added":["compatibility"],"changed":["description"],"removed":[]},"status":"changed","synopsis":{"added":["* | expression [ AS output_name ] [, ...]","[ FROM from_item [, ...] ]","[ GROUP BY expression [, ...] ]","[ { UNION | INTERSECT | EXCEPT [ ALL ] } select ]","[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]","[ FOR UPDATE [ OF tablename [, ...] ] ]","[ LIMIT { count | ALL } [ { OFFSET | , } start ]]","where from_item can be:","[ ONLY ] table_name [ * ]","[ [ AS ] alias [ ( column_alias_list ) ] ]","|","( select )","[ AS ] alias [ ( column_alias_list ) ]","|","from_item [ NATURAL ] join_type from_item","[ ON join_condition | USING ( join_column_list ) ]"],"removed":["expression [ AS name ] [, ...]","[ INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table ]","[ FROM table [ alias ] [, ...] ]","[ GROUP BY column [, ...] ]","[ { UNION [ ALL ] | INTERSECT | EXCEPT } select ]","[ ORDER BY column [ ASC | DESC | USING operator ] [, ...] ]","[ FOR UPDATE [ OF class_name [, ...] ] ]"]},"to":"7.1"},{"from":"7.1","purpose_changed":true,"renamed":null,"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]","[ LIMIT [ start , ] { count | ALL } ]"],"removed":["[ { UNION | INTERSECT | EXCEPT [ ALL ] } select ]","[ LIMIT { count | ALL } [ { OFFSET | , } start ]]"]},"to":"7.2"},{"from":"7.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ LIMIT { count | ALL } ]","[ OFFSET start ]","[ FOR UPDATE [ OF tablename [, ...] ] ]"],"removed":["[ LIMIT [ start , ] { count | ALL } ]","[ OFFSET start ]","where from_item can be:","[ ONLY ] table_name [ * ]","[ [ AS ] alias [ ( column_alias_list ) ] ]","|","( select )","[ AS ] alias [ ( column_alias_list ) ]","|","from_item [ NATURAL ] join_type from_item","[ ON join_condition | USING ( join_column_list ) ]"]},"to":"7.3"},{"from":"7.3","purpose_changed":false,"renamed":null,"sections":{"added":["parameters","notes"],"changed":["description","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"7.4"},{"from":"7.4","purpose_changed":true,"renamed":null,"sections":{"added":["examples","see_also"],"changed":["notes","compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"8.0"},{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] ]"],"removed":["[ FOR UPDATE [ OF tablename [, ...] ] ]"]},"to":"8.1"},{"from":"8.1","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]"],"removed":[]},"to":"8.2"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]"],"removed":[]},"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["[ WITH [ RECURSIVE ] with_query [, ...] ]","* | expression [ [ AS ] output_name ] [, ...]","[ HAVING condition [, ...] ]","[ WINDOW window_name AS ( window_definition ) [, ...] ]","[ OFFSET start [ ROW | ROWS ] ]","[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]"],"removed":[]},"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"9.0"},{"from":"9.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["parameters"],"removed":[]},"status":"changed","synopsis":{"added":["INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table","[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]"],"removed":[]},"to":"9.1"},{"from":"9.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"9.3"},{"from":"9.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.4"},{"from":"9.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":[],"removed":["[ HAVING condition [, ...] ]"]},"to":"9.5"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"10"},{"from":"10","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"11"},{"from":"11","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes","compatibility"],"removed":[]},"status":"changed","synopsis":{"added":["[ { * | expression [ [ AS ] output_name ] } [, ...] ]"],"removed":[]},"to":"12"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"14"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["compatibility"],"removed":[]},"status":"changed","synopsis":null,"to":"17"},{"from":"18","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"changed","synopsis":{"added":["[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]","[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]","[ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF from_reference [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ]"],"removed":["[ GROUP BY expression [, ...] ]","[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]"]},"to":"19"}],"content_hash":"4997cff2f7f2f5018adf89a0db7923a3a2e98fba277d8b0434999040d8762c72","editorial":{},"first_version":"6.4","group":"query","imported_at":"2026-09-30T17:43:39.128205+08:00","last_version":"20","name":"SELECT INTO","object":"INTO","position":11009,"present_in":["6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"define a new table from the results of a query","purpose_zh":"","related":["create-table-as"],"slug":"select-into","source_rev":"a709ab85","synopsis":"[ WITH [ RECURSIVE ] with_query [, ...] ]\nSELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]\n[ { * | expression [ [ AS ] output_name ] } [, ...] ]\nINTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table\n[ FROM from_item [, ...] ]\n[ WHERE condition ]\n[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]\n[ HAVING condition ]\n[ WINDOW window_name AS ( window_definition ) [, ...] ]\n[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]\n[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]\n[ LIMIT { count | ALL } ]\n[ OFFSET start [ ROW | ROWS ] ]\n[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } { ONLY | WITH TIES } ]\n[ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF from_reference [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ]","verb":"SELECT"}},"Definition":{"Collection":"sql","Key":"select-into","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"select-into","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-SELECTINTO","file":"sql-selectinto.html","lang":"en","name":"SELECT INTO","purpose":"define a new table from the results of a query","purpose_zh":"","related":["create-table-as"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e 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 \u003ccode class=\"command\"\u003eSELECT\u003c/code\u003e. The new table's columns have the names and data types associated with the output columns of the \u003ccode class=\"command\"\u003eSELECT\u003c/code\u003e.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eTEMPORARY\u003c/code\u003e or \u003ccode class=\"literal\"\u003eTEMP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf specified, the table is created as a temporary table. Refer to \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e for details.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eIf specified, the table is created as an unlogged table. Refer to \u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e for details.\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_table\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eThe name (optionally schema-qualified) of the table to be created.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003eAll other parameters are described in detail under \u003ca href=\"/docs/18/sql-select.html\" title=\"SELECT\"\u003e\u003cspan class=\"refentrytitle\"\u003eSELECT\u003c/span\u003e\u003c/a\u003e.\u003c/p\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003e\u003ca href=\"/docs/18/sql-createtableas.html\" title=\"CREATE TABLE AS\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e\u003c/a\u003e is functionally similar to \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e. \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e is the recommended syntax, since this form of \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e is not available in \u003cspan class=\"application\"\u003eECPG\u003c/span\u003e or \u003cspan class=\"application\"\u003ePL/pgSQL\u003c/span\u003e, because they interpret the \u003ccode class=\"literal\"\u003eINTO\u003c/code\u003e clause differently. Furthermore, \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e offers a superset of the functionality provided by \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eIn contrast to \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e, \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e does not allow specifying properties like a table's access method with \u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-METHOD\"\u003e\u003ccode class=\"literal\"\u003eUSING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/a\u003e or the table's tablespace with \u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-TABLESPACE\"\u003e\u003ccode class=\"literal\"\u003eTABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/a\u003e. Use \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e if necessary. Therefore, the default table access method is chosen for the new table. See \u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\"\u003edefault_table_access_method\u003c/a\u003e for more information.\u003c/p\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003eCreate a new table \u003ccode class=\"literal\"\u003efilms_recent\u003c/code\u003e consisting of only recent entries from the table \u003ccode class=\"literal\"\u003efilms\u003c/code\u003e:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eSELECT * INTO films_recent FROM films WHERE date_prod \u0026gt;= '2002-01-01';\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003eThe SQL standard uses \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e to represent selecting values into scalar variables of a host program, rather than creating a new table. This indeed is the usage found in \u003cspan class=\"application\"\u003eECPG\u003c/span\u003e (see \u003ca href=\"/docs/18/ecpg.html\" title=\"Chapter 34. ECPG — Embedded SQL in C\"\u003eChapter 34\u003c/a\u003e) and \u003cspan class=\"application\"\u003ePL/pgSQL\u003c/span\u003e (see \u003ca href=\"/docs/18/plpgsql.html\" title=\"Chapter 41. PL/pgSQL — SQL Procedural Language\"\u003eChapter 41\u003c/a\u003e). The \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e usage of \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e to represent table creation is historical. Some other SQL implementations also use \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e in this way (but most SQL implementations support \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e instead). Apart from such compatibility considerations, it is best to use \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e for this purpose in new code.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/create-table-as/?v=18\" title=\"CREATE TABLE AS\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE AS\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"[ WITH [ RECURSIVE ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ewith_query\u003c/code\u003e\u003c/em\u003e [, ...] ]\nSELECT [ ALL | DISTINCT [ ON ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [, ...] ) ] ]\n    [ { * | \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [ [ AS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoutput_name\u003c/code\u003e\u003c/em\u003e ] } [, ...] ]\n    INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_table\u003c/code\u003e\u003c/em\u003e\n    [ FROM \u003cem class=\"replaceable\"\u003e\u003ccode\u003efrom_item\u003c/code\u003e\u003c/em\u003e [, ...] ]\n    [ WHERE \u003cem class=\"replaceable\"\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\n    [ GROUP BY \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [, ...] ]\n    [ HAVING \u003cem class=\"replaceable\"\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\n    [ WINDOW \u003cem class=\"replaceable\"\u003e\u003ccode\u003ewindow_name\u003c/code\u003e\u003c/em\u003e AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ewindow_definition\u003c/code\u003e\u003c/em\u003e ) [, ...] ]\n    [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eselect\u003c/code\u003e\u003c/em\u003e ]\n    [ ORDER BY \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [ ASC | DESC | USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoperator\u003c/code\u003e\u003c/em\u003e ] [ NULLS { FIRST | LAST } ] [, ...] ]\n    [ LIMIT { \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e | ALL } ]\n    [ OFFSET \u003cem class=\"replaceable\"\u003e\u003ccode\u003estart\u003c/code\u003e\u003c/em\u003e [ ROW | ROWS ] ]\n    [ FETCH { FIRST | NEXT } [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e ] { ROW | ROWS } ONLY ]\n    [ FOR { UPDATE | SHARE } [ OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e [, ...] ] [ NOWAIT ] [...] ]","synopsis_text":"[ WITH [ RECURSIVE ] with_query [, ...] ]\nSELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]\n[ { * | expression [ [ AS ] output_name ] } [, ...] ]\nINTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table\n[ FROM from_item [, ...] ]\n[ WHERE condition ]\n[ GROUP BY expression [, ...] ]\n[ HAVING condition ]\n[ WINDOW window_name AS ( window_definition ) [, ...] ]\n[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]\n[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]\n[ LIMIT { count | ALL } ]\n[ OFFSET start [ ROW | ROWS ] ]\n[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]\n[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"select-into","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"SELECT INTO","Summary":"根据查询结果定义一个新表","BodyHTML":"\u003cpre\u003e[ WITH [ RECURSIVE ] with_query [, ...] ]\nSELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]\n[ { * | expression [ [ AS ] output_name ] } [, ...] ]\nINTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table\n[ FROM from_item [, ...] ]\n[ WHERE condition ]\n[ GROUP BY expression [, ...] ]\n[ HAVING condition ]\n[ WINDOW window_name AS ( window_definition ) [, ...] ]\n[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]\n[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]\n[ LIMIT { count | ALL } ]\n[ OFFSET start [ ROW | ROWS ] ]\n[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]\n[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003eSELECT INTO\u003c/code\u003e创建一个新表，并用查询计算得到的数据填充该表。与普通的\u003ccode\u003eSELECT\u003c/code\u003e不同，这些数据不会返回给客户端。新表的列具有与\u003ccode\u003eSELECT\u003c/code\u003e输出列对应的名称和数据类型。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eTEMPORARY\u003c/code\u003e 或 \u003ccode\u003eTEMP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定，将把该表创建为临时表。详见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003ccode\u003eUNLOGGED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定，该表将创建为不记录 WAL 的表。详见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003enew_table\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要创建的表名（可选地带模式限定）。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e所有其他参数都在\u003ca href=\"/docs/18/sql-select.html\" title=\"SELECT\" rel=\"nofollow\"\u003e\u003cspan\u003eSELECT\u003c/span\u003e\u003c/a\u003e中有详细说明。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\u003ca href=\"/docs/18/sql-createtableas.html\" title=\"CREATE TABLE AS\" rel=\"nofollow\"\u003e\u003ccode\u003eCREATE TABLE AS\u003c/code\u003e\u003c/a\u003e在功能上与 \u003ccode\u003eSELECT INTO\u003c/code\u003e类似。\u003ccode\u003eCREATE TABLE AS\u003c/code\u003e 是推荐使用的语法，因为这种形式的\u003ccode\u003eSELECT INTO\u003c/code\u003e在\u003cspan\u003eECPG\u003c/span\u003e 或\u003cspan\u003ePL/pgSQL\u003c/span\u003e中不可用，因为它们对 \u003ccode\u003eINTO\u003c/code\u003e子句有不同的解释。此外，\u003ccode\u003eCREATE TABLE AS\u003c/code\u003e提供的功能是 \u003ccode\u003eSELECT INTO\u003c/code\u003e所提供功能的超集。\u003c/p\u003e\u003cp\u003e与\u003ccode\u003eCREATE TABLE AS\u003c/code\u003e不同，\u003ccode\u003eSELECT INTO\u003c/code\u003e不允许使用\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-METHOD\" rel=\"nofollow\"\u003e\u003ccode\u003eUSING \u003cem\u003e\u003ccode\u003emethod\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/a\u003e指定诸如表访问方法之类的属性，也不允许使用\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-TABLESPACE\" rel=\"nofollow\"\u003e\u003ccode\u003eTABLESPACE \u003cem\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/a\u003e指定表空间。如有需要，请使用 \u003ccode\u003eCREATE TABLE AS\u003c/code\u003e。因此，新表会选用默认的表访问方法。更多信息请参见\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\" rel=\"nofollow\"\u003edefault_table_access_method\u003c/a\u003e。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e创建一个新表\u003ccode\u003efilms_recent\u003c/code\u003e，它只包含表 \u003ccode\u003efilms\u003c/code\u003e中的最近条目：\u003c/p\u003e\u003cpre\u003eSELECT * INTO films_recent FROM films WHERE date_prod \u0026gt;= \u0026#39;2002-01-01\u0026#39;;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003eSQL 标准使用\u003ccode\u003eSELECT INTO\u003c/code\u003e表示将值选入宿主程序的标量变量，而不是创建一个新表。这确实是 \u003cspan\u003eECPG\u003c/span\u003e（见\u003ca href=\"/docs/18/ecpg.html\" rel=\"nofollow\"\u003e第 34 章\u003c/a\u003e）和 \u003cspan\u003ePL/pgSQL\u003c/span\u003e（见\u003ca href=\"/docs/18/plpgsql.html\" rel=\"nofollow\"\u003e第 41 章\u003c/a\u003e）中的用法。\u003cspan\u003ePostgreSQL\u003c/span\u003e使用\u003ccode\u003eSELECT INTO\u003c/code\u003e表示创建表则是历史遗留用法。其他一些 SQL 实现也以这种方式使用\u003ccode\u003eSELECT INTO\u003c/code\u003e（但大多数 SQL 实现改为支持\u003ccode\u003eCREATE TABLE AS\u003c/code\u003e）。撇开这类兼容性考虑，对于新代码，最好为此目的使用\u003ccode\u003eCREATE TABLE AS\u003c/code\u003e。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/create-table-as/?v=18\" title=\"CREATE TABLE AS\" rel=\"nofollow\"\u003e\u003cspan\u003eCREATE TABLE AS\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"d9ec370f85a897032a133b22a229b90fc92e05a177f64b1d7f65947b3e74591c","Payload":{"purpose_zh":"根据查询结果定义一个新表","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e创建一个新表，并用查询计算得到的数据填充该表。与普通的\u003ccode class=\"command\"\u003eSELECT\u003c/code\u003e不同，这些数据不会返回给客户端。新表的列具有与\u003ccode class=\"command\"\u003eSELECT\u003c/code\u003e输出列对应的名称和数据类型。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eTEMPORARY\u003c/code\u003e 或 \u003ccode class=\"literal\"\u003eTEMP\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定，将把该表创建为临时表。详见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003ccode class=\"literal\"\u003eUNLOGGED\u003c/code\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e如果指定，该表将创建为不记录 WAL 的表。详见\u003ca href=\"/docs/18/sql-createtable.html\" title=\"CREATE TABLE\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE\u003c/span\u003e\u003c/a\u003e。\u003c/p\u003e\u003c/dd\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_table\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e要创建的表名（可选地带模式限定）。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003cp\u003e所有其他参数都在\u003ca href=\"/docs/18/sql-select.html\" title=\"SELECT\"\u003e\u003cspan class=\"refentrytitle\"\u003eSELECT\u003c/span\u003e\u003c/a\u003e中有详细说明。\u003c/p\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e\u003ca href=\"/docs/18/sql-createtableas.html\" title=\"CREATE TABLE AS\"\u003e\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e\u003c/a\u003e在功能上与 \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e类似。\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e 是推荐使用的语法，因为这种形式的\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e在\u003cspan class=\"application\"\u003eECPG\u003c/span\u003e 或\u003cspan class=\"application\"\u003ePL/pgSQL\u003c/span\u003e中不可用，因为它们对 \u003ccode class=\"literal\"\u003eINTO\u003c/code\u003e子句有不同的解释。此外，\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e提供的功能是 \u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e所提供功能的超集。\u003c/p\u003e\u003cp\u003e与\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e不同，\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e不允许使用\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-METHOD\"\u003e\u003ccode class=\"literal\"\u003eUSING \u003cem class=\"replaceable\"\u003e\u003ccode\u003emethod\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/a\u003e指定诸如表访问方法之类的属性，也不允许使用\u003ca href=\"/docs/18/sql-createtable.html#SQL-CREATETABLE-TABLESPACE\"\u003e\u003ccode class=\"literal\"\u003eTABLESPACE \u003cem class=\"replaceable\"\u003e\u003ccode\u003etablespace_name\u003c/code\u003e\u003c/em\u003e\u003c/code\u003e\u003c/a\u003e指定表空间。如有需要，请使用 \u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e。因此，新表会选用默认的表访问方法。更多信息请参见\u003ca href=\"/docs/18/runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD\"\u003edefault_table_access_method\u003c/a\u003e。\u003c/p\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e创建一个新表\u003ccode class=\"literal\"\u003efilms_recent\u003c/code\u003e，它只包含表 \u003ccode class=\"literal\"\u003efilms\u003c/code\u003e中的最近条目：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003eSELECT * INTO films_recent FROM films WHERE date_prod \u0026gt;= '2002-01-01';\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003eSQL 标准使用\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e表示将值选入宿主程序的标量变量，而不是创建一个新表。这确实是 \u003cspan class=\"application\"\u003eECPG\u003c/span\u003e（见\u003ca href=\"/docs/18/ecpg.html\" title=\"第 34 章 ECPG — C 中的嵌入式 SQL\"\u003e第 34 章\u003c/a\u003e）和 \u003cspan class=\"application\"\u003ePL/pgSQL\u003c/span\u003e（见\u003ca href=\"/docs/18/plpgsql.html\" title=\"第 41 章 PL/pgSQL — SQL 过程语言\"\u003e第 41 章\u003c/a\u003e）中的用法。\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e使用\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e表示创建表则是历史遗留用法。其他一些 SQL 实现也以这种方式使用\u003ccode class=\"command\"\u003eSELECT INTO\u003c/code\u003e（但大多数 SQL 实现改为支持\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e）。撇开这类兼容性考虑，对于新代码，最好为此目的使用\u003ccode class=\"command\"\u003eCREATE TABLE AS\u003c/code\u003e。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/create-table-as/?v=18\" title=\"CREATE TABLE AS\"\u003e\u003cspan class=\"refentrytitle\"\u003eCREATE TABLE AS\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"[ WITH [ RECURSIVE ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003ewith_query\u003c/code\u003e\u003c/em\u003e [, ...] ]\nSELECT [ ALL | DISTINCT [ ON ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [, ...] ) ] ]\n    [ { * | \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [ [ AS ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoutput_name\u003c/code\u003e\u003c/em\u003e ] } [, ...] ]\n    INTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003enew_table\u003c/code\u003e\u003c/em\u003e\n    [ FROM \u003cem class=\"replaceable\"\u003e\u003ccode\u003efrom_item\u003c/code\u003e\u003c/em\u003e [, ...] ]\n    [ WHERE \u003cem class=\"replaceable\"\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\n    [ GROUP BY \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [, ...] ]\n    [ HAVING \u003cem class=\"replaceable\"\u003e\u003ccode\u003econdition\u003c/code\u003e\u003c/em\u003e ]\n    [ WINDOW \u003cem class=\"replaceable\"\u003e\u003ccode\u003ewindow_name\u003c/code\u003e\u003c/em\u003e AS ( \u003cem class=\"replaceable\"\u003e\u003ccode\u003ewindow_definition\u003c/code\u003e\u003c/em\u003e ) [, ...] ]\n    [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] \u003cem class=\"replaceable\"\u003e\u003ccode\u003eselect\u003c/code\u003e\u003c/em\u003e ]\n    [ ORDER BY \u003cem class=\"replaceable\"\u003e\u003ccode\u003eexpression\u003c/code\u003e\u003c/em\u003e [ ASC | DESC | USING \u003cem class=\"replaceable\"\u003e\u003ccode\u003eoperator\u003c/code\u003e\u003c/em\u003e ] [ NULLS { FIRST | LAST } ] [, ...] ]\n    [ LIMIT { \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e | ALL } ]\n    [ OFFSET \u003cem class=\"replaceable\"\u003e\u003ccode\u003estart\u003c/code\u003e\u003c/em\u003e [ ROW | ROWS ] ]\n    [ FETCH { FIRST | NEXT } [ \u003cem class=\"replaceable\"\u003e\u003ccode\u003ecount\u003c/code\u003e\u003c/em\u003e ] { ROW | ROWS } ONLY ]\n    [ FOR { UPDATE | SHARE } [ OF \u003cem class=\"replaceable\"\u003e\u003ccode\u003etable_name\u003c/code\u003e\u003c/em\u003e [, ...] ] [ NOWAIT ] [...] ]","synopsis_text":"[ WITH [ RECURSIVE ] with_query [, ...] ]\nSELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]\n[ { * | expression [ [ AS ] output_name ] } [, ...] ]\nINTO [ TEMPORARY | TEMP | UNLOGGED ] [ TABLE ] new_table\n[ FROM from_item [, ...] ]\n[ WHERE condition ]\n[ GROUP BY expression [, ...] ]\n[ HAVING condition ]\n[ WINDOW window_name AS ( window_definition ) [, ...] ]\n[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]\n[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]\n[ LIMIT { count | ALL } ]\n[ OFFSET start [ ROW | ROWS ] ]\n[ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]\n[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","6.4","6.5","7.0","7.1","7.2","7.3","7.4","8.0","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
