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.

201 lines
15KB

  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 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-createschema.html" title="CREATE SCHEMA" /><link rel="next" href="sql-createserver.html" title="CREATE 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">CREATE SEQUENCE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createschema.html" title="CREATE 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-createserver.html" title="CREATE SERVER">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATESEQUENCE"><div class="titlepage"></div><a id="id-1.9.3.81.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE SEQUENCE</span></h2><p>CREATE SEQUENCE — define a new sequence generator</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ TEMPORARY | TEMP ] SEQUENCE [ IF NOT EXISTS ] <em class="replaceable"><code>name</code></em>
  4. [ AS <em class="replaceable"><code>data_type</code></em> ]
  5. [ INCREMENT [ BY ] <em class="replaceable"><code>increment</code></em> ]
  6. [ MINVALUE <em class="replaceable"><code>minvalue</code></em> | NO MINVALUE ] [ MAXVALUE <em class="replaceable"><code>maxvalue</code></em> | NO MAXVALUE ]
  7. [ START [ WITH ] <em class="replaceable"><code>start</code></em> ] [ CACHE <em class="replaceable"><code>cache</code></em> ] [ [ NO ] CYCLE ]
  8. [ OWNED BY { <em class="replaceable"><code>table_name</code></em>.<em class="replaceable"><code>column_name</code></em> | NONE } ]
  9. </pre></div><div class="refsect1" id="id-1.9.3.81.5"><h2>Description</h2><p>
  10. <code class="command">CREATE SEQUENCE</code> creates a new sequence number
  11. generator. This involves creating and initializing a new special
  12. single-row table with the name <em class="replaceable"><code>name</code></em>. The generator will be
  13. owned by the user issuing the command.
  14. </p><p>
  15. If a schema name is given then the sequence is created in the
  16. specified schema. Otherwise it is created in the current schema.
  17. Temporary sequences exist in a special schema, so a schema name cannot be
  18. given when creating a temporary sequence.
  19. The sequence name must be distinct from the name of any other sequence,
  20. table, index, view, or foreign table in the same schema.
  21. </p><p>
  22. After a sequence is created, you use the functions
  23. <code class="function">nextval</code>,
  24. <code class="function">currval</code>, and
  25. <code class="function">setval</code>
  26. to operate on the sequence. These functions are documented in
  27. <a class="xref" href="functions-sequence.html" title="9.16. Sequence Manipulation Functions">Section 9.16</a>.
  28. </p><p>
  29. Although you cannot update a sequence directly, you can use a query like:
  30. </p><pre class="programlisting">
  31. SELECT * FROM <em class="replaceable"><code>name</code></em>;
  32. </pre><p>
  33. to examine the parameters and current state of a sequence. In particular,
  34. the <code class="literal">last_value</code> field of the sequence shows the last value
  35. allocated by any session. (Of course, this value might be obsolete
  36. by the time it's printed, if other sessions are actively doing
  37. <code class="function">nextval</code> calls.)
  38. </p></div><div class="refsect1" id="id-1.9.3.81.6"><h2>Parameters</h2><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>
  39. If specified, the sequence object is created only for this
  40. session, and is automatically dropped on session exit. Existing
  41. permanent sequences with the same name are not visible (in this
  42. session) while the temporary sequence exists, unless they are
  43. referenced with schema-qualified names.
  44. </p></dd><dt><span class="term"><code class="literal">IF NOT EXISTS</code></span></dt><dd><p>
  45. Do not throw an error if a relation with the same name already exists.
  46. A notice is issued in this case. Note that there is no guarantee that
  47. the existing relation is anything like the sequence that would have
  48. been created - it might not even be a sequence.
  49. </p></dd><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  50. The name (optionally schema-qualified) of the sequence to be created.
  51. </p></dd><dt><span class="term"><em class="replaceable"><code>data_type</code></em></span></dt><dd><p>
  52. The optional
  53. clause <code class="literal">AS <em class="replaceable"><code>data_type</code></em></code>
  54. specifies the data type of the sequence. Valid types are
  55. <code class="literal">smallint</code>, <code class="literal">integer</code>,
  56. and <code class="literal">bigint</code>. <code class="literal">bigint</code> is the
  57. default. The data type determines the default minimum and maximum
  58. values of the sequence.
  59. </p></dd><dt><span class="term"><em class="replaceable"><code>increment</code></em></span></dt><dd><p>
  60. The optional clause <code class="literal">INCREMENT BY <em class="replaceable"><code>increment</code></em></code> specifies
  61. which value is added to the current sequence value to create a
  62. new value. A positive value will make an ascending sequence, a
  63. negative one a descending sequence. The default value is 1.
  64. </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>
  65. The optional clause <code class="literal">MINVALUE <em class="replaceable"><code>minvalue</code></em></code> determines
  66. the minimum value a sequence can generate. If this clause is not
  67. supplied or <code class="option">NO MINVALUE</code> is specified, then
  68. defaults will be used. The default for an ascending sequence is 1. The
  69. default for a descending sequence is the minimum value of the data type.
  70. </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>
  71. The optional clause <code class="literal">MAXVALUE <em class="replaceable"><code>maxvalue</code></em></code> determines
  72. the maximum value for the sequence. If this clause is not
  73. supplied or <code class="option">NO MAXVALUE</code> is specified, then
  74. default values will be used. The default for an ascending sequence is
  75. the maximum value of the data type. The default for a descending
  76. sequence is -1.
  77. </p></dd><dt><span class="term"><em class="replaceable"><code>start</code></em></span></dt><dd><p>
  78. The optional clause <code class="literal">START WITH <em class="replaceable"><code>start</code></em> </code> allows the
  79. sequence to begin anywhere. The default starting value is
  80. <em class="replaceable"><code>minvalue</code></em> for
  81. ascending sequences and <em class="replaceable"><code>maxvalue</code></em> for descending ones.
  82. </p></dd><dt><span class="term"><em class="replaceable"><code>cache</code></em></span></dt><dd><p>
  83. The optional clause <code class="literal">CACHE <em class="replaceable"><code>cache</code></em></code> specifies how
  84. many sequence numbers are to be preallocated and stored in
  85. memory for faster access. The minimum value is 1 (only one value
  86. can be generated at a time, i.e., no cache), and this is also the
  87. default.
  88. </p></dd><dt><span class="term"><code class="literal">CYCLE</code><br /></span><span class="term"><code class="literal">NO CYCLE</code></span></dt><dd><p>
  89. The <code class="literal">CYCLE</code> option allows the sequence to wrap
  90. around when the <em class="replaceable"><code>maxvalue</code></em> or <em class="replaceable"><code>minvalue</code></em> has been reached by an
  91. ascending or descending sequence respectively. If the limit is
  92. reached, the next number generated will be the <em class="replaceable"><code>minvalue</code></em> or <em class="replaceable"><code>maxvalue</code></em>, respectively.
  93. </p><p>
  94. If <code class="literal">NO CYCLE</code> is specified, any calls to
  95. <code class="function">nextval</code> after the sequence has reached its
  96. maximum value will return an error. If neither
  97. <code class="literal">CYCLE</code> or <code class="literal">NO CYCLE</code> are
  98. specified, <code class="literal">NO CYCLE</code> is the default.
  99. </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>
  100. The <code class="literal">OWNED BY</code> option causes the sequence to be
  101. associated with a specific table column, such that if that column
  102. (or its whole table) is dropped, the sequence will be automatically
  103. dropped as well. The specified table must have the same owner and be in
  104. the same schema as the sequence.
  105. <code class="literal">OWNED BY NONE</code>, the default, specifies that there
  106. is no such association.
  107. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.81.7"><h2>Notes</h2><p>
  108. Use <code class="command">DROP SEQUENCE</code> to remove a sequence.
  109. </p><p>
  110. Sequences are based on <code class="type">bigint</code> arithmetic, so the range
  111. cannot exceed the range of an eight-byte integer
  112. (-9223372036854775808 to 9223372036854775807).
  113. </p><p>
  114. Because <code class="function">nextval</code> and <code class="function">setval</code> calls are never
  115. rolled back, sequence objects cannot be used if <span class="quote">“<span class="quote">gapless</span>”</span>
  116. assignment of sequence numbers is needed. It is possible to build
  117. gapless assignment by using exclusive locking of a table containing a
  118. counter; but this solution is much more expensive than sequence
  119. objects, especially if many transactions need sequence numbers
  120. concurrently.
  121. </p><p>
  122. Unexpected results might be obtained if a <em class="replaceable"><code>cache</code></em> setting greater than one is
  123. used for a sequence object that will be used concurrently by
  124. multiple sessions. Each session will allocate and cache successive
  125. sequence values during one access to the sequence object and
  126. increase the sequence object's <code class="literal">last_value</code> accordingly.
  127. Then, the next <em class="replaceable"><code>cache</code></em>-1
  128. uses of <code class="function">nextval</code> within that session simply return the
  129. preallocated values without touching the sequence object. So, any
  130. numbers allocated but not used within a session will be lost when
  131. that session ends, resulting in <span class="quote">“<span class="quote">holes</span>”</span> in the
  132. sequence.
  133. </p><p>
  134. Furthermore, although multiple sessions are guaranteed to allocate
  135. distinct sequence values, the values might be generated out of
  136. sequence when all the sessions are considered. For example, with
  137. a <em class="replaceable"><code>cache</code></em> setting of 10,
  138. session A might reserve values 1..10 and return
  139. <code class="function">nextval</code>=1, then session B might reserve values
  140. 11..20 and return <code class="function">nextval</code>=11 before session A
  141. has generated <code class="literal">nextval</code>=2. Thus, with a
  142. <em class="replaceable"><code>cache</code></em> setting of one
  143. it is safe to assume that <code class="function">nextval</code> values are generated
  144. sequentially; with a <em class="replaceable"><code>cache</code></em> setting greater than one you
  145. should only assume that the <code class="function">nextval</code> values are all
  146. distinct, not that they are generated purely sequentially. Also,
  147. <code class="literal">last_value</code> will reflect the latest value reserved by
  148. any session, whether or not it has yet been returned by
  149. <code class="function">nextval</code>.
  150. </p><p>
  151. Another consideration is that a <code class="function">setval</code> executed on
  152. such a sequence will not be noticed by other sessions until they
  153. have used up any preallocated values they have cached.
  154. </p></div><div class="refsect1" id="id-1.9.3.81.8"><h2>Examples</h2><p>
  155. Create an ascending sequence called <code class="literal">serial</code>, starting at 101:
  156. </p><pre class="programlisting">
  157. CREATE SEQUENCE serial START 101;
  158. </pre><p>
  159. </p><p>
  160. Select the next number from this sequence:
  161. </p><pre class="programlisting">
  162. SELECT nextval('serial');
  163. nextval
  164. ---------
  165. 101
  166. </pre><p>
  167. </p><p>
  168. Select the next number from this sequence:
  169. </p><pre class="programlisting">
  170. SELECT nextval('serial');
  171. nextval
  172. ---------
  173. 102
  174. </pre><p>
  175. </p><p>
  176. Use this sequence in an <code class="command">INSERT</code> command:
  177. </p><pre class="programlisting">
  178. INSERT INTO distributors VALUES (nextval('serial'), 'nothing');
  179. </pre><p>
  180. </p><p>
  181. Update the sequence value after a <code class="command">COPY FROM</code>:
  182. </p><pre class="programlisting">
  183. BEGIN;
  184. COPY distributors FROM 'input_file';
  185. SELECT setval('serial', max(id)) FROM distributors;
  186. END;
  187. </pre></div><div class="refsect1" id="id-1.9.3.81.9"><h2>Compatibility</h2><p>
  188. <code class="command">CREATE SEQUENCE</code> conforms to the <acronym class="acronym">SQL</acronym>
  189. standard, with the following exceptions:
  190. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  191. Obtaining the next value is done using the <code class="function">nextval()</code>
  192. function instead of the standard's <code class="command">NEXT VALUE FOR</code>
  193. expression.
  194. </p></li><li class="listitem"><p>
  195. The <code class="literal">OWNED BY</code> clause is a <span class="productname">PostgreSQL</span>
  196. extension.
  197. </p></li></ul></div></div><div class="refsect1" id="id-1.9.3.81.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-altersequence.html" title="ALTER SEQUENCE"><span class="refentrytitle">ALTER 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-createschema.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-createserver.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE SCHEMA </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE SERVER</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1