SELECT INTO
大纲
[ 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 ] [...] ]
参数
- TEMPORARY or TEMP
-
如果被指定,该表被创建为一个临时表。详见 CREATE TABLE。
- UNLOGGED
-
如果被指定,该表被创建为一个不做日志的表。详见 CREATE TABLE。
- new_table
-
要创建的表的名字(可以是模式限定的)。
所有其他参数在SELECT中有详细描述。
注解
CREATE TABLE AS在功能上与 SELECT INTO相似。CREATE TABLE AS 是被推荐的语法,因为这种形式的SELECT INTO在ECPG 或PL/pgSQL中不可用,因为它们对 INTO子句的解释不同。此外, CREATE TABLE AS提供的功能是 SELECT INTO的超集。
要为SELECT INTO创建的表增加 OID, 启用default_with_oids配置变量。 CREATE TABLE AS可以使用 WITH OIDS子句。
示例
创建一个只由来自films的最近项构成的 新表films_recent:
SELECT * INTO films_recent FROM films WHERE date_prod >= '2002-01-01';