gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

145 líneas
14KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE TABLE AS</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-createtable.html" title="CREATE TABLE" /><link rel="next" href="sql-createtablespace.html" title="CREATE TABLESPACE" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE TABLE AS</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createtable.html" title="CREATE TABLE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createtablespace.html" title="CREATE TABLESPACE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATETABLEAS"><div class="titlepage"></div><a id="id-1.9.3.86.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE TABLE AS</span></h2><p>CREATE TABLE AS — define a new table from the results of a query</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <em class="replaceable"><code>table_name</code></em>
  4. [ (<em class="replaceable"><code>column_name</code></em> [, ...] ) ]
  5. [ USING <em class="replaceable"><code>method</code></em> ]
  6. [ WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] ) | WITHOUT OIDS ]
  7. [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
  8. [ TABLESPACE <em class="replaceable"><code>tablespace_name</code></em> ]
  9. AS <em class="replaceable"><code>query</code></em>
  10. [ WITH [ NO ] DATA ]
  11. </pre></div><div class="refsect1" id="id-1.9.3.86.5"><h2>Description</h2><p>
  12. <code class="command">CREATE TABLE AS</code> creates a table and fills it
  13. with data computed by a <code class="command">SELECT</code> command.
  14. The table columns have the
  15. names and data types associated with the output columns of the
  16. <code class="command">SELECT</code> (except that you can override the column
  17. names by giving an explicit list of new column names).
  18. </p><p>
  19. <code class="command">CREATE TABLE AS</code> bears some resemblance to
  20. creating a view, but it is really quite different: it creates a new
  21. table and evaluates the query just once to fill the new table
  22. initially. The new table will not track subsequent changes to the
  23. source tables of the query. In contrast, a view re-evaluates its
  24. defining <code class="command">SELECT</code> statement whenever it is
  25. queried.
  26. </p></div><div class="refsect1" id="id-1.9.3.86.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">GLOBAL</code> or <code class="literal">LOCAL</code></span></dt><dd><p>
  27. Ignored for compatibility. Use of these keywords is deprecated;
  28. refer to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for details.
  29. </p></dd></dl></div><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">TEMPORARY</code> or <code class="literal">TEMP</code></span></dt><dd><p>
  30. If specified, the table is created as a temporary table.
  31. Refer to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for details.
  32. </p></dd><dt><span class="term"><code class="literal">UNLOGGED</code></span></dt><dd><p>
  33. If specified, the table is created as an unlogged table.
  34. Refer to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for details.
  35. </p></dd><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  36. Do not throw an error if a relation with the same name already exists.
  37. A notice is issued in this case. Refer to <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>
  38. for details.
  39. </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  40. The name (optionally schema-qualified) of the table to be created.
  41. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  42. The name of a column in the new table. If column names are not
  43. provided, they are taken from the output column names of the query.
  44. </p></dd><dt><span class="term"><code class="literal">USING <em class="replaceable"><code>method</code></em></code></span></dt><dd><p>
  45. This optional clause specifies the table access method to use to store
  46. the contents for the new table; the method needs be an access method of
  47. type <code class="literal">TABLE</code>. See <a class="xref" href="tableam.html" title="Chapter 60. Table Access Method Interface Definition">Chapter 60</a> for more
  48. information. If this option is not specified, the default table access
  49. method is chosen for the new table. See <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLE-ACCESS-METHOD">default_table_access_method</a> for more information.
  50. </p></dd><dt><span class="term"><code class="literal">WITH ( <em class="replaceable"><code>storage_parameter</code></em> [= <em class="replaceable"><code>value</code></em>] [, ... ] )</code></span></dt><dd><p>
  51. This clause specifies optional storage parameters for the new table;
  52. see <a class="xref" href="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS" title="Storage Parameters">Storage Parameters</a> for more
  53. information. For backward-compatibility the <code class="literal">WITH</code>
  54. clause for a table can also include <code class="literal">OIDS=FALSE</code> to
  55. specify that rows of the new table should contain no OIDs (object
  56. identifiers), <code class="literal">OIDS=TRUE</code> is not supported anymore.
  57. </p></dd><dt><span class="term"><code class="literal">WITHOUT OIDS</code></span></dt><dd><p>
  58. This is backward-compatible syntax for declaring a table
  59. <code class="literal">WITHOUT OIDS</code>, creating a table <code class="literal">WITH
  60. OIDS</code> is not supported anymore.
  61. </p></dd><dt><span class="term"><code class="literal">ON COMMIT</code></span></dt><dd><p>
  62. The behavior of temporary tables at the end of a transaction
  63. block can be controlled using <code class="literal">ON COMMIT</code>.
  64. The three options are:
  65. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">PRESERVE ROWS</code></span></dt><dd><p>
  66. No special action is taken at the ends of transactions.
  67. This is the default behavior.
  68. </p></dd><dt><span class="term"><code class="literal">DELETE ROWS</code></span></dt><dd><p>
  69. All rows in the temporary table will be deleted at the end
  70. of each transaction block. Essentially, an automatic <a class="xref" href="sql-truncate.html" title="TRUNCATE"><span class="refentrytitle">TRUNCATE</span></a> is done
  71. at each commit.
  72. </p></dd><dt><span class="term"><code class="literal">DROP</code></span></dt><dd><p>
  73. The temporary table will be dropped at the end of the current
  74. transaction block.
  75. </p></dd></dl></div></dd><dt><span class="term"><code class="literal">TABLESPACE <em class="replaceable"><code>tablespace_name</code></em></code></span></dt><dd><p>
  76. The <em class="replaceable"><code>tablespace_name</code></em> is the name
  77. of the tablespace in which the new table is to be created.
  78. If not specified,
  79. <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TABLESPACE">default_tablespace</a> is consulted, or
  80. <a class="xref" href="runtime-config-client.html#GUC-TEMP-TABLESPACES">temp_tablespaces</a> if the table is temporary.
  81. </p></dd><dt><span class="term"><em class="replaceable"><code>query</code></em></span></dt><dd><p>
  82. A <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>, <a class="link" href="sql-select.html#SQL-TABLE" title="TABLE Command">TABLE</a>, or <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a>
  83. command, or an <a class="xref" href="sql-execute.html" title="EXECUTE"><span class="refentrytitle">EXECUTE</span></a> command that runs a
  84. prepared <code class="command">SELECT</code>, <code class="command">TABLE</code>, or
  85. <code class="command">VALUES</code> query.
  86. </p></dd><dt><span class="term"><code class="literal">WITH [ NO ] DATA</code></span></dt><dd><p>
  87. This clause specifies whether or not the data produced by the query
  88. should be copied into the new table. If not, only the table structure
  89. is copied. The default is to copy the data.
  90. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.86.7"><h2>Notes</h2><p>
  91. This command is functionally similar to <a class="xref" href="sql-selectinto.html" title="SELECT INTO"><span class="refentrytitle">SELECT INTO</span></a>, but it is
  92. preferred since it is less likely to be confused with other uses of
  93. the <code class="command">SELECT INTO</code> syntax. Furthermore, <code class="command">CREATE
  94. TABLE AS</code> offers a superset of the functionality offered
  95. by <code class="command">SELECT INTO</code>.
  96. </p></div><div class="refsect1" id="id-1.9.3.86.8"><h2>Examples</h2><p>
  97. Create a new table <code class="literal">films_recent</code> consisting of only
  98. recent entries from the table <code class="literal">films</code>:
  99. </p><pre class="programlisting">
  100. CREATE TABLE films_recent AS
  101. SELECT * FROM films WHERE date_prod &gt;= '2002-01-01';
  102. </pre><p>
  103. </p><p>
  104. To copy a table completely, the short form using
  105. the <code class="literal">TABLE</code> command can also be used:
  106. </p><pre class="programlisting">
  107. CREATE TABLE films2 AS
  108. TABLE films;
  109. </pre><p>
  110. </p><p>
  111. Create a new temporary table <code class="literal">films_recent</code>, consisting of
  112. only recent entries from the table <code class="literal">films</code>, using a
  113. prepared statement. The new table will be dropped at commit:
  114. </p><pre class="programlisting">
  115. PREPARE recentfilms(date) AS
  116. SELECT * FROM films WHERE date_prod &gt; $1;
  117. CREATE TEMP TABLE films_recent ON COMMIT DROP AS
  118. EXECUTE recentfilms('2002-01-01');
  119. </pre></div><div class="refsect1" id="id-1.9.3.86.9"><h2>Compatibility</h2><p>
  120. <code class="command">CREATE TABLE AS</code> conforms to the <acronym class="acronym">SQL</acronym>
  121. standard. The following are nonstandard extensions:
  122. </p><div class="itemizedlist"><ul class="itemizedlist compact" style="list-style-type: disc; "><li class="listitem"><p>
  123. The standard requires parentheses around the subquery clause; in
  124. <span class="productname">PostgreSQL</span>, these parentheses are
  125. optional.
  126. </p></li><li class="listitem"><p>
  127. In the standard, the <code class="literal">WITH [ NO ] DATA</code> clause
  128. is required; in PostgreSQL it is optional.
  129. </p></li><li class="listitem"><p><span class="productname">PostgreSQL</span> handles temporary tables in a way
  130. rather different from the standard; see
  131. <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>
  132. for details.
  133. </p></li><li class="listitem"><p>
  134. The <code class="literal">WITH</code> clause is a <span class="productname">PostgreSQL</span>
  135. extension; storage parameters are not in the standard.
  136. </p></li><li class="listitem"><p>
  137. The <span class="productname">PostgreSQL</span> concept of tablespaces is not
  138. part of the standard. Hence, the clause <code class="literal">TABLESPACE</code>
  139. is an extension.
  140. </p></li></ul></div></div><div class="refsect1" id="id-1.9.3.86.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-creatematerializedview.html" title="CREATE MATERIALIZED VIEW"><span class="refentrytitle">CREATE MATERIALIZED VIEW</span></a>, <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>, <a class="xref" href="sql-execute.html" title="EXECUTE"><span class="refentrytitle">EXECUTE</span></a>, <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>, <a class="xref" href="sql-selectinto.html" title="SELECT INTO"><span class="refentrytitle">SELECT INTO</span></a>, <a class="xref" href="sql-values.html" title="VALUES"><span class="refentrytitle">VALUES</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createtable.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createtablespace.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE TABLE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE TABLESPACE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1