create_table_as.7

来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 184 行

7
184
字号
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE TABLE AS" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE TABLE AS \- define a new table from the results of a query.SH SYNOPSIS.sp.nfCREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE \fItable_name\fR    [ (\fIcolumn_name\fR [, ...] ) ]    [ WITH ( \fIstorage_parameter\fR [= \fIvalue\fR] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]    [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]    [ TABLESPACE \fItablespace\fR ]    AS \fIquery\fR.sp.fi.SH "DESCRIPTION".PP\fBCREATE TABLE AS\fR creates a table and fills itwith data computed by a \fBSELECT\fR command.The table columns have thenames and data types associated with the output columns of the\fBSELECT\fR (except that you can override the columnnames by giving an explicit list of new column names)..PP\fBCREATE TABLE AS\fR bears some resemblance tocreating a view, but it is really quite different: it creates a newtable and evaluates the query just once to fill the new tableinitially. The new table will not track subsequent changes to thesource tables of the query. In contrast, a view re-evaluates itsdefining \fBSELECT\fR statement whenever it isqueried..SH "PARAMETERS".TP\fBGLOBAL or LOCAL\fRIgnored for compatibility. Refer to CREATE TABLE [\fBcreate_table\fR(7)] fordetails..PP.TP\fBTEMPORARY or TEMP\fRIf specified, the table is created as a temporary table.Refer to CREATE TABLE [\fBcreate_table\fR(7)] for details..TP\fB\fItable_name\fB\fRThe name (optionally schema-qualified) of the table to be created..TP\fB\fIcolumn_name\fB\fRThe name of a column in the new table. If column names are notprovided, they are taken from the output column names of thequery. If the table is created from an\fBEXECUTE\fR command, a column name list cannot bespecified..TP\fBWITH ( \fIstorage_parameter\fB [= \fIvalue\fB] [, ... ] )\fRThis clause specifies optional storage parameters for the new table;see Storage Parameters [\fBcreate_table\fR(7)] for moreinformation. The WITH clausecan also include OIDS=TRUE (or just OIDS)to specify that rows of the new tableshould have OIDs (object identifiers) assigned to them, orOIDS=FALSE to specify that the rows should not have OIDs.See CREATE TABLE [\fBcreate_table\fR(7)] for more information..TP\fBWITH OIDS\fR.TP\fBWITHOUT OIDS\fRThese are obsolescent syntaxes equivalent to WITH (OIDS)and WITH (OIDS=FALSE), respectively. If you wish to giveboth an OIDS setting and storage parameters, you must usethe WITH ( ... ) syntax; see above..TP\fBON COMMIT\fRThe behavior of temporary tables at the end of a transactionblock can be controlled using ON COMMIT.The three options are:.RS.TP\fBPRESERVE ROWS\fRNo special action is taken at the ends of transactions.This is the default behavior..TP\fBDELETE ROWS\fRAll rows in the temporary table will be deleted at the endof each transaction block. Essentially, an automatic TRUNCATE [\fBtruncate\fR(7)] is doneat each commit..TP\fBDROP\fRThe temporary table will be dropped at the end of the currenttransaction block..RE.PP.TP\fBTABLESPACE \fItablespace\fB\fRThe \fItablespace\fR is the nameof the tablespace in which the new table is to be created.If not specified,default_tablespace is used, or the database'sdefault tablespace if default_tablespace is an emptystring..TP\fB\fIquery\fB\fRA SELECT [\fBselect\fR(7)] orVALUES [\fBvalues\fR(7)] command,or an EXECUTE [\fBexecute\fR(7)] commandthat runs a prepared \fBSELECT\fR or \fBVALUES\fR query..SH "NOTES".PPThis command is functionally similar to SELECT INTO [\fBselect_into\fR(7)], but it ispreferred since it is less likely to be confused with other uses ofthe \fBSELECT INTO\fR syntax. Furthermore, \fBCREATETABLE AS\fR offers a superset of the functionality offeredby \fBSELECT INTO\fR..PPPrior to PostgreSQL 8.0, \fBCREATETABLE AS\fR always included OIDs in the table itcreated. As of PostgreSQL 8.0,the \fBCREATE TABLE AS\fR command allows the user toexplicitly specify whether OIDs should be included. If thepresence of OIDs is not explicitly specified,the default_with_oids configuration variable isused. As of PostgreSQL 8.1,this variable is false by default, so the default behavior is notidentical to pre-8.0 releases. Applications thatrequire OIDs in the table created by \fBCREATE TABLEAS\fR should explicitly specify WITH (OIDS)to ensure proper behavior..SH "EXAMPLES".PPCreate a new table films_recent consisting of onlyrecent entries from the table films:.sp.nfCREATE TABLE films_recent AS  SELECT * FROM films WHERE date_prod >= '2002-01-01';.sp.fi.PPCreate a new temporary table films_recent, consisting ofonly recent entries from the table films, using aprepared statement. The new table has OIDs and will be dropped at commit:.sp.nfPREPARE recentfilms(date) AS  SELECT * FROM films WHERE date_prod > $1;CREATE TEMP TABLE films_recent WITH (OIDS) ON COMMIT DROP AS  EXECUTE recentfilms('2002-01-01');.sp.fi.SH "COMPATIBILITY".PP\fBCREATE TABLE AS\fR conforms to the SQLstandard, with the following exceptions:.TP 0.2i\(buThe standard requires parentheses around the subquery clause; inPostgreSQL, these parentheses areoptional..TP 0.2i\(buThe standard defines a WITH [ NO ] DATA clause;this is not currently implemented by PostgreSQL.The behavior provided by PostgreSQL is equivalentto the standard's WITH DATA case.WITH NO DATA can be simulated by appendingLIMIT 0 to the query..TP 0.2i\(buPostgreSQL handles temporary tables in a wayrather different from the standard; seeCREATE TABLE [\fBcreate_table\fR(7)]for details..TP 0.2i\(buThe WITH clause is a PostgreSQLextension; neither storage parameters nor OIDs are in the standard..TP 0.2i\(buThe PostgreSQL concept of tablespaces is notpart of the standard. Hence, the clause TABLESPACEis an extension..PP.SH "SEE ALSO"CREATE TABLE [\fBcreate_table\fR(7)], EXECUTE [\fBexecute\fR(l)], SELECT [\fBselect\fR(l)], SELECT INTO [\fBselect_into\fR(l)], VALUES [\fBvalues\fR(l)]

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?