gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
18KB

  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 FOREIGN TABLE</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-createforeigndatawrapper.html" title="CREATE FOREIGN DATA WRAPPER" /><link rel="next" href="sql-createfunction.html" title="CREATE FUNCTION" /></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 FOREIGN TABLE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createforeigndatawrapper.html" title="CREATE FOREIGN DATA WRAPPER">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-createfunction.html" title="CREATE FUNCTION">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEFOREIGNTABLE"><div class="titlepage"></div><a id="id-1.9.3.66.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE FOREIGN TABLE</span></h2><p>CREATE FOREIGN TABLE — define a new foreign table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE FOREIGN TABLE [ IF NOT EXISTS ] <em class="replaceable"><code>table_name</code></em> ( [
  4. { <em class="replaceable"><code>column_name</code></em> <em class="replaceable"><code>data_type</code></em> [ OPTIONS ( <em class="replaceable"><code>option</code></em> '<em class="replaceable"><code>value</code></em>' [, ... ] ) ] [ COLLATE <em class="replaceable"><code>collation</code></em> ] [ <em class="replaceable"><code>column_constraint</code></em> [ ... ] ]
  5. | <em class="replaceable"><code>table_constraint</code></em> }
  6. [, ... ]
  7. ] )
  8. [ INHERITS ( <em class="replaceable"><code>parent_table</code></em> [, ... ] ) ]
  9. SERVER <em class="replaceable"><code>server_name</code></em>
  10. [ OPTIONS ( <em class="replaceable"><code>option</code></em> '<em class="replaceable"><code>value</code></em>' [, ... ] ) ]
  11. CREATE FOREIGN TABLE [ IF NOT EXISTS ] <em class="replaceable"><code>table_name</code></em>
  12. PARTITION OF <em class="replaceable"><code>parent_table</code></em> [ (
  13. { <em class="replaceable"><code>column_name</code></em> [ WITH OPTIONS ] [ <em class="replaceable"><code>column_constraint</code></em> [ ... ] ]
  14. | <em class="replaceable"><code>table_constraint</code></em> }
  15. [, ... ]
  16. ) ] <em class="replaceable"><code>partition_bound_spec</code></em>
  17. SERVER <em class="replaceable"><code>server_name</code></em>
  18. [ OPTIONS ( <em class="replaceable"><code>option</code></em> '<em class="replaceable"><code>value</code></em>' [, ... ] ) ]
  19. <span class="phrase">where <em class="replaceable"><code>column_constraint</code></em> is:</span>
  20. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  21. { NOT NULL |
  22. NULL |
  23. CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] |
  24. DEFAULT <em class="replaceable"><code>default_expr</code></em> |
  25. GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) STORED }
  26. <span class="phrase">and <em class="replaceable"><code>table_constraint</code></em> is:</span>
  27. [ CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> ]
  28. CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ]
  29. </pre></div><div class="refsect1" id="SQL-CREATEFOREIGNTABLE-DESCRIPTION"><h2>Description</h2><p>
  30. <code class="command">CREATE FOREIGN TABLE</code> creates a new foreign table
  31. in the current database. The table will be owned by the user issuing the
  32. command.
  33. </p><p>
  34. If a schema name is given (for example, <code class="literal">CREATE FOREIGN TABLE
  35. myschema.mytable ...</code>) then the table is created in the specified
  36. schema. Otherwise it is created in the current schema.
  37. The name of the foreign table must be
  38. distinct from the name of any other foreign table, table, sequence, index,
  39. view, or materialized view in the same schema.
  40. </p><p>
  41. <code class="command">CREATE FOREIGN TABLE</code> also automatically creates a data
  42. type that represents the composite type corresponding to one row of
  43. the foreign table. Therefore, foreign tables cannot have the same
  44. name as any existing data type in the same schema.
  45. </p><p>
  46. If <code class="literal">PARTITION OF</code> clause is specified then the table is
  47. created as a partition of <code class="literal">parent_table</code> with specified
  48. bounds.
  49. </p><p>
  50. To be able to create a foreign table, you must have <code class="literal">USAGE</code>
  51. privilege on the foreign server, as well as <code class="literal">USAGE</code>
  52. privilege on all column types used in the table.
  53. </p></div><div class="refsect1" id="id-1.9.3.66.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  54. Do not throw an error if a relation with the same name already exists.
  55. A notice is issued in this case. Note that there is no guarantee that
  56. the existing relation is anything like the one that would have been
  57. created.
  58. </p></dd><dt><span class="term"><em class="replaceable"><code>table_name</code></em></span></dt><dd><p>
  59. The name (optionally schema-qualified) of the table to be created.
  60. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  61. The name of a column to be created in the new table.
  62. </p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p>
  63. The data type of the column. This can include array
  64. specifiers. For more information on the data types supported by
  65. <span class="productname">PostgreSQL</span>, refer to <a class="xref" href="datatype.html" title="Chapter 8. Data Types">Chapter 8</a>.
  66. </p></dd><dt><span class="term"><code class="literal">COLLATE <em class="replaceable"><code>collation</code></em></code></span></dt><dd><p>
  67. The <code class="literal">COLLATE</code> clause assigns a collation to
  68. the column (which must be of a collatable data type).
  69. If not specified, the column data type's default collation is used.
  70. </p></dd><dt><span class="term"><code class="literal">INHERITS ( <em class="replaceable"><code>parent_table</code></em> [, ... ] )</code></span></dt><dd><p>
  71. The optional <code class="literal">INHERITS</code> clause specifies a list of
  72. tables from which the new foreign table automatically inherits
  73. all columns. Parent tables can be plain tables or foreign tables.
  74. See the similar form of
  75. <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for more details.
  76. </p></dd><dt><span class="term"><code class="literal">PARTITION OF <em class="replaceable"><code>parent_table</code></em> FOR VALUES <em class="replaceable"><code>partition_bound_spec</code></em></code></span></dt><dd><p>
  77. This form can be used to create the foreign table as partition of
  78. the given parent table with specified partition bound values.
  79. See the similar form of
  80. <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a> for more details.
  81. Note that it is currently not allowed to create the foreign table as a
  82. partition of the parent table if there are <code class="literal">UNIQUE</code>
  83. indexes on the parent table. (See also
  84. <a class="link" href="sql-altertable.html" title="ALTER TABLE"><code class="command">ALTER TABLE ATTACH PARTITION</code></a>.)
  85. </p></dd><dt><span class="term"><code class="literal">CONSTRAINT <em class="replaceable"><code>constraint_name</code></em></code></span></dt><dd><p>
  86. An optional name for a column or table constraint. If the
  87. constraint is violated, the constraint name is present in error messages,
  88. so constraint names like <code class="literal">col must be positive</code> can be used
  89. to communicate helpful constraint information to client applications.
  90. (Double-quotes are needed to specify constraint names that contain spaces.)
  91. If a constraint name is not specified, the system generates a name.
  92. </p></dd><dt><span class="term"><code class="literal">NOT NULL</code></span></dt><dd><p>
  93. The column is not allowed to contain null values.
  94. </p></dd><dt><span class="term"><code class="literal">NULL</code></span></dt><dd><p>
  95. The column is allowed to contain null values. This is the default.
  96. </p><p>
  97. This clause is only provided for compatibility with
  98. non-standard SQL databases. Its use is discouraged in new
  99. applications.
  100. </p></dd><dt><span class="term"><code class="literal">CHECK ( <em class="replaceable"><code>expression</code></em> ) [ NO INHERIT ] </code></span></dt><dd><p>
  101. The <code class="literal">CHECK</code> clause specifies an expression producing a
  102. Boolean result which each row in the foreign table is expected
  103. to satisfy; that is, the expression should produce TRUE or UNKNOWN,
  104. never FALSE, for all rows in the foreign table.
  105. A check constraint specified as a column constraint should
  106. reference that column's value only, while an expression
  107. appearing in a table constraint can reference multiple columns.
  108. </p><p>
  109. Currently, <code class="literal">CHECK</code> expressions cannot contain
  110. subqueries nor refer to variables other than columns of the
  111. current row. The system column <code class="literal">tableoid</code>
  112. may be referenced, but not any other system column.
  113. </p><p>
  114. A constraint marked with <code class="literal">NO INHERIT</code> will not propagate to
  115. child tables.
  116. </p></dd><dt><span class="term"><code class="literal">DEFAULT
  117. <em class="replaceable"><code>default_expr</code></em></code></span></dt><dd><p>
  118. The <code class="literal">DEFAULT</code> clause assigns a default data value for
  119. the column whose column definition it appears within. The value
  120. is any variable-free expression (subqueries and cross-references
  121. to other columns in the current table are not allowed). The
  122. data type of the default expression must match the data type of the
  123. column.
  124. </p><p>
  125. The default expression will be used in any insert operation that
  126. does not specify a value for the column. If there is no default
  127. for a column, then the default is null.
  128. </p></dd><dt><span class="term"><code class="literal">GENERATED ALWAYS AS ( <em class="replaceable"><code>generation_expr</code></em> ) STORED</code><a id="id-1.9.3.66.6.2.13.1.2" class="indexterm"></a></span></dt><dd><p>
  129. This clause creates the column as a <em class="firstterm">generated
  130. column</em>. The column cannot be written to, and when read the
  131. result of the specified expression will be returned.
  132. </p><p>
  133. The keyword <code class="literal">STORED</code> is required to signify that the
  134. column will be computed on write. (The computed value will be presented
  135. to the foreign-data wrapper for storage and must be returned on
  136. reading.)
  137. </p><p>
  138. The generation expression can refer to other columns in the table, but
  139. not other generated columns. Any functions and operators used must be
  140. immutable. References to other tables are not allowed.
  141. </p></dd><dt><span class="term"><em class="replaceable"><code>server_name</code></em></span></dt><dd><p>
  142. The name of an existing foreign server to use for the foreign table.
  143. For details on defining a server, see <a class="xref" href="sql-createserver.html" title="CREATE SERVER"><span class="refentrytitle">CREATE SERVER</span></a>.
  144. </p></dd><dt><span class="term"><code class="literal">OPTIONS ( <em class="replaceable"><code>option</code></em> '<em class="replaceable"><code>value</code></em>' [, ...] )</code></span></dt><dd><p>
  145. Options to be associated with the new foreign table or one of its
  146. columns.
  147. The allowed option names and values are specific to each foreign
  148. data wrapper and are validated using the foreign-data wrapper's
  149. validator function. Duplicate option names are not allowed (although
  150. it's OK for a table option and a column option to have the same name).
  151. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.66.7"><h2>Notes</h2><p>
  152. Constraints on foreign tables (such as <code class="literal">CHECK</code>
  153. or <code class="literal">NOT NULL</code> clauses) are not enforced by the
  154. core <span class="productname">PostgreSQL</span> system, and most foreign data wrappers
  155. do not attempt to enforce them either; that is, the constraint is
  156. simply assumed to hold true. There would be little point in such
  157. enforcement since it would only apply to rows inserted or updated via
  158. the foreign table, and not to rows modified by other means, such as
  159. directly on the remote server. Instead, a constraint attached to a
  160. foreign table should represent a constraint that is being enforced by
  161. the remote server.
  162. </p><p>
  163. Some special-purpose foreign data wrappers might be the only access
  164. mechanism for the data they access, and in that case it might be
  165. appropriate for the foreign data wrapper itself to perform constraint
  166. enforcement. But you should not assume that a wrapper does that
  167. unless its documentation says so.
  168. </p><p>
  169. Although <span class="productname">PostgreSQL</span> does not attempt to enforce
  170. constraints on foreign tables, it does assume that they are correct
  171. for purposes of query optimization. If there are rows visible in the
  172. foreign table that do not satisfy a declared constraint, queries on
  173. the table might produce incorrect answers. It is the user's
  174. responsibility to ensure that the constraint definition matches
  175. reality.
  176. </p><p>
  177. Similar considerations apply to generated columns. Stored generated
  178. columns are computed on insert or update on the local
  179. <span class="productname">PostgreSQL</span> server and handed to the
  180. foreign-data wrapper for writing out to the foreign data store, but it is
  181. not enforced that a query of the foreign table returns values for stored
  182. generated columns that are consistent with the generation expression.
  183. Again, this might result in incorrect query results.
  184. </p><p>
  185. While rows can be moved from local partitions to a foreign-table partition
  186. (provided the foreign data wrapper supports tuple routing), they cannot be
  187. moved from a foreign-table partition to another partition.
  188. </p></div><div class="refsect1" id="SQL-CREATEFOREIGNTABLE-EXAMPLES"><h2>Examples</h2><p>
  189. Create foreign table <code class="structname">films</code>, which will be accessed through
  190. the server <code class="structname">film_server</code>:
  191. </p><pre class="programlisting">
  192. CREATE FOREIGN TABLE films (
  193. code char(5) NOT NULL,
  194. title varchar(40) NOT NULL,
  195. did integer NOT NULL,
  196. date_prod date,
  197. kind varchar(10),
  198. len interval hour to minute
  199. )
  200. SERVER film_server;
  201. </pre><p>
  202. Create foreign table <code class="structname">measurement_y2016m07</code>, which will be
  203. accessed through the server <code class="structname">server_07</code>, as a partition
  204. of the range partitioned table <code class="structname">measurement</code>:
  205. </p><pre class="programlisting">
  206. CREATE FOREIGN TABLE measurement_y2016m07
  207. PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
  208. SERVER server_07;
  209. </pre></div><div class="refsect1" id="SQL-CREATEFOREIGNTABLE-COMPATIBILITY"><h2>Compatibility</h2><p>
  210. The <code class="command">CREATE FOREIGN TABLE</code> command largely conforms to the
  211. <acronym class="acronym">SQL</acronym> standard; however, much as with
  212. <a class="link" href="sql-createtable.html" title="CREATE TABLE"><code class="command">CREATE TABLE</code></a>,
  213. <code class="literal">NULL</code> constraints and zero-column foreign tables are permitted.
  214. The ability to specify column default values is also
  215. a <span class="productname">PostgreSQL</span> extension. Table inheritance, in the form
  216. defined by <span class="productname">PostgreSQL</span>, is nonstandard.
  217. </p></div><div class="refsect1" id="id-1.9.3.66.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterforeigntable.html" title="ALTER FOREIGN TABLE"><span class="refentrytitle">ALTER FOREIGN TABLE</span></a>, <a class="xref" href="sql-dropforeigntable.html" title="DROP FOREIGN TABLE"><span class="refentrytitle">DROP FOREIGN TABLE</span></a>, <a class="xref" href="sql-createtable.html" title="CREATE TABLE"><span class="refentrytitle">CREATE TABLE</span></a>, <a class="xref" href="sql-createserver.html" title="CREATE SERVER"><span class="refentrytitle">CREATE SERVER</span></a>, <a class="xref" href="sql-importforeignschema.html" title="IMPORT FOREIGN SCHEMA"><span class="refentrytitle">IMPORT FOREIGN SCHEMA</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-createforeigndatawrapper.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-createfunction.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE FOREIGN DATA WRAPPER </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE FUNCTION</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1