ddl-sequence.xml 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
<topic id="topic_eys_3wy_sp">
  <title id="im148803">Creating and Using Sequences</title>
  <body>
    <p>You can use sequences to auto-increment unique ID columns of a table whenever a record is
      added. Sequences are often used to assign unique identification numbers to rows added to a
      table. You can declare an identifier column of type <codeph>SERIAL</codeph> to implicitly
      create a sequence for use with a column.</p>
  </body>
  <topic id="topic87" xml:lang="en">
    <title>Creating a Sequence</title>
    <body>
      <p>The <codeph>CREATE SEQUENCE </codeph>command creates and initializes a special single-row
        sequence generator table with the given sequence name. The sequence name must be distinct
        from the name of any other sequence, table, index, or view in the same schema. For
        example:</p>
      <p>
        <codeblock>CREATE SEQUENCE myserial START 101;
</codeblock>
      </p>
    </body>
  </topic>
  <topic id="topic88" xml:lang="en">
    <title>Using a Sequence</title>
    <body>
      <p>After you create a sequence generator table using <codeph>CREATE SEQUENCE</codeph>, you can
        use the <codeph>nextval</codeph> function to operate on the sequence. For example, to insert
        a row into a table that gets the next value of a sequence:</p>
      <p>
        <codeblock>INSERT INTO vendors VALUES (nextval('myserial'), 'acme');
</codeblock>
      </p>
      <p>You can also use the <codeph>setval</codeph> function to reset a sequence's counter value.
        For example:</p>
      <p>
        <codeblock>SELECT setval('myserial', 201);
</codeblock>
      </p>
      <p>A <codeph>nextval</codeph> operation is never rolled back. Afetched value is considered
        used, even if the transaction that performed the <codeph>nextval</codeph> fails. This means
        that failed transactions can leave unused holes in the sequence of assigned values.
          <codeph>setval</codeph> operations are never rolled back. </p>
      <p>Note that the <codeph>nextval</codeph> function is not allowed in <codeph>UPDATE</codeph>
        or <codeph>DELETE</codeph> statements if mirroring is enabled, and the
          <codeph>currval</codeph> and <codeph>lastval</codeph> functions are not supported in
        Greenplum Database.</p>
      <p>To examine the current settings of a sequence, query the sequence table:</p>
      <p>
        <codeblock>SELECT * FROM myserial;
</codeblock>
      </p>
    </body>
  </topic>
  <topic id="topic89" xml:lang="en">
    <title>Altering a Sequence</title>
    <body>
      <p>The <codeph>ALTER SEQUENCE</codeph> command changes the parameters of an existing sequence
        generator. For example:</p>
      <p>
        <codeblock>ALTER SEQUENCE myserial RESTART WITH 105;
</codeblock>
      </p>
      <p>Any parameters not set in the <codeph>ALTER SEQUENCE</codeph> command retain their prior
        settings.</p>
    </body>
  </topic>
  <topic id="topic90" xml:lang="en">
    <title>Dropping a Sequence</title>
    <body>
      <p>The <codeph>DROP SEQUENCE</codeph> command removes a sequence generator table. For
        example:</p>
      <p>
        <codeblock>DROP SEQUENCE myserial;
</codeblock>
      </p>
    </body>
  </topic>
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  <topic id="topic91" xml:lang="en">
    <title>Making a Sequence the Default Value for a Column</title>
    <body>
      <p>A sequence can be used in the <codeph>CREATE TABLE</codeph> command (instead of the
          <codeph>SERIAL</codeph> or <codeph>BIGSERIAL</codeph> type):</p>
      <p>
        <codeblock>CREATE TABLE ... (
            id INT4 DEFAULT nextval('id_seq')
            );
</codeblock>
      </p>
      <p>Alternatively, a column can be altered and a sequence can be added as default value:</p>
        <codeblock>ALTER TABLE ... ALTER COLUMN id SET DEFAULT nextval('id_seq');
</codeblock>
    </body>
  </topic>
95
</topic>