gooderp18绿色标准版
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

152 lines
13KB

  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 DOMAIN</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-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES" /><link rel="next" href="sql-altereventtrigger.html" title="ALTER EVENT TRIGGER" /></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 DOMAIN</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES">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-altereventtrigger.html" title="ALTER EVENT TRIGGER">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-ALTERDOMAIN"><div class="titlepage"></div><a id="id-1.9.3.9.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER DOMAIN</span></h2><p>ALTER DOMAIN —
  3. change the definition of a domain
  4. </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  5. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  6. { SET DEFAULT <em class="replaceable"><code>expression</code></em> | DROP DEFAULT }
  7. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  8. { SET | DROP } NOT NULL
  9. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  10. ADD <em class="replaceable"><code>domain_constraint</code></em> [ NOT VALID ]
  11. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  12. DROP CONSTRAINT [ IF EXISTS ] <em class="replaceable"><code>constraint_name</code></em> [ RESTRICT | CASCADE ]
  13. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  14. RENAME CONSTRAINT <em class="replaceable"><code>constraint_name</code></em> TO <em class="replaceable"><code>new_constraint_name</code></em>
  15. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  16. VALIDATE CONSTRAINT <em class="replaceable"><code>constraint_name</code></em>
  17. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  18. OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_USER | SESSION_USER }
  19. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  20. RENAME TO <em class="replaceable"><code>new_name</code></em>
  21. ALTER DOMAIN <em class="replaceable"><code>name</code></em>
  22. SET SCHEMA <em class="replaceable"><code>new_schema</code></em>
  23. </pre></div><div class="refsect1" id="id-1.9.3.9.5"><h2>Description</h2><p>
  24. <code class="command">ALTER DOMAIN</code> changes the definition of an existing domain.
  25. There are several sub-forms:
  26. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP DEFAULT</code></span></dt><dd><p>
  27. These forms set or remove the default value for a domain. Note
  28. that defaults only apply to subsequent <code class="command">INSERT</code>
  29. commands; they do not affect rows already in a table using the domain.
  30. </p></dd><dt><span class="term"><code class="literal">SET</code>/<code class="literal">DROP NOT NULL</code></span></dt><dd><p>
  31. These forms change whether a domain is marked to allow NULL
  32. values or to reject NULL values. You can only <code class="literal">SET NOT NULL</code>
  33. when the columns using the domain contain no null values.
  34. </p></dd><dt><span class="term"><code class="literal">ADD <em class="replaceable"><code>domain_constraint</code></em> [ NOT VALID ]</code></span></dt><dd><p>
  35. This form adds a new constraint to a domain using the same syntax as
  36. <a class="xref" href="sql-createdomain.html" title="CREATE DOMAIN"><span class="refentrytitle">CREATE DOMAIN</span></a>.
  37. When a new constraint is added to a domain, all columns using that
  38. domain will be checked against the newly added constraint. These
  39. checks can be suppressed by adding the new constraint using the
  40. <code class="literal">NOT VALID</code> option; the constraint can later be made
  41. valid using <code class="command">ALTER DOMAIN ... VALIDATE CONSTRAINT</code>.
  42. Newly inserted or updated rows are always checked against all
  43. constraints, even those marked <code class="literal">NOT VALID</code>.
  44. <code class="literal">NOT VALID</code> is only accepted for <code class="literal">CHECK</code> constraints.
  45. </p></dd><dt><span class="term"><code class="literal">DROP CONSTRAINT [ IF EXISTS ]</code></span></dt><dd><p>
  46. This form drops constraints on a domain.
  47. If <code class="literal">IF EXISTS</code> is specified and the constraint
  48. does not exist, no error is thrown. In this case a notice is issued instead.
  49. </p></dd><dt><span class="term"><code class="literal">RENAME CONSTRAINT</code></span></dt><dd><p>
  50. This form changes the name of a constraint on a domain.
  51. </p></dd><dt><span class="term"><code class="literal">VALIDATE CONSTRAINT</code></span></dt><dd><p>
  52. This form validates a constraint previously added as
  53. <code class="literal">NOT VALID</code>, that is, it verifies that all values in
  54. table columns of the domain type satisfy the specified constraint.
  55. </p></dd><dt><span class="term"><code class="literal">OWNER</code></span></dt><dd><p>
  56. This form changes the owner of the domain to the specified user.
  57. </p></dd><dt><span class="term"><code class="literal">RENAME</code></span></dt><dd><p>
  58. This form changes the name of the domain.
  59. </p></dd><dt><span class="term"><code class="literal">SET SCHEMA</code></span></dt><dd><p>
  60. This form changes the schema of the domain. Any constraints
  61. associated with the domain are moved into the new schema as well.
  62. </p></dd></dl></div><p>
  63. You must own the domain to use <code class="command">ALTER DOMAIN</code>.
  64. To change the schema of a domain, you must also have
  65. <code class="literal">CREATE</code> privilege on the new schema.
  66. To alter the owner, you must also be a direct or indirect member of the new
  67. owning role, and that role must have <code class="literal">CREATE</code> privilege on
  68. the domain's schema. (These restrictions enforce that altering the owner
  69. doesn't do anything you couldn't do by dropping and recreating the domain.
  70. However, a superuser can alter ownership of any domain anyway.)
  71. </p></div><div class="refsect1" id="id-1.9.3.9.6"><h2>Parameters</h2><p>
  72. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  73. The name (possibly schema-qualified) of an existing domain to
  74. alter.
  75. </p></dd><dt><span class="term"><em class="replaceable"><code>domain_constraint</code></em></span></dt><dd><p>
  76. New domain constraint for the domain.
  77. </p></dd><dt><span class="term"><em class="replaceable"><code>constraint_name</code></em></span></dt><dd><p>
  78. Name of an existing constraint to drop or rename.
  79. </p></dd><dt><span class="term"><code class="literal">NOT VALID</code></span></dt><dd><p>
  80. Do not verify existing stored data for constraint validity.
  81. </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
  82. Automatically drop objects that depend on the constraint,
  83. and in turn all objects that depend on those objects
  84. (see <a class="xref" href="ddl-depend.html" title="5.14. Dependency Tracking">Section 5.14</a>).
  85. </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
  86. Refuse to drop the constraint if there are any dependent
  87. objects. This is the default behavior.
  88. </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
  89. The new name for the domain.
  90. </p></dd><dt><span class="term"><em class="replaceable"><code>new_constraint_name</code></em></span></dt><dd><p>
  91. The new name for the constraint.
  92. </p></dd><dt><span class="term"><em class="replaceable"><code>new_owner</code></em></span></dt><dd><p>
  93. The user name of the new owner of the domain.
  94. </p></dd><dt><span class="term"><em class="replaceable"><code>new_schema</code></em></span></dt><dd><p>
  95. The new schema for the domain.
  96. </p></dd></dl></div><p>
  97. </p></div><div class="refsect1" id="id-1.9.3.9.7"><h2>Notes</h2><p>
  98. Although <code class="command">ALTER DOMAIN ADD CONSTRAINT</code> attempts to verify
  99. that existing stored data satisfies the new constraint, this check is not
  100. bulletproof, because the command cannot <span class="quote">“<span class="quote">see</span>”</span> table rows that
  101. are newly inserted or updated and not yet committed. If there is a hazard
  102. that concurrent operations might insert bad data, the way to proceed is to
  103. add the constraint using the <code class="literal">NOT VALID</code> option, commit
  104. that command, wait until all transactions started before that commit have
  105. finished, and then issue <code class="command">ALTER DOMAIN VALIDATE
  106. CONSTRAINT</code> to search for data violating the constraint. This
  107. method is reliable because once the constraint is committed, all new
  108. transactions are guaranteed to enforce it against new values of the domain
  109. type.
  110. </p><p>
  111. Currently, <code class="command">ALTER DOMAIN ADD CONSTRAINT</code>, <code class="command">ALTER
  112. DOMAIN VALIDATE CONSTRAINT</code>, and <code class="command">ALTER DOMAIN SET NOT
  113. NULL</code> will fail if the named domain or any derived domain is used
  114. within a container-type column (a composite, array, or range column) in
  115. any table in the database. They should eventually be improved to be able
  116. to verify the new constraint for such nested values.
  117. </p></div><div class="refsect1" id="id-1.9.3.9.8"><h2>Examples</h2><p>
  118. To add a <code class="literal">NOT NULL</code> constraint to a domain:
  119. </p><pre class="programlisting">
  120. ALTER DOMAIN zipcode SET NOT NULL;
  121. </pre><p>
  122. To remove a <code class="literal">NOT NULL</code> constraint from a domain:
  123. </p><pre class="programlisting">
  124. ALTER DOMAIN zipcode DROP NOT NULL;
  125. </pre><p>
  126. </p><p>
  127. To add a check constraint to a domain:
  128. </p><pre class="programlisting">
  129. ALTER DOMAIN zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
  130. </pre><p>
  131. </p><p>
  132. To remove a check constraint from a domain:
  133. </p><pre class="programlisting">
  134. ALTER DOMAIN zipcode DROP CONSTRAINT zipchk;
  135. </pre><p>
  136. </p><p>
  137. To rename a check constraint on a domain:
  138. </p><pre class="programlisting">
  139. ALTER DOMAIN zipcode RENAME CONSTRAINT zipchk TO zip_check;
  140. </pre><p>
  141. </p><p>
  142. To move the domain into a different schema:
  143. </p><pre class="programlisting">
  144. ALTER DOMAIN zipcode SET SCHEMA customers;
  145. </pre></div><div class="refsect1" id="SQL-ALTERDOMAIN-COMPATIBILITY"><h2>Compatibility</h2><p>
  146. <code class="command">ALTER DOMAIN</code> conforms to the <acronym class="acronym">SQL</acronym>
  147. standard, except for the <code class="literal">OWNER</code>, <code class="literal">RENAME</code>, <code class="literal">SET SCHEMA</code>, and
  148. <code class="literal">VALIDATE CONSTRAINT</code> variants, which are
  149. <span class="productname">PostgreSQL</span> extensions. The <code class="literal">NOT VALID</code>
  150. clause of the <code class="literal">ADD CONSTRAINT</code> variant is also a
  151. <span class="productname">PostgreSQL</span> extension.
  152. </p></div><div class="refsect1" id="SQL-ALTERDOMAIN-SEE-ALSO"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createdomain.html" title="CREATE DOMAIN"><span class="refentrytitle">CREATE DOMAIN</span></a>, <a class="xref" href="sql-dropdomain.html" title="DROP DOMAIN"><span class="refentrytitle">DROP DOMAIN</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-alterdefaultprivileges.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-altereventtrigger.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER DEFAULT PRIVILEGES </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> ALTER EVENT TRIGGER</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1