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.

160 lines
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>ALTER SEQUENCE</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-alterschema.html" title="ALTER SCHEMA" /><link rel="next" href="sql-alterserver.html" title="ALTER SERVER" /></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 SEQUENCE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-alterschema.html" title="ALTER SCHEMA">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-alterserver.html" title="ALTER SERVER">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-ALTERSEQUENCE"><div class="titlepage"></div><a id="id-1.9.3.30.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">ALTER SEQUENCE</span></h2><p>ALTER SEQUENCE —
  3. change the definition of a sequence generator
  4. </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  5. ALTER SEQUENCE [ IF EXISTS ] <em class="replaceable"><code>name</code></em>
  6. [ AS <em class="replaceable"><code>data_type</code></em> ]
  7. [ INCREMENT [ BY ] <em class="replaceable"><code>increment</code></em> ]
  8. [ MINVALUE <em class="replaceable"><code>minvalue</code></em> | NO MINVALUE ] [ MAXVALUE <em class="replaceable"><code>maxvalue</code></em> | NO MAXVALUE ]
  9. [ START [ WITH ] <em class="replaceable"><code>start</code></em> ]
  10. [ RESTART [ [ WITH ] <em class="replaceable"><code>restart</code></em> ] ]
  11. [ CACHE <em class="replaceable"><code>cache</code></em> ] [ [ NO ] CYCLE ]
  12. [ OWNED BY { <em class="replaceable"><code>table_name</code></em>.<em class="replaceable"><code>column_name</code></em> | NONE } ]
  13. ALTER SEQUENCE [ IF EXISTS ] <em class="replaceable"><code>name</code></em> OWNER TO { <em class="replaceable"><code>new_owner</code></em> | CURRENT_USER | SESSION_USER }
  14. ALTER SEQUENCE [ IF EXISTS ] <em class="replaceable"><code>name</code></em> RENAME TO <em class="replaceable"><code>new_name</code></em>
  15. ALTER SEQUENCE [ IF EXISTS ] <em class="replaceable"><code>name</code></em> SET SCHEMA <em class="replaceable"><code>new_schema</code></em>
  16. </pre></div><div class="refsect1" id="id-1.9.3.30.5"><h2>Description</h2><p>
  17. <code class="command">ALTER SEQUENCE</code> changes the parameters of an existing
  18. sequence generator. Any parameters not specifically set in the
  19. <code class="command">ALTER SEQUENCE</code> command retain their prior settings.
  20. </p><p>
  21. You must own the sequence to use <code class="command">ALTER SEQUENCE</code>.
  22. To change a sequence's schema, you must also have <code class="literal">CREATE</code>
  23. privilege on the new schema.
  24. To alter the owner, you must also be a direct or indirect member of the new
  25. owning role, and that role must have <code class="literal">CREATE</code> privilege on
  26. the sequence's schema. (These restrictions enforce that altering the owner
  27. doesn't do anything you couldn't do by dropping and recreating the sequence.
  28. However, a superuser can alter ownership of any sequence anyway.)
  29. </p></div><div class="refsect1" id="id-1.9.3.30.6"><h2>Parameters</h2><p>
  30. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  31. The name (optionally schema-qualified) of a sequence to be altered.
  32. </p></dd><dt><span class="term"><code class="literal">IF EXISTS</code></span></dt><dd><p>
  33. Do not throw an error if the sequence does not exist. A notice is issued
  34. in this case.
  35. </p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p>
  36. The optional
  37. clause <code class="literal">AS <em class="replaceable"><code>data_type</code></em></code>
  38. changes the data type of the sequence. Valid types are
  39. <code class="literal">smallint</code>, <code class="literal">integer</code>,
  40. and <code class="literal">bigint</code>.
  41. </p><p>
  42. Changing the data type automatically changes the minimum and maximum
  43. values of the sequence if and only if the previous minimum and maximum
  44. values were the minimum or maximum value of the old data type (in
  45. other words, if the sequence had been created using <code class="literal">NO
  46. MINVALUE</code> or <code class="literal">NO MAXVALUE</code>, implicitly or
  47. explicitly). Otherwise, the minimum and maximum values are preserved,
  48. unless new values are given as part of the same command. If the
  49. minimum and maximum values do not fit into the new data type, an error
  50. will be generated.
  51. </p></dd><dt><span class="term"><em class="replaceable"><code>increment</code></em></span></dt><dd><p>
  52. The clause <code class="literal">INCREMENT BY <em class="replaceable"><code>increment</code></em></code> is
  53. optional. A positive value will make an ascending sequence, a
  54. negative one a descending sequence. If unspecified, the old
  55. increment value will be maintained.
  56. </p></dd><dt><span class="term"><em class="replaceable"><code>minvalue</code></em><br /></span><span class="term"><code class="literal">NO MINVALUE</code></span></dt><dd><p>
  57. The optional clause <code class="literal">MINVALUE <em class="replaceable"><code>minvalue</code></em></code> determines
  58. the minimum value a sequence can generate. If <code class="literal">NO
  59. MINVALUE</code> is specified, the defaults of 1 and
  60. the minimum value of the data type for ascending and descending sequences,
  61. respectively, will be used. If neither option is specified,
  62. the current minimum value will be maintained.
  63. </p></dd><dt><span class="term"><em class="replaceable"><code>maxvalue</code></em><br /></span><span class="term"><code class="literal">NO MAXVALUE</code></span></dt><dd><p>
  64. The optional clause <code class="literal">MAXVALUE <em class="replaceable"><code>maxvalue</code></em></code> determines
  65. the maximum value for the sequence. If <code class="literal">NO
  66. MAXVALUE</code> is specified, the defaults of
  67. the maximum value of the data type and -1 for ascending and descending
  68. sequences, respectively, will be used. If neither option is
  69. specified, the current maximum value will be maintained.
  70. </p></dd><dt><span class="term"><em class="replaceable"><code>start</code></em></span></dt><dd><p>
  71. The optional clause <code class="literal">START WITH <em class="replaceable"><code>start</code></em></code> changes the
  72. recorded start value of the sequence. This has no effect on the
  73. <span class="emphasis"><em>current</em></span> sequence value; it simply sets the value
  74. that future <code class="command">ALTER SEQUENCE RESTART</code> commands will use.
  75. </p></dd><dt><span class="term"><em class="replaceable"><code>restart</code></em></span></dt><dd><p>
  76. The optional clause <code class="literal">RESTART [ WITH <em class="replaceable"><code>restart</code></em> ]</code> changes the
  77. current value of the sequence. This is similar to calling the
  78. <code class="function">setval</code> function with <code class="literal">is_called</code> =
  79. <code class="literal">false</code>: the specified value will be returned by the
  80. <span class="emphasis"><em>next</em></span> call of <code class="function">nextval</code>.
  81. Writing <code class="literal">RESTART</code> with no <em class="replaceable"><code>restart</code></em> value is equivalent to supplying
  82. the start value that was recorded by <code class="command">CREATE SEQUENCE</code>
  83. or last set by <code class="command">ALTER SEQUENCE START WITH</code>.
  84. </p><p>
  85. In contrast to a <code class="function">setval</code> call,
  86. a <code class="literal">RESTART</code> operation on a sequence is transactional
  87. and blocks concurrent transactions from obtaining numbers from the
  88. same sequence. If that's not the desired mode of
  89. operation, <code class="function">setval</code> should be used.
  90. </p></dd><dt><span class="term"><em class="replaceable"><code>cache</code></em></span></dt><dd><p>
  91. The clause <code class="literal">CACHE <em class="replaceable"><code>cache</code></em></code> enables
  92. sequence numbers to be preallocated and stored in memory for
  93. faster access. The minimum value is 1 (only one value can be
  94. generated at a time, i.e., no cache). If unspecified, the old
  95. cache value will be maintained.
  96. </p></dd><dt><span class="term"><code class="literal">CYCLE</code></span></dt><dd><p>
  97. The optional <code class="literal">CYCLE</code> key word can be used to enable
  98. the sequence to wrap around when the
  99. <em class="replaceable"><code>maxvalue</code></em> or
  100. <em class="replaceable"><code>minvalue</code></em> has been
  101. reached by
  102. an ascending or descending sequence respectively. If the limit is
  103. reached, the next number generated will be the
  104. <em class="replaceable"><code>minvalue</code></em> or
  105. <em class="replaceable"><code>maxvalue</code></em>,
  106. respectively.
  107. </p></dd><dt><span class="term"><code class="literal">NO CYCLE</code></span></dt><dd><p>
  108. If the optional <code class="literal">NO CYCLE</code> key word is
  109. specified, any calls to <code class="function">nextval</code> after the
  110. sequence has reached its maximum value will return an error.
  111. If neither <code class="literal">CYCLE</code> or <code class="literal">NO
  112. CYCLE</code> are specified, the old cycle behavior will be
  113. maintained.
  114. </p></dd><dt><span class="term"><code class="literal">OWNED BY</code> <em class="replaceable"><code>table_name</code></em>.<em class="replaceable"><code>column_name</code></em><br /></span><span class="term"><code class="literal">OWNED BY NONE</code></span></dt><dd><p>
  115. The <code class="literal">OWNED BY</code> option causes the sequence to be
  116. associated with a specific table column, such that if that column
  117. (or its whole table) is dropped, the sequence will be automatically
  118. dropped as well. If specified, this association replaces any
  119. previously specified association for the sequence. The specified
  120. table must have the same owner and be in the same schema as the
  121. sequence.
  122. Specifying <code class="literal">OWNED BY NONE</code> removes any existing
  123. association, making the sequence <span class="quote">“<span class="quote">free-standing</span>”</span>.
  124. </p></dd><dt><span class="term"><em class="replaceable"><code>new_owner</code></em></span></dt><dd><p>
  125. The user name of the new owner of the sequence.
  126. </p></dd><dt><span class="term"><em class="replaceable"><code>new_name</code></em></span></dt><dd><p>
  127. The new name for the sequence.
  128. </p></dd><dt><span class="term"><em class="replaceable"><code>new_schema</code></em></span></dt><dd><p>
  129. The new schema for the sequence.
  130. </p></dd></dl></div><p>
  131. </p></div><div class="refsect1" id="id-1.9.3.30.7"><h2>Notes</h2><p>
  132. <code class="command">ALTER SEQUENCE</code> will not immediately affect
  133. <code class="function">nextval</code> results in backends,
  134. other than the current one, that have preallocated (cached) sequence
  135. values. They will use up all cached values prior to noticing the changed
  136. sequence generation parameters. The current backend will be affected
  137. immediately.
  138. </p><p>
  139. <code class="command">ALTER SEQUENCE</code> does not affect the <code class="function">currval</code>
  140. status for the sequence. (Before <span class="productname">PostgreSQL</span>
  141. 8.3, it sometimes did.)
  142. </p><p>
  143. <code class="command">ALTER SEQUENCE</code> blocks
  144. concurrent <code class="function">nextval</code>, <code class="function">currval</code>,
  145. <code class="function">lastval</code>, and <code class="command">setval</code> calls.
  146. </p><p>
  147. For historical reasons, <code class="command">ALTER TABLE</code> can be used with
  148. sequences too; but the only variants of <code class="command">ALTER TABLE</code>
  149. that are allowed with sequences are equivalent to the forms shown above.
  150. </p></div><div class="refsect1" id="id-1.9.3.30.8"><h2>Examples</h2><p>
  151. Restart a sequence called <code class="literal">serial</code>, at 105:
  152. </p><pre class="programlisting">
  153. ALTER SEQUENCE serial RESTART WITH 105;
  154. </pre></div><div class="refsect1" id="id-1.9.3.30.9"><h2>Compatibility</h2><p>
  155. <code class="command">ALTER SEQUENCE</code> conforms to the <acronym class="acronym">SQL</acronym>
  156. standard, except for the <code class="literal">AS</code>, <code class="literal">START WITH</code>,
  157. <code class="literal">OWNED BY</code>, <code class="literal">OWNER TO</code>, <code class="literal">RENAME TO</code>, and
  158. <code class="literal">SET SCHEMA</code> clauses, which are
  159. <span class="productname">PostgreSQL</span> extensions.
  160. </p></div><div class="refsect1" id="id-1.9.3.30.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-createsequence.html" title="CREATE SEQUENCE"><span class="refentrytitle">CREATE SEQUENCE</span></a>, <a class="xref" href="sql-dropsequence.html" title="DROP SEQUENCE"><span class="refentrytitle">DROP SEQUENCE</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-alterschema.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-alterserver.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALTER SCHEMA </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> ALTER SERVER</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1