Sequence: SEQUENCE

First, the sequence of presentation

Oracle database object is a sequence, the main role is used to generate a unique value. Objects can be found in the sequence after sequence data dictionary is created, so the sequence can be shared by multiple objects.

Second, create a sequence

Sequence created using the CREATE SEQUENCE syntax:

CREATE SEQUENCE sequence 
[INCREMENT BY n]
 [START WITH n]
 [{MAXVALUE n | NOMAXVALUE}]
 [{MINVALUE n | NOMINVALUE}]
 [{CYCLE | NOCYCLE}]
 [{CACHE n | NOCACHE}];
  • NCREMENT BY: used to define a sequence of steps, if omitted, it defaults to 1, and if negative, the sequence of the representative value is decreasing.
  • START WITH: initial value (i.e., generating a first value) is defined sequences, the default is one.
  • MAXVALUE: maximum value defined sequence can be generated. NOMAXVALUE is the default option, representing no maximum time, for incrementing the sequence, the system can generate a maximum power of 10 to 27; for a descending sequence, the maximum value is -1.
  • MINVALUE: define the minimum sequence can be generated. NOMINVALUE is the default, not representative of the minimum, then, for a descending sequence, the system can generate the minimum value is a negative power of 10 to 26; for a descending sequence, the minimum is 1.
  • CYCLE and NOCYCLE: sequence generator when the value indicates whether the cycle limit is reached. If the cycle, when the sequence reaches the maximum value is incremented, the minimum cycle; For a descending sequence, at the minimum value, maximum value to the cycle. If not recycled, the limit is reached, continue to generate new value error occurs.
  • CACHE: the size of the memory block storing the sequence defined, the default is 20. NOCACHE not expressed sequence buffer memory. Sequence of memory buffer, can improve the performance sequence.

E.g:

CREATE SEQUENCE invoice_seq
INCREMENT BY 1
START WITH 1
MAXVALUE 9999999
NOCYCLE NOCACHE;

Third, the query sequence

Once the sequence is created, the code sequence was created textual data dictionary, the data can be seen in user_objects dictionary, such as:

SELECT object_name,object_id,object_type  FROM user_objects WHERE object_name = 'INVOICE_SEQ';
Save the sequence detailed information in user_sequences table:

SELECT sequence_name, min_value, max_value, increment_by, last_number  FROM user_sequences;

Fourth, the use of sequence

And dummy column NEXTVAL CURRVAL

  • NEXTVAL: Returns the next available sequence value, which is returned each time a unique reference value, the actual true for different users. When using sequence.NEXTVAL, a new sequence number is generated and the current sequence number is placed CURRVAL.
  • CURRVAL: you get the current sequence value. On the use of CURRVAL before the first use NEXTVAL, it will complain.

Use as follows:

SELECT invoice_seq.CURRVAL,invoice_seq.NEXTVAL FROM DUAL;

INSERT INTO invoice  (invoice_id, vendor_id, invoice_number, invoice_total  )
      VALUES (invoice_seq.NEXTVAL, 10, 'INV' || invoice_seq.CURRVAL, 100  );

CURRVAL NEXTVAL and may be used in the following context:

  • SELECT statement is not part of the sub-query list field.
  • List SELECT INSERT statement neutron query.
  • INSERT statement in the VALUES clause.
  • UPDATE statement SET clause.

Can not be used in the context of the following NEXTVAL and CURRVAL:

  • SELECT list view.
  • DISTINCT with SELECT statement.
  • With GROUP BY, HAVING or ORDER BY clause of the SELECT statement.
  • Clause in SELECT, DELETE, or UPDATE statement.
  • DEFAULT expression in a CREATE TABLE or ALTER TABLE statement.

Also note, ROLLBACK sequence value can not roll back.

Fifth, modify the sequence

Such as:

ALTER SEQUENCE invoice_seq     INCREMENT BY 2 MAXVALUE 10   NOCACHE  NOCYCLE;

When modifying sequence, we have the following limitations:

  • The sequence can start value table.
  • It is not greater than the current minimum value.
  • Not less than the current maximum value.
  • Sequence revised rules will not affect the value of the previous sequence, only the future sequence value will be affected.
  • The user must have the permission of ALTER SEQUENCE.

Six deleted sequence

DROP SEQUENCE invoice_seq;

Guess you like

Origin www.cnblogs.com/springsnow/p/11547425.html