aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/ref/create_sequence.sgml
diff options
context:
space:
mode:
Diffstat (limited to 'doc/src/sgml/ref/create_sequence.sgml')
-rw-r--r--doc/src/sgml/ref/create_sequence.sgml497
1 files changed, 242 insertions, 255 deletions
diff --git a/doc/src/sgml/ref/create_sequence.sgml b/doc/src/sgml/ref/create_sequence.sgml
index f6d6ae0b8d4..352c839f917 100644
--- a/doc/src/sgml/ref/create_sequence.sgml
+++ b/doc/src/sgml/ref/create_sequence.sgml
@@ -1,23 +1,23 @@
-<REFENTRY ID="SQL-CREATESEQUENCE">
- <REFMETA>
- <REFENTRYTITLE>
+<refentry id="SQL-CREATESEQUENCE">
+ <refmeta>
+ <refentrytitle>
CREATE SEQUENCE
- </REFENTRYTITLE>
- <REFMISCINFO>SQL - Language Statements</REFMISCINFO>
- </REFMETA>
- <REFNAMEDIV>
- <REFNAME>
+ </refentrytitle>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+ <refnamediv>
+ <refname>
CREATE SEQUENCE
- </REFNAME>
- <REFPURPOSE>
+ </refname>
+ <refpurpose>
Creates a new sequence number generator
- </REFPURPOSE>
+ </refpurpose>
</refnamediv>
- <REFSYNOPSISDIV>
- <REFSYNOPSISDIVINFO>
- <DATE>1998-04-15</DATE>
- </REFSYNOPSISDIVINFO>
- <SYNOPSIS>
+ <refsynopsisdiv>
+ <refsynopsisdivinfo>
+ <date>1998-04-15</date>
+ </refsynopsisdivinfo>
+ <synopsis>
CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable>
[ INCREMENT <replaceable class="parameter">increment</replaceable> ]
[ MINVALUE <replaceable class="parameter">minvalue</replaceable> ]
@@ -25,218 +25,207 @@ CREATE SEQUENCE <replaceable class="parameter">seqname</replaceable>
[ START <replaceable class="parameter">start</replaceable> ]
[ CACHE <replaceable class="parameter">cache</replaceable> ]
[ CYCLE ]
- </SYNOPSIS>
+ </synopsis>
- <REFSECT2 ID="R2-SQL-CREATESEQUENCE-1">
- <REFSECT2INFO>
- <DATE>1998-09-11</DATE>
- </REFSECT2INFO>
- <TITLE>
+ <refsect2 id="R2-SQL-CREATESEQUENCE-1">
+ <refsect2info>
+ <date>1998-09-11</date>
+ </refsect2info>
+ <title>
Inputs
- </TITLE>
- <PARA>
- </PARA>
- <VARIABLELIST>
- <VARLISTENTRY>
- <TERM>
- <replaceable class="parameter">seqname</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- The name of a sequence to be created.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- <replaceable class="parameter">increment</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- The <option>INCREMENT <replaceable class="parameter">increment</replaceable></option> clause is optional. A positive value will make an
- ascending sequence, a negative one a descending sequence.
- The default value is one (1).
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- <replaceable class="parameter">minvalue</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- The optional clause <option>MINVALUE
- <replaceable class="parameter">minvalue</replaceable></option>
- determines the minimum value
- a sequence can generate. The defaults are 1 and -2147483647 for
- ascending and descending sequences, respectively.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- <replaceable class="parameter">maxvalue</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- Use the optional clause <option>MAXVALUE
- <replaceable class="parameter">maxvalue</replaceable></option> to
- determine the maximum
- value for the sequence. The defaults are 2147483647 and -1 for
- ascending and descending sequences, respectively.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
+ </title>
+ <para>
+
+ <variablelist>
+ <varlistentry>
+ <term><replaceable class="parameter">seqname</replaceable></term>
+ <listitem>
+ <para>
+ The name of a sequence to be created.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">increment</replaceable></term>
+ <listitem>
+ <para>
+ The
+ <option>INCREMENT <replaceable class="parameter">increment</replaceable></option>
+ clause is optional. A positive value will make an
+ ascending sequence, a negative one a descending sequence.
+ The default value is one (1).
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">minvalue</replaceable></term>
+ <listitem>
+ <para>
+ The optional clause <option>MINVALUE
+ <replaceable class="parameter">minvalue</replaceable></option>
+ determines the minimum value
+ a sequence can generate. The defaults are 1 and -2147483647 for
+ ascending and descending sequences, respectively.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">maxvalue</replaceable></term>
+ <listitem>
+ <para>
+ Use the optional clause <option>MAXVALUE
+ <replaceable class="parameter">maxvalue</replaceable></option> to
+ determine the maximum
+ value for the sequence. The defaults are 2147483647 and -1 for
+ ascending and descending sequences, respectively.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">start</replaceable></term>
+ <listitem>
+ <para>
+ The optional <option>START
<replaceable class="parameter">start</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- The optional <option>START
- <replaceable class="parameter">start</replaceable>
- clause</option> enables the sequence to begin anywhere.
- The default starting value is
- <replaceable class="parameter">minvalue</replaceable>
- for ascending sequences and
- <replaceable class="parameter">maxvalue</replaceable>
- for descending ones.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- <replaceable class="parameter">cache</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- The <option>CACHE <replaceable class="parameter">cache</replaceable></option> option
- enables sequence numbers to be preallocated
- and stored in memory for faster access. The minimum
- value is 1 (only one value can be generated at a time, i.e. no cache)
- and this is also the default.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- CYCLE
- </TERM>
- <LISTITEM>
- <PARA>
- The optional CYCLE keyword may be used to enable the sequence
- to continue when the
- <replaceable class="parameter">maxvalue</replaceable> or
- <replaceable class="parameter">minvalue</replaceable> has been
- reached by
- an ascending or descending sequence respectively. If the limit is
- reached, the next number generated will be whatever the
- <replaceable class="parameter">minvalue</replaceable> or
- <replaceable class="parameter">maxvalue</replaceable> is,
- as appropriate.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- </variablelist>
- </REFSECT2>
-
- <REFSECT2 ID="R2-SQL-CREATESEQUENCE-2">
- <REFSECT2INFO>
- <DATE>1998-09-11</DATE>
- </REFSECT2INFO>
- <TITLE>
+ clause</option> enables the sequence to begin anywhere.
+ The default starting value is
+ <replaceable class="parameter">minvalue</replaceable>
+ for ascending sequences and
+ <replaceable class="parameter">maxvalue</replaceable>
+ for descending ones.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">cache</replaceable></term>
+ <listitem>
+ <para>
+ The <option>CACHE <replaceable class="parameter">cache</replaceable></option> option
+ enables sequence numbers to be preallocated
+ and stored in memory for faster access. The minimum
+ value is 1 (only one value can be generated at a time, i.e. no cache)
+ and this is also the default.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>CYCLE</term>
+ <listitem>
+ <para>
+ The optional CYCLE keyword may be used to enable the sequence
+ to continue when the
+ <replaceable class="parameter">maxvalue</replaceable> or
+ <replaceable class="parameter">minvalue</replaceable> has been
+ reached by
+ an ascending or descending sequence respectively. If the limit is
+ reached, the next number generated will be whatever the
+ <replaceable class="parameter">minvalue</replaceable> or
+ <replaceable class="parameter">maxvalue</replaceable> is,
+ as appropriate.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </para>
+ </refsect2>
+
+ <refsect2 id="R2-SQL-CREATESEQUENCE-2">
+ <refsect2info>
+ <date>1998-09-11</date>
+ </refsect2info>
+ <title>
Outputs
- </TITLE>
- <PARA>
+ </title>
+ <para>
- <VARIABLELIST>
- <VARLISTENTRY>
- <TERM>
- <ReturnValue>CREATE</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- Message returned if the command is successful.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
-<ReturnValue>ERROR: amcreate: '<replaceable class="parameter">seqname</replaceable>' relation already exists</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- If the sequence specified already exists.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
-<ReturnValue>ERROR: DefineSequence: START value (<replaceable class="parameter">start</replaceable>) can't be > MAXVALUE (<replaceable class="parameter">maxvalue</replaceable>)</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- If the specified starting value is out of range.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
-<ReturnValue>ERROR: DefineSequence: START value (<replaceable class="parameter">start</replaceable>) can't be < MINVALUE (<replaceable class="parameter">minvalue</replaceable>)</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- If the specified starting value is out of range.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
-<ReturnValue>ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">minvalue</replaceable>) can't be >= MAXVALUE (<replaceable class="parameter">maxvalue</replaceable>)</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- If the minimum and maximum values are inconsistant.
+ <variablelist>
+ <varlistentry>
+ <term><computeroutput>
+CREATE
+ </computeroutput></term>
+ <listitem>
+ <para>
+ Message returned if the command is successful.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><computeroutput>
+ERROR: Relation '<replaceable class="parameter">seqname</replaceable>' already exists
+ </computeroutput></term>
+ <listitem>
+ <para>
+ If the sequence specified already exists.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><computeroutput>
+ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">start</replaceable>) can't be >= MAXVALUE (<replaceable class="parameter">max</replaceable>)
+ </computeroutput></term>
+ <listitem>
+ <para>
+ If the specified starting value is out of range.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><computeroutput>
+ERROR: DefineSequence: START value (<replaceable class="parameter">start</replaceable>) can't be < MINVALUE (<replaceable class="parameter">min</replaceable>)
+ </computeroutput></term>
+ <listitem>
+ <para>
+ If the specified starting value is out of range.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><computeroutput>
+ERROR: DefineSequence: MINVALUE (<replaceable class="parameter">min</replaceable>) can't be >= MAXVALUE (<replaceable class="parameter">max</replaceable>)
+ </computeroutput></term>
+ <listitem>
+ <para>
+ If the minimum and maximum values are inconsistant.
</para>
</listitem>
</varlistentry>
- </VARIABLELIST>
+ </variablelist>
</para>
- </REFSECT2>
- </REFSYNOPSISDIV>
-
- <REFSECT1 ID="R1-SQL-CREATESEQUENCE-1">
- <REFSECT1INFO>
- <DATE>1998-09-11</DATE>
- </REFSECT1INFO>
- <TITLE>
+ </refsect2>
+ </refsynopsisdiv>
+
+ <refsect1 id="R1-SQL-CREATESEQUENCE-1">
+ <refsect1info>
+ <date>1998-09-11</date>
+ </refsect1info>
+ <title>
Description
- </TITLE>
- <PARA>
- CREATE SEQUENCE will enter a new sequence number generator
+ </title>
+ <para>
+ <command>CREATE SEQUENCE</command> will enter a new sequence number generator
into the current data base. This involves creating and initialising a
new single-row
table with the name <replaceable class="parameter">seqname</replaceable>.
The generator will be "owned" by the user issuing the command.
- </PARA>
+ </para>
<para>
After a sequence is created, you may use the function
-<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
-to get a new number from the sequence.
-The function
-<function>currval('<replaceable class="parameter">seqname</replaceable>')</function>
- may be used to determine the number returned by the last call to
-<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
- for the specified sequence in the current session.
-The function
-<function>setval('<replaceable class="parameter">seqname</replaceable>',
- <replaceable class="parameter">newvalue</replaceable>)</function>
-may be used to set the current value of the specified sequence.
-The next call to
-<function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
-will return the given value plus the sequence increment.
+ <function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
+ to get a new number from the sequence.
+ The function
+ <function>currval('<replaceable class="parameter">seqname</replaceable>')</function>
+ may be used to determine the number returned by the last call to
+ <function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
+ for the specified sequence in the current session.
+ The function
+ <function>setval('<replaceable class="parameter">seqname</replaceable>',
+ <replaceable class="parameter">newvalue</replaceable>)</function>
+ may be used to set the current value of the specified sequence.
+ The next call to
+ <function>nextval(<replaceable class="parameter">seqname</replaceable>)</function>
+ will return the given value plus the sequence increment.
</para>
-
+
<para>
Use a query like
<programlisting>
@@ -256,7 +245,7 @@ SELECT last_value FROM sequence_name;
calls to a generator.
</para>
-<caution>
+ <caution>
<para>
Unexpected results may be obtained if a cache setting greater than one
is used for a sequence object that will be used concurrently by multiple
@@ -279,35 +268,35 @@ SELECT last_value FROM sequence_name;
whether or not it has yet been returned by nextval.
</para>
</caution>
-
- <REFSECT2 ID="R2-SQL-CREATESEQUENCE-3">
- <REFSECT2INFO>
- <DATE>1998-09-11</DATE>
- </REFSECT2INFO>
- <TITLE>
+
+ <refsect2 id="R2-SQL-CREATESEQUENCE-3">
+ <refsect2info>
+ <date>1998-09-11</date>
+ </refsect2info>
+ <title>
Notes
- </TITLE>
- <PARA>
- Refer to the DROP SEQUENCE statement to remove a sequence.
- </PARA>
+ </title>
+ <para>
+ Refer to the <command>DROP SEQUENCE</command> statement to remove a sequence.
+ </para>
<para>
Each backend uses its own cache to store allocated numbers.
Numbers that are cached but not used in the current session will be
lost, resulting in "holes" in the sequence.
</para>
- </REFSECT2>
+ </refsect2>
</refsect1>
-
- <REFSECT1 ID="R1-SQL-CREATESEQUENCE-2">
- <TITLE>
+
+ <refsect1 id="R1-SQL-CREATESEQUENCE-2">
+ <title>
Usage
- </TITLE>
- <PARA>
+ </title>
+ <para>
Create an ascending sequence called <literal>serial</literal>, starting at 101:
- </PARA>
- <ProgramListing>
+ </para>
+ <programlisting>
CREATE SEQUENCE serial START 101;
- </ProgramListing>
+ </programlisting>
<para>
Select the next number from this sequence
<programlisting>
@@ -328,47 +317,45 @@ INSERT INTO distributors VALUES (NEXTVAL('serial'),'nothing');
<para>
Set the sequence value after a COPY FROM:
<programlisting>
- CREATE FUNCTION distributors_id_max() RETURNS INT4
+CREATE FUNCTION distributors_id_max() RETURNS INT4
AS 'SELECT max(id) FROM distributors'
LANGUAGE 'sql';
- BEGIN;
+BEGIN;
COPY distributors FROM 'input_file';
SELECT setval('serial', distributors_id_max());
- END;
+END;
</programlisting>
</para>
-
- </REFSECT1>
-
- <REFSECT1 ID="R1-SQL-CREATESEQUENCE-3">
- <TITLE>
+ </refsect1>
+
+ <refsect1 id="R1-SQL-CREATESEQUENCE-3">
+ <title>
Compatibility
- </TITLE>
- <PARA>
+ </title>
+ <para>
<command>CREATE SEQUENCE</command> is a <productname>Postgres</productname>
language extension.
- </PARA>
-
- <REFSECT2 ID="R2-SQL-CREATESEQUENCE-4">
- <REFSECT2INFO>
- <DATE>1998-09-11</DATE>
- </REFSECT2INFO>
- <TITLE>
+ </para>
+
+ <refsect2 id="R2-SQL-CREATESEQUENCE-4">
+ <refsect2info>
+ <date>1998-09-11</date>
+ </refsect2info>
+ <title>
SQL92
- </TITLE>
- <PARA>
+ </title>
+ <para>
There is no <command>CREATE SEQUENCE</command> statement
- in <acronym>SQL92</acronym>.
- </PARA>
+ in <acronym>SQL92</acronym>.
+ </para>
</refsect2>
</refsect1>
-</REFENTRY>
-
+</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
-sgml-omittag:t
+sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t