3 PostgreSQL documentation
6 <refentry id=
"SQL-CREATETABLEAS">
8 <refentrytitle id=
"sql-createtableas-title">CREATE TABLE AS
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>CREATE TABLE AS
</refname>
15 <refpurpose>define a new table from the results of a query
</refpurpose>
18 <indexterm zone=
"sql-createtableas">
19 <primary>CREATE TABLE AS
</primary>
24 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE
<replaceable>table_name
</replaceable>
25 [ (
<replaceable>column_name
</replaceable> [, ...] ) ]
26 [ WITH (
<replaceable class=
"PARAMETER">storage_parameter
</replaceable> [=
<replaceable class=
"PARAMETER">value
</replaceable>] [, ... ] ) | WITH OIDS | WITHOUT OIDS ]
27 [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
28 [ TABLESPACE
<replaceable class=
"PARAMETER">tablespace
</replaceable> ]
29 AS
<replaceable>query
</replaceable>
35 <title>Description
</title>
38 <command>CREATE TABLE AS
</command> creates a table and fills it
39 with data computed by a
<command>SELECT
</command> command.
40 The table columns have the
41 names and data types associated with the output columns of the
42 <command>SELECT
</command> (except that you can override the column
43 names by giving an explicit list of new column names).
47 <command>CREATE TABLE AS
</command> bears some resemblance to
48 creating a view, but it is really quite different: it creates a new
49 table and evaluates the query just once to fill the new table
50 initially. The new table will not track subsequent changes to the
51 source tables of the query. In contrast, a view re-evaluates its
52 defining
<command>SELECT
</command> statement whenever it is
58 <title>Parameters
</title>
62 <term><literal>GLOBAL
</literal> or
<literal>LOCAL
</literal></term>
65 Ignored for compatibility. Refer to
<xref
66 linkend=
"sql-createtable" endterm=
"sql-createtable-title"> for
75 <term><literal>TEMPORARY<
/> or
<literal>TEMP<
/></term>
78 If specified, the table is created as a temporary table.
79 Refer to
<xref linkend=
"sql-createtable" endterm=
"sql-createtable-title"> for details.
85 <term><replaceable>table_name
</replaceable></term>
88 The name (optionally schema-qualified) of the table to be created.
94 <term><replaceable>column_name
</replaceable></term>
97 The name of a column in the new table. If column names are not
98 provided, they are taken from the output column names of the
99 query. If the table is created from an
100 <command>EXECUTE
</command> command, a column name list cannot be
107 <term><literal>WITH (
<replaceable class=
"PARAMETER">storage_parameter
</replaceable> [=
<replaceable class=
"PARAMETER">value
</replaceable>] [, ... ] )
</literal></term>
110 This clause specifies optional storage parameters for the new table;
111 see
<xref linkend=
"sql-createtable-storage-parameters"
112 endterm=
"sql-createtable-storage-parameters-title"> for more
113 information. The
<literal>WITH<
/> clause
114 can also include
<literal>OIDS=TRUE<
/> (or just
<literal>OIDS<
/>)
115 to specify that rows of the new table
116 should have OIDs (object identifiers) assigned to them, or
117 <literal>OIDS=FALSE<
/> to specify that the rows should not have OIDs.
118 See
<xref linkend=
"sql-createtable"
119 endterm=
"sql-createtable-title"> for more information.
125 <term><literal>WITH OIDS<
/></term>
126 <term><literal>WITHOUT OIDS<
/></term>
129 These are obsolescent syntaxes equivalent to
<literal>WITH (OIDS)<
/>
130 and
<literal>WITH (OIDS=FALSE)<
/>, respectively. If you wish to give
131 both an
<literal>OIDS<
/> setting and storage parameters, you must use
132 the
<literal>WITH ( ... )<
/> syntax; see above.
138 <term><literal>ON COMMIT
</literal></term>
141 The behavior of temporary tables at the end of a transaction
142 block can be controlled using
<literal>ON COMMIT
</literal>.
143 The three options are:
147 <term><literal>PRESERVE ROWS
</literal></term>
150 No special action is taken at the ends of transactions.
151 This is the default behavior.
157 <term><literal>DELETE ROWS
</literal></term>
160 All rows in the temporary table will be deleted at the end
161 of each transaction block. Essentially, an automatic
<xref
162 linkend=
"sql-truncate" endterm=
"sql-truncate-title"> is done
169 <term><literal>DROP
</literal></term>
172 The temporary table will be dropped at the end of the current
183 <term><literal>TABLESPACE
<replaceable class=
"PARAMETER">tablespace
</replaceable></literal></term>
186 The
<replaceable class=
"PARAMETER">tablespace
</replaceable> is the name
187 of the tablespace in which the new table is to be created.
189 <xref linkend=
"guc-default-tablespace"> is consulted, or
190 <xref linkend=
"guc-temp-tablespaces"> if the table is temporary.
196 <term><replaceable>query
</replaceable></term>
199 A
<xref linkend=
"sql-select"
200 endterm=
"sql-select-title">,
<link linkend=
"sql-table">TABLE
</link>,
202 <xref linkend=
"sql-values" endterm=
"sql-values-title"> command,
203 or an
<xref linkend=
"sql-execute" endterm=
"sql-execute-title"> command
204 that runs a prepared
<command>SELECT<
/>,
<command>TABLE<
/>, or
<command>VALUES<
/> query.
210 <term><literal>WITH [ NO ] DATA<
/></term>
213 This clause specifies whether or not the data produced by the query
214 should be copied into the new table. If not, only the table structure
215 is copied. The default is to copy the data.
227 This command is functionally similar to
<xref
228 linkend=
"sql-selectinto" endterm=
"sql-selectinto-title">, but it is
229 preferred since it is less likely to be confused with other uses of
230 the
<command>SELECT INTO<
/> syntax. Furthermore,
<command>CREATE
231 TABLE AS
</command> offers a superset of the functionality offered
232 by
<command>SELECT INTO
</command>.
236 Prior to
<productname>PostgreSQL
</productname> 8.0,
<command>CREATE
237 TABLE AS
</command> always included OIDs in the table it
238 created. As of
<productname>PostgreSQL
</productname> 8.0,
239 the
<command>CREATE TABLE AS
</command> command allows the user to
240 explicitly specify whether OIDs should be included. If the
241 presence of OIDs is not explicitly specified,
242 the
<xref linkend=
"guc-default-with-oids"> configuration variable is
243 used. As of
<productname>PostgreSQL
</productname> 8.1,
244 this variable is false by default, so the default behavior is not
245 identical to pre-
8.0 releases. Applications that
246 require OIDs in the table created by
<command>CREATE TABLE
247 AS
</command> should explicitly specify
<literal>WITH (OIDS)
</literal>
248 to ensure proper behavior.
253 <title>Examples
</title>
256 Create a new table
<literal>films_recent
</literal> consisting of only
257 recent entries from the table
<literal>films
</literal>:
260 CREATE TABLE films_recent AS
261 SELECT * FROM films WHERE date_prod
>= '
2002-
01-
01';
266 To copy a table completely, the short form using
267 the
<literal>TABLE
</literal> command can also be used:
270 CREATE TABLE films2 AS
276 Create a new temporary table
<literal>films_recent
</literal>, consisting of
277 only recent entries from the table
<literal>films
</literal>, using a
278 prepared statement. The new table has OIDs and will be dropped at commit:
281 PREPARE recentfilms(date) AS
282 SELECT * FROM films WHERE date_prod
> $
1;
283 CREATE TEMP TABLE films_recent WITH (OIDS) ON COMMIT DROP AS
284 EXECUTE recentfilms('
2002-
01-
01');
290 <title>Compatibility
</title>
293 <command>CREATE TABLE AS
</command> conforms to the
<acronym>SQL
</acronym>
294 standard. The following are nonstandard extensions:
296 <itemizedlist spacing=
"compact">
299 The standard requires parentheses around the subquery clause; in
300 <productname>PostgreSQL
</productname>, these parentheses are
307 In the standard, the
<literal>WITH [ NO ] DATA
</literal> clause
308 is required; in PostgreSQL it is optional.
314 <productname>PostgreSQL<
/> handles temporary tables in a way
315 rather different from the standard; see
316 <xref linkend=
"sql-createtable" endterm=
"sql-createtable-title">
323 The
<literal>WITH<
/> clause is a
<productname>PostgreSQL
</productname>
324 extension; neither storage parameters nor OIDs are in the standard.
330 The
<productname>PostgreSQL
</productname> concept of tablespaces is not
331 part of the standard. Hence, the clause
<literal>TABLESPACE
</literal>
340 <title>See Also
</title>
342 <simplelist type=
"inline">
343 <member><xref linkend=
"sql-createtable" endterm=
"sql-createtable-title"></member>
344 <member><xref linkend=
"sql-execute" endterm=
"sql-execute-title"></member>
345 <member><xref linkend=
"sql-select" endterm=
"sql-select-title"></member>
346 <member><xref linkend=
"sql-selectinto" endterm=
"sql-selectinto-title"></member>
347 <member><xref linkend=
"sql-values" endterm=
"sql-values-title"></member>