Home Explore Blog CI



postgresql

4th chunk of `doc/src/sgml/ref/create_table_as.sgml`
d13373f49ecd9a4778612a5ad0ff3fd50057397136afd83f0000000100000e9c
 linkend="sql-execute"><command>EXECUTE</command></link> command that runs a
      prepared <command>SELECT</command>, <command>TABLE</command>, or
      <command>VALUES</command> query.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>WITH [ NO ] DATA</literal></term>
    <listitem>
     <para>
      This clause specifies whether or not the data produced by the query
      should be copied into the new table.  If not, only the table structure
      is copied.  The default is to copy the data.
     </para>
    </listitem>
   </varlistentry>

  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   This command is functionally similar to <xref
   linkend="sql-selectinto"/>, but it is
   preferred since it is less likely to be confused with other uses of
   the <command>SELECT INTO</command> syntax. Furthermore, <command>CREATE
   TABLE AS</command> offers a superset of the functionality offered
   by <command>SELECT INTO</command>.
  </para>
 </refsect1>

 <refsect1>
  <title>Examples</title>

  <para>
   Create a new table <literal>films_recent</literal> consisting of only
   recent entries from the table <literal>films</literal>:

<programlisting>
CREATE TABLE films_recent AS
  SELECT * FROM films WHERE date_prod &gt;= '2002-01-01';
</programlisting>
  </para>

  <para>
   To copy a table completely, the short form using
   the <literal>TABLE</literal> command can also be used:

<programlisting>
CREATE TABLE films2 AS
  TABLE films;
</programlisting>
  </para>

  <para>
   Create a new temporary table <literal>films_recent</literal>, consisting of
   only recent entries from the table <literal>films</literal>, using a
   prepared statement.  The new table will be dropped at commit:

<programlisting>
PREPARE recentfilms(date) AS
  SELECT * FROM films WHERE date_prod &gt; $1;
CREATE TEMP TABLE films_recent ON COMMIT DROP AS
  EXECUTE recentfilms('2002-01-01');
</programlisting></para>
 </refsect1>

 <refsect1>
  <title>Compatibility</title>

  <para>
   <command>CREATE TABLE AS</command> conforms to the <acronym>SQL</acronym>
   standard.  The following are nonstandard extensions:

   <itemizedlist spacing="compact">
    <listitem>
     <para>
      The standard requires parentheses around the subquery clause; in
      <productname>PostgreSQL</productname>, these parentheses are
      optional.
     </para>
    </listitem>

    <listitem>
     <para>
      In the standard, the <literal>WITH [ NO ] DATA</literal> clause
      is required; in PostgreSQL it is optional.
     </para>
    </listitem>

    <listitem>
     <para><productname>PostgreSQL</productname> handles temporary tables in a way
      rather different from the standard; see
      <xref linkend="sql-createtable"/>
      for details.
     </para>
    </listitem>

    <listitem>
     <para>
      The <literal>WITH</literal> clause is a <productname>PostgreSQL</productname>
      extension; storage parameters are not in the standard.
     </para>
    </listitem>

    <listitem>
     <para>
      The <productname>PostgreSQL</productname> concept of tablespaces is not
      part of the standard.  Hence, the clause <literal>TABLESPACE</literal>
      is an extension.
     </para>
    </listitem>
   </itemizedlist></para>
 </refsect1>

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-creatematerializedview"/></member>
   <member><xref linkend="sql-createtable"/></member>
   <member><xref linkend="sql-execute"/></member>
   <member><xref linkend="sql-select"/></member>
   <member><xref linkend="sql-selectinto"/></member>
   <member><xref linkend="sql-values"/></member>
  </simplelist>
 </refsect1>

</refentry>

Title: CREATE TABLE AS - Examples, Compatibility, and See Also
Summary
This section provides examples of using CREATE TABLE AS, including creating a table from a subset of another table's data, copying a table entirely, and using a prepared statement. It also details the command's compatibility with the SQL standard, highlighting PostgreSQL's extensions such as optional parentheses around the subquery, optional WITH [NO] DATA clause, handling of temporary tables, the WITH clause for storage parameters, and the TABLESPACE clause. Finally, it lists related SQL commands.