[Reprint] Usage and instance requirements of SEQUENCE in Oracle database

In Oracle database, what is a sequence? What is it used for? Sequence (SEQUENCE) is actually a sequence number generator, which can automatically generate sequence numbers for rows in a table and generate a set of equally spaced values ​​(types are numbers). Its main purpose is to generate a table's primary key value, which can be referenced in an insert statement, as well as checking the current value through a query, or incrementing the sequence to the next value.

Creating sequences requires the CREATE SEQUENCE system privilege. The sequence creation syntax is as follows:

CREATE SEQUENCE sequence name [INCREMENT BY n] [START WITH n] [{MAXVALUE/ MINVALUE n|NOMAXVALUE}] [] [{CACHE n|NOCACHE}];

INCREMENT BY is used to define the step size of the sequence , if omitted, it defaults to 1, and if a negative value appears, the value of the sequence is decremented by this step.

START WITH defines the initial value of the sequence (that is, the first value produced), which defaults to 1.

MAXVALUE defines the maximum value that the sequence generator can produce. The option NOMAXVALUE is the default option, which means that there is no maximum value defined. At this time, for the increasing sequence, the maximum value that the system can generate is 10 to the 27th power; for the decreasing sequence, the maximum value is -1.

MINVALUE defines the minimum value that the sequence generator can produce. The option NOMAXVALUE is the default option, which means that there is no minimum value defined. At this time, for the descending sequence, the minimum value that the system can generate is ~10 to the 26th power; for the increasing sequence, the minimum value is 1.

CYCLE and NOCYCLE indicate whether to cycle when the value of the sequence generator reaches the limit. CYCLE stands for cycle and NOCYCLE stands for no cycle. If looping, when the increasing sequence reaches the maximum, loop to the minimum; for the decreasing sequence when the minimum is reached, loop to the maximum. If you don't loop, after reaching the limit value and continuing to generate new values, an error will occur.
CACHE (buffer) defines the size of the memory block that stores the sequence, the default is 20. NOCACHE means no memory buffering of sequences. Memory buffering of sequences can improve sequence performance.

The syntax for deleting a sequence is:

DROP SEQUENCE sequence name;

where:

the person who deletes the sequence should be the creator of the sequence or a user with the DROP ANY SEQUENCE system privilege. A sequence cannot be referenced once it has been deleted.

Some parts of the sequence can also be modified in use, but not the SATRT WITH option. Modifications to the sequence only affect the sequence numbers that are subsequently generated, and the sequence numbers that have already been generated remain unchanged.
The syntax for modifying a sequence is as follows:

Create and delete a sequence

Example 1: Create a sequence:

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

Execution result: The

sequence has been created.

Step 2: Delete the sequence:

DROP SEQUENCE ABC;

Execution result: The

sequence has been dropped.

Description: The sequence created above is named ABC, which is an increasing sequence, the increment is 1, and the initial value is 10. The sequence does not loop and does not use memory. No minimum value is defined, the default minimum value is 1 and the maximum value is 9 999 999.

Use of Sequences

If you have already created a sequence, how can you refer to the sequence? The method is to use CURRVAL and NEXTVAL to refer to the value of the sequence.

In the process of numbering, there are various reasons for gaps. If a stored procedure picks a number from a sequence, makes it a local variable, but never uses it, the number is lost. It will no longer be able to return to the original sequence, resulting in a gap in the sequence of values. You don't have to worry about this in the relational database model. But sometimes people care about this, and these people want to know which numbers are missing.

Calling NEXTVAL will generate the next sequence number in the sequence. The sequence name should be indicated when calling, that is, it is called in the following way:

sequence name. NEXTVAL

CURRVAL is used to generate the current value of the sequence, no matter how many times it is called, it will not generate the next sequence. value. If the sequence has not yet produced the next value of the sequence by calling NEXTVAL, there is no point in quoting CURRVAL first. The method of calling CURRVAL is the same as above. To indicate the sequence name, call it in the following way:

sequence name.CURRVAL.Generate

the value of the sequence.

Step 1: Generate the first value of the sequence:

SELECT ABC.NEXTVAL FROM DUAL;

Execution result:

NEXTVAL ------------------ 10

Step 2: Generate the next value of the sequence :

SELECT ABC.NEXTVAL FROM DUAL;

execution result:

NEXTVAL ------------------- 11

The current value of the generated sequence:

SELECT ABC.CURRVAL FROM DUAL;

execution result:

CURRVAL - ------------------- 11
Description: The first call to NEXTVAL produces the initial value of the sequence, which is known to be 10 by definition. The second call yields 11 because the sequence has a stride of 1. Call CURRVAL, display the current value of 11, do not generate a new value. Oracle's analytic functions provide a much faster way to check for gaps. They allow you to use full, set-oriented SQL processing while still seeing the value of the next row (LEAD) or previous row (LAG).




Example analysis: Ensure serial numbers are consecutive.
Sometimes, such as on the financial document number, the system must ensure that the serial number must be consecutive. At this time, if this sequence is used as the generation mechanism of the serial number, can all the serial numbers be consecutive? In order to ensure this purpose, the CACHE parameter needs to be set.
  The CACHE parameter is mainly used to specify the number of sequence numbers that can be pre-allocated in the cache. What does this mean? That is, if this number is 20 (the default is 20), then Oracle will automatically generate 20 serial numbers and put them in the cache when it starts. When the application needs to use it, it can be used immediately from the cache. Thereby, the performance of the application can be improved. When the 20 serial numbers are used up, the database will generate another 20 serial numbers and store them in the cache of the database. Obviously, this design is to improve the performance of the database. But there is also a side effect. For example, the system has generated 20 serial numbers (from 1991 to 2010) in the cache. In fact, only 15 were used (for example, in 2005). At this point, if the database needs to be restarted for maintenance or other reasons.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326422337&siteId=291194637