|
Server : Apache/2.2.2 (Fedora) System : Linux App1.pathumtani.go.th 2.6.20-1.2320.fc5smp #1 SMP Tue Jun 12 19:40:16 EDT 2007 i686 User : apache ( 48) PHP Version : 5.2.9 Disable Function : NONE Directory : /proc/self/root/usr/share/doc/postgresql-8.1.9/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>CREATE TABLE AS</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.1.9 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="CREATE TABLE"
HREF="sql-createtable.html"><LINK
REL="NEXT"
TITLE="CREATE TABLESPACE"
HREF="sql-createtablespace.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-04-20T04:40:08"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.1.9 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createtable.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createtable.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createtablespace.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createtablespace.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-CREATETABLEAS"
></A
>CREATE TABLE AS</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN43908"
></A
><H2
>Name</H2
>CREATE TABLE AS -- define a new table from the results of a query</DIV
><A
NAME="AEN43911"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN43913"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
>
[ (<TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
> [, ...] ) ] [ [ WITH | WITHOUT ] OIDS ]
AS <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN43918"
></A
><H2
>Description</H2
><P
> <TT
CLASS="COMMAND"
>CREATE TABLE AS</TT
> creates a table and fills it
with data computed by a <TT
CLASS="COMMAND"
>SELECT</TT
> command or an
<TT
CLASS="COMMAND"
>EXECUTE</TT
> that runs a prepared
<TT
CLASS="COMMAND"
>SELECT</TT
> command. The table columns have the
names and data types associated with the output columns of the
<TT
CLASS="COMMAND"
>SELECT</TT
> (except that you can override the column
names by giving an explicit list of new column names).
</P
><P
> <TT
CLASS="COMMAND"
>CREATE TABLE AS</TT
> bears some resemblance to
creating a view, but it is really quite different: it creates a new
table and evaluates the query just once to fill the new table
initially. The new table will not track subsequent changes to the
source tables of the query. In contrast, a view re-evaluates its
defining <TT
CLASS="COMMAND"
>SELECT</TT
> statement whenever it is
queried.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN43929"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>GLOBAL</TT
> or <TT
CLASS="LITERAL"
>LOCAL</TT
></DT
><DD
><P
> Ignored for compatibility. Refer to <A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
> for
details.
</P
></DD
></DL
></DIV
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>TEMPORARY</TT
> or <TT
CLASS="LITERAL"
>TEMP</TT
></DT
><DD
><P
> If specified, the table is created as a temporary table.
Refer to <A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
> for details.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>table_name</I
></TT
></DT
><DD
><P
> The name (optionally schema-qualified) of the table to be created.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>column_name</I
></TT
></DT
><DD
><P
> The name of a column in the new table. If column names are not
provided, they are taken from the output column names of the
query. If the table is created from an
<TT
CLASS="COMMAND"
>EXECUTE</TT
> command, a column name list cannot be
specified.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>WITH OIDS</TT
><BR><TT
CLASS="LITERAL"
>WITHOUT OIDS</TT
></DT
><DD
><P
> This optional clause specifies whether the table created by
<TT
CLASS="COMMAND"
>CREATE TABLE AS</TT
> should include OIDs. If
neither form of this clause is specified, the value of the
<A
HREF="runtime-config-compatible.html#GUC-DEFAULT-WITH-OIDS"
>default_with_oids</A
> configuration parameter is
used.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></DT
><DD
><P
> A query statement (that is, a <TT
CLASS="COMMAND"
>SELECT</TT
> command
or an <TT
CLASS="COMMAND"
>EXECUTE</TT
> command that runs a prepared
<TT
CLASS="COMMAND"
>SELECT</TT
> command). Refer to <A
HREF="sql-select.html"
><I
>SELECT</I
></A
> or <A
HREF="sql-execute.html"
><I
>EXECUTE</I
></A
>,
respectively, for a description of the allowed syntax.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN43977"
></A
><H2
>Notes</H2
><P
> This command is functionally similar to <A
HREF="sql-selectinto.html"
><I
>SELECT INTO</I
></A
>, but it is
preferred since it is less likely to be confused with other uses of
the <TT
CLASS="COMMAND"
>SELECT INTO</TT
> syntax. Furthermore, <TT
CLASS="COMMAND"
>CREATE
TABLE AS</TT
> offers a superset of the functionality offered
by <TT
CLASS="COMMAND"
>SELECT INTO</TT
>.
</P
><P
> Prior to <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.0, <TT
CLASS="COMMAND"
>CREATE
TABLE AS</TT
> always included OIDs in the table it
created. As of <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.0,
the <TT
CLASS="COMMAND"
>CREATE TABLE AS</TT
> command allows the user to
explicitly specify whether OIDs should be included. If the
presence of OIDs is not explicitly specified,
the <A
HREF="runtime-config-compatible.html#GUC-DEFAULT-WITH-OIDS"
>default_with_oids</A
> configuration variable is
used. As of <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.1,
this variable is false by default, so the default behavior is not
identical to pre-8.0 releases. Applications that
require OIDs in the table created by <TT
CLASS="COMMAND"
>CREATE TABLE
AS</TT
> should explicitly specify <TT
CLASS="LITERAL"
>WITH OIDS</TT
>
to ensure proper behavior.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN43993"
></A
><H2
>Examples</H2
><P
> Create a new table <TT
CLASS="LITERAL"
>films_recent</TT
> consisting of only
recent entries from the table <TT
CLASS="LITERAL"
>films</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE films_recent AS
SELECT * FROM films WHERE date_prod >= '2002-01-01';</PRE
><P>
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN43999"
></A
><H2
>Compatibility</H2
><P
> <TT
CLASS="COMMAND"
>CREATE TABLE AS</TT
> conforms to the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>
standard, with the following exceptions:
<P
></P
></P><UL
COMPACT="COMPACT"
><LI
><P
> The standard requires parentheses around the subquery clause; in
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, these parentheses are
optional.
</P
></LI
><LI
><P
> The standard defines an <TT
CLASS="LITERAL"
>ON COMMIT</TT
> clause;
this is not currently implemented by <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
</P
></LI
><LI
><P
> The standard defines a <TT
CLASS="LITERAL"
>WITH [ NO ] DATA</TT
> clause;
this is not currently implemented by <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.
The behavior provided by <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> is equivalent
to the standard's <TT
CLASS="LITERAL"
>WITH DATA</TT
> case.
</P
></LI
><LI
><P
> <TT
CLASS="LITERAL"
>WITH/WITHOUT OIDS</TT
> is a <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
extension.
</P
></LI
><LI
><P
> <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> handles temporary tables in a way
rather different from the standard; see
<A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
>
for details.
</P
></LI
></UL
><P>
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN44026"
></A
><H2
>See Also</H2
><A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
>, <A
HREF="sql-execute.html"
><I
>EXECUTE</I
></A
>, <A
HREF="sql-select.html"
><I
>SELECT</I
></A
>, <A
HREF="sql-selectinto.html"
><I
>SELECT INTO</I
></A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-createtable.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-createtablespace.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>CREATE TABLE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>CREATE TABLESPACE</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>