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.

236 lines
21KB

  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>ALTER 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-alterforeigndatawrapper.html" title="ALTER FOREIGN DATA WRAPPER" /><link rel="next" href="sql-alterfunction.html" title="ALTER 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">ALTER FOREIGN TABLE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-alterforeigndatawrapper.html" title="ALTER 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-alterfunction.html" title="ALTER FUNCTION">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-ALTERFOREIGNTABLE"><div class="titlepage"></div><a id="id-1.9.3.13.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER FOREIGN TABLE</span></h2><p>ALTER FOREIGN TABLE — change the definition of a foreign table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. ALTER FOREIGN TABLE [ IF EXISTS ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ]
  4. <em class="replaceable"><code>action</code></em> [, ... ]
  5. ALTER FOREIGN TABLE [ IF EXISTS ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ]
  6. RENAME [ COLUMN ] <em class="replaceable"><code>column_name</code></em> TO <em class="replaceable"><code>new_column_name</code></em>
  7. ALTER FOREIGN TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
  8. RENAME TO <em class="replaceable"><code>new_name</code></em>
  9. ALTER FOREIGN TABLE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
  10. SET SCHEMA <em class="replaceable"><code>new_schema</code></em>
  11. <span class="phrase">where <em class="replaceable"><code>action</code></em> is one of:</span>
  12. ADD [ COLUMN ] <em class="replaceable"><code>column_name</code></em> <em class="replaceable"><code>data_type</code></em> [ COLLATE <em class="replaceable"><code>collation</code></em> ] [ <em class="replaceable"><code>column_constraint</code></em> [ ... ] ]
  13. DROP [ COLUMN ] [ IF EXISTS ] <em class="replaceable"><code>column_name</code></em> [ RESTRICT | CASCADE ]
  14. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> [ SET DATA ] TYPE <em class="replaceable"><code>data_type</code></em> [ COLLATE <em class="replaceable"><code>collation</code></em> ]
  15. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET DEFAULT <em class="replaceable"><code>expression</code></em>
  16. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> DROP DEFAULT
  17. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> { SET | DROP } NOT NULL
  18. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STATISTICS <em class="replaceable"><code>integer</code></em>
  19. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET ( <em class="replaceable"><code>attribute_option</code></em> = <em class="replaceable"><code>value</code></em> [, ... ] )
  20. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> RESET ( <em class="replaceable"><code>attribute_option</code></em> [, ... ] )
  21. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
  22. ALTER [ COLUMN ] <em class="replaceable"><code>column_name</code></em> OPTIONS ( [ ADD | SET | DROP ] <em class="replaceable"><code>option</code></em> ['<em class="replaceable"><code>value</code></em>'] [, ... ])
  23. ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]
  24. VALIDATE CONSTRAINT <em class="replaceable"><code>constraint_name</code></em>
  25. DROP CONSTRAINT [ IF EXISTS ] <em class="replaceable"><code>constraint_name</code></em> [ RESTRICT | CASCADE ]
  26. DISABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]
  27. ENABLE TRIGGER [ <em class="replaceable"><code>trigger_name</code></em> | ALL | USER ]
  28. ENABLE REPLICA TRIGGER <em class="replaceable"><code>trigger_name</code></em>
  29. ENABLE ALWAYS TRIGGER <em class="replaceable"><code>trigger_name</code></em>
  30. SET WITHOUT OIDS
  31. INHERIT <em class="replaceable"><code>parent_table</code></em>
  32. NO INHERIT <em class="replaceable"><code>parent_table</code></em>
  33. OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_USER | SESSION_USER }
  34. OPTIONS ( [ ADD | SET | DROP ] <em class="replaceable"><code>option</code></em> ['<em class="replaceable"><code>value</code></em>'] [, ... ])
  35. </pre></div><div class="refsect1" id="id-1.9.3.13.5"><h2>Description</h2><p>
  36. <code class="command">ALTER FOREIGN TABLE</code> changes the definition of an
  37. existing foreign table. There are several subforms:
  38. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">ADD COLUMN</code></span></dt><dd><p>
  39. This form adds a new column to the foreign table, using the same syntax as
  40. <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.
  41. Unlike the case when adding a column to a regular table, nothing happens
  42. to the underlying storage: this action simply declares that
  43. some new column is now accessible through the foreign table.
  44. </p></dd><dt><span class="term"><code class="literal">DROP COLUMN [ IF EXISTS ]</code></span></dt><dd><p>
  45. This form drops a column from a foreign table.
  46. You will need to say <code class="literal">CASCADE</code> if
  47. anything outside the table depends on the column; for example,
  48. views.
  49. If <code class="literal">IF EXISTS</code> is specified and the column
  50. does not exist, no error is thrown. In this case a notice
  51. is issued instead.
  52. </p></dd><dt><span class="term"><code class="literal">SET DATA TYPE</code></span></dt><dd><p>
  53. This form changes the type of a column of a foreign table.
  54. Again, this has no effect on any underlying storage: this action simply
  55. changes the type that <span class="productname">PostgreSQL</span> believes the column to
  56. have.
  57. </p></dd><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP DEFAULT</code></span></dt><dd><p>
  58. These forms set or remove the default value for a column.
  59. Default values only apply in subsequent <code class="command">INSERT</code>
  60. or <code class="command">UPDATE</code> commands; they do not cause rows already in the
  61. table to change.
  62. </p></dd><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP NOT NULL</code></span></dt><dd><p>
  63. Mark a column as allowing, or not allowing, null values.
  64. </p></dd><dt><span class="term"><code class="literal">SET STATISTICS</code></span></dt><dd><p>
  65. This form
  66. sets the per-column statistics-gathering target for subsequent
  67. <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> operations.
  68. See the similar form of <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>
  69. for more details.
  70. </p></dd><dt><span class="term"><code class="literal">SET ( <em class="replaceable"><code>attribute_option</code></em> = <em class="replaceable"><code>value</code></em> [, ... ] )</code><br /></span><span class="term"><code class="literal">RESET ( <em class="replaceable"><code>attribute_option</code></em> [, ... ] )</code></span></dt><dd><p>
  71. This form sets or resets per-attribute options.
  72. See the similar form of <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>
  73. for more details.
  74. </p></dd><dt><span class="term">
  75. <code class="literal">SET STORAGE</code>
  76. </span></dt><dd><p>
  77. This form sets the storage mode for a column.
  78. See the similar form of <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>
  79. for more details.
  80. Note that the storage mode has no effect unless the table's
  81. foreign-data wrapper chooses to pay attention to it.
  82. </p></dd><dt><span class="term"><code class="literal">ADD <em class="replaceable"><code>table_constraint</code></em> [ NOT VALID ]</code></span></dt><dd><p>
  83. This form adds a new constraint to a foreign table, using the same
  84. syntax as <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.
  85. Currently only <code class="literal">CHECK</code> constraints are supported.
  86. </p><p>
  87. Unlike the case when adding a constraint to a regular table, nothing is
  88. done to verify the constraint is correct; rather, this action simply
  89. declares that some new condition should be assumed to hold for all rows
  90. in the foreign table. (See the discussion
  91. in <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>.)
  92. If the constraint is marked <code class="literal">NOT VALID</code>, then it isn't
  93. assumed to hold, but is only recorded for possible future use.
  94. </p></dd><dt><span class="term"><code class="literal">VALIDATE CONSTRAINT</code></span></dt><dd><p>
  95. This form marks as valid a constraint that was previously marked
  96. as <code class="literal">NOT VALID</code>. No action is taken to verify the
  97. constraint, but future queries will assume that it holds.
  98. </p></dd><dt><span class="term"><code class="literal">DROP CONSTRAINT [ IF EXISTS ]</code></span></dt><dd><p>
  99. This form drops the specified constraint on a foreign table.
  100. If <code class="literal">IF EXISTS</code> is specified and the constraint
  101. does not exist, no error is thrown.
  102. In this case a notice is issued instead.
  103. </p></dd><dt><span class="term"><code class="literal">DISABLE</code>/<code class="literal">ENABLE [ REPLICA | ALWAYS ] TRIGGER</code></span></dt><dd><p>
  104. These forms configure the firing of trigger(s) belonging to the foreign
  105. table. See the similar form of <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a> for more
  106. details.
  107. </p></dd><dt><span class="term"><code class="literal">SET WITHOUT OIDS</code></span></dt><dd><p>
  108. Backward compatibility syntax for removing the <code class="literal">oid</code>
  109. system column. As <code class="literal">oid</code> system columns cannot be added
  110. anymore, this never has an effect.
  111. </p></dd><dt><span class="term"><code class="literal">INHERIT <em class="replaceable"><code>parent_table</code></em></code></span></dt><dd><p>
  112. This form adds the target foreign table as a new child of the specified
  113. parent table.
  114. See the similar form of <a class="xref" href="sql-altertable.html" title="ALTER TABLE"><span class="refentrytitle">ALTER TABLE</span></a>
  115. for more details.
  116. </p></dd><dt><span class="term"><code class="literal">NO INHERIT <em class="replaceable"><code>parent_table</code></em></code></span></dt><dd><p>
  117. This form removes the target foreign table from the list of children of
  118. the specified parent table.
  119. </p></dd><dt><span class="term"><code class="literal">OWNER</code></span></dt><dd><p>
  120. This form changes the owner of the foreign table to the
  121. specified user.
  122. </p></dd><dt><span class="term"><code class="literal">OPTIONS ( [ ADD | SET | DROP ] <em class="replaceable"><code>option</code></em> ['<em class="replaceable"><code>value</code></em>'] [, ... ] )</code></span></dt><dd><p>
  123. Change options for the foreign table or one of its columns.
  124. <code class="literal">ADD</code>, <code class="literal">SET</code>, and <code class="literal">DROP</code>
  125. specify the action to be performed. <code class="literal">ADD</code> is assumed
  126. if no operation is explicitly specified. Duplicate option names are not
  127. allowed (although it's OK for a table option and a column option to have
  128. the same name). Option names and values are also validated using the
  129. foreign data wrapper library.
  130. </p></dd><dt><span class="term"><code class="literal">RENAME</code></span></dt><dd><p>
  131. The <code class="literal">RENAME</code> forms change the name of a foreign table
  132. or the name of an individual column in a foreign table.
  133. </p></dd><dt><span class="term"><code class="literal">SET SCHEMA</code></span></dt><dd><p>
  134. This form moves the foreign table into another schema.
  135. </p></dd></dl></div><p>
  136. </p><p>
  137. All the actions except <code class="literal">RENAME</code> and <code class="literal">SET SCHEMA</code>
  138. can be combined into
  139. a list of multiple alterations to apply in parallel. For example, it
  140. is possible to add several columns and/or alter the type of several
  141. columns in a single command.
  142. </p><p>
  143. If the command is written as <code class="literal">ALTER FOREIGN TABLE IF EXISTS ...</code>
  144. and the foreign table does not exist, no error is thrown. A notice is
  145. issued in this case.
  146. </p><p>
  147. You must own the table to use <code class="command">ALTER FOREIGN TABLE</code>.
  148. To change the schema of a foreign table, you must also have
  149. <code class="literal">CREATE</code> privilege on the new schema.
  150. To alter the owner, you must also be a direct or indirect member of the new
  151. owning role, and that role must have <code class="literal">CREATE</code> privilege on
  152. the table's schema. (These restrictions enforce that altering the owner
  153. doesn't do anything you couldn't do by dropping and recreating the table.
  154. However, a superuser can alter ownership of any table anyway.)
  155. To add a column or alter a column type, you must also
  156. have <code class="literal">USAGE</code> privilege on the data type.
  157. </p></div><div class="refsect1" id="id-1.9.3.13.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  158. The name (possibly schema-qualified) of an existing foreign table to
  159. alter. If <code class="literal">ONLY</code> is specified before the table name, only
  160. that table is altered. If <code class="literal">ONLY</code> is not specified, the table
  161. and all its descendant tables (if any) are altered. Optionally,
  162. <code class="literal">*</code> can be specified after the table name to explicitly
  163. indicate that descendant tables are included.
  164. </p></dd><dt><span class="term"><em class="replaceable"><code>column_name</code></em></span></dt><dd><p>
  165. Name of a new or existing column.
  166. </p></dd><dt><span class="term"><em class="replaceable"><code>new_column_name</code></em></span></dt><dd><p>
  167. New name for an existing column.
  168. </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
  169. New name for the table.
  170. </p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p>
  171. Data type of the new column, or new data type for an existing
  172. column.
  173. </p></dd><dt><span class="term"><em class="replaceable"><code>table_constraint</code></em></span></dt><dd><p>
  174. New table constraint for the foreign table.
  175. </p></dd><dt><span class="term"><em class="replaceable"><code>constraint_name</code></em></span></dt><dd><p>
  176. Name of an existing constraint to drop.
  177. </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
  178. Automatically drop objects that depend on the dropped column
  179. or constraint (for example, views referencing the column),
  180. and in turn all objects that depend on those objects
  181. (see <a class="xref" href="ddl-depend.html" title="5.14. Dependency Tracking">Section 5.14</a>).
  182. </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
  183. Refuse to drop the column or constraint if there are any dependent
  184. objects. This is the default behavior.
  185. </p></dd><dt><span class="term"><em class="replaceable"><code>trigger_name</code></em></span></dt><dd><p>
  186. Name of a single trigger to disable or enable.
  187. </p></dd><dt><span class="term"><code class="literal">ALL</code></span></dt><dd><p>
  188. Disable or enable all triggers belonging to the foreign table. (This
  189. requires superuser privilege if any of the triggers are internally
  190. generated triggers. The core system does not add such triggers to
  191. foreign tables, but add-on code could do so.)
  192. </p></dd><dt><span class="term"><code class="literal">USER</code></span></dt><dd><p>
  193. Disable or enable all triggers belonging to the foreign table except
  194. for internally generated triggers.
  195. </p></dd><dt><span class="term"><em class="replaceable"><code>parent_table</code></em></span></dt><dd><p>
  196. A parent table to associate or de-associate with this foreign table.
  197. </p></dd><dt><span class="term"><em class="replaceable"><code>new_owner</code></em></span></dt><dd><p>
  198. The user name of the new owner of the table.
  199. </p></dd><dt><span class="term"><em class="replaceable"><code>new_schema</code></em></span></dt><dd><p>
  200. The name of the schema to which the table will be moved.
  201. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.13.7"><h2>Notes</h2><p>
  202. The key word <code class="literal">COLUMN</code> is noise and can be omitted.
  203. </p><p>
  204. Consistency with the foreign server is not checked when a column is added
  205. or removed with <code class="literal">ADD COLUMN</code> or
  206. <code class="literal">DROP COLUMN</code>, a <code class="literal">NOT NULL</code>
  207. or <code class="literal">CHECK</code> constraint is added, or a column type is changed
  208. with <code class="literal">SET DATA TYPE</code>. It is the user's responsibility to ensure
  209. that the table definition matches the remote side.
  210. </p><p>
  211. Refer to <a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a> for a further description of valid
  212. parameters.
  213. </p></div><div class="refsect1" id="id-1.9.3.13.8"><h2>Examples</h2><p>
  214. To mark a column as not-null:
  215. </p><pre class="programlisting">
  216. ALTER FOREIGN TABLE distributors ALTER COLUMN street SET NOT NULL;
  217. </pre><p>
  218. </p><p>
  219. To change options of a foreign table:
  220. </p><pre class="programlisting">
  221. ALTER FOREIGN TABLE myschema.distributors OPTIONS (ADD opt1 'value', SET opt2 'value2', DROP opt3 'value3');
  222. </pre></div><div class="refsect1" id="id-1.9.3.13.9"><h2>Compatibility</h2><p>
  223. The forms <code class="literal">ADD</code>, <code class="literal">DROP</code>,
  224. and <code class="literal">SET DATA TYPE</code>
  225. conform with the SQL standard. The other forms are
  226. <span class="productname">PostgreSQL</span> extensions of the SQL standard.
  227. Also, the ability to specify more than one manipulation in a single
  228. <code class="command">ALTER FOREIGN TABLE</code> command is an extension.
  229. </p><p>
  230. <code class="command">ALTER FOREIGN TABLE DROP COLUMN</code> can be used to drop the only
  231. column of a foreign table, leaving a zero-column table. This is an
  232. extension of SQL, which disallows zero-column foreign tables.
  233. </p></div><div class="refsect1" id="id-1.9.3.13.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createforeigntable.html" title="CREATE FOREIGN TABLE"><span class="refentrytitle">CREATE FOREIGN TABLE</span></a>, <a class="xref" href="sql-dropforeigntable.html" title="DROP FOREIGN TABLE"><span class="refentrytitle">DROP FOREIGN TABLE</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-alterforeigndatawrapper.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-alterfunction.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER 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"> ALTER FUNCTION</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1