Oracle creates Sequence sequence

202-01-19 Oracle learning creation sequence


One, Oracle version

Oracle    11.2.0.1.0 - 64bit

Tool        SQLPlus

 

Second, what is the sequence

Through learning, I learned that sequence is a kind of Oracle data object. It is a set of sequence numbers that increase or decrease in an orderly manner. The main function is to generate the primary key or unique key of the data table. Sequences do not occupy storage space on the disk and are objects that live in memory.

Create syntax:

CREATE SEQUENCE sequence name

INCREMENT BY [NUM1]

START WITH [NUM2]

MAXVALUE [NUM3] | [NOMAXVALUE]

MINVALUE [NUM4] | [NOMINVALUE]

NOCYCLE | CYCLE

NOCACHE | CACHE [NUM5]

[ORDER];

 

3. Keyword explanation:

1. INCREMENT BY NUM1: The step size is NUM1. NUM1 can be positive or negative, but both must be integers. The default is 1, and it cannot be 0 (0 will lose the meaning of the sequence).

When NUM1 is positive, the sequence value becomes larger and larger, and when it is negative, it becomes smaller.

2. START WITH [NUM2]: Define the initial value or starting value NUM2 of the sequence. This is the first value of the sequence. The default value is 1.

3. MAXVALUE [NUM3] | [NOMAXVALUE]: Define the maximum value NUM3 that the sequence can generate. If the maximum value is not set, the default is NOMAXVALUE. The maximum value of the increasing sequence is said to be 10 to the 27th power; the maximum value of the decreasing sequence is -1.

4. MINVALUE [NUM4] | [NOMINVALUE]: Define the minimum value NUM4 that the sequence can generate. If the minimum value is not set, the default is NOMINVALUE, and the minimum value of the increasing sequence is 1

5. NOCYCLE | [CYCLE]: Define whether to cycle when the value generated by the sequence reaches the maximum. NOCYCLE means no cycle. If a sequence takes a long time to run out, you can consider using a cycle sequence. The start value of the next loop can be viewed in the last_number field in user_sequences.

6. NOCACHE | [CACHE] [NUM5]: Define whether the sequence uses cache and the size of the cache block. The default size is 20. The connection is disconnected and the cache is emptied (I guess, haha).

7. [ORDER]: ORDER option, defines whether the sequence occurs in order, the default is NO. There is no difference in a single-instance database, because the sequence must happen sequentially. In a cluster environment, if cache Cache=20, instance 1 takes 1-20 and stores it in memory; when instance 2 references the same sequence again, it will take 21-40... so the sequence value obtained in multiple instances It will be different. After Order is defined, each instance of the cluster must get the sequence value in order.

 

Four, create an example:

CREATE SEQUENCE S2

INCREMENT BY 2 --Step 2

START WITH 1 --start value 1

MAXVALUE 20 --Maximum value 20

MINVALUE -20 --Minimum value -20

NOCYCLE --No cycle

NOCACHE; --Do not cache

 

Fives,

1. Get the value of the current sequence:

select s2.currval from dual;

2. Get the value of the next sequence:

select s2.nextval from dual;

3. Query what sequences are under the user:

select * from user_sequences;

 

6. After the sequence is created, it can be modified:

1. alter sequence s2 increment by -2; --modified to a decreasing sequence

2. alter sequence s2 maxvalue 100; - modify the maximum value to 100

3. alter sequence s2 cache 30; --modified as a cache, the cache block size is 30, which means that a cache can store up to 30 sequence values ​​in the memory at a time

 

7. Summary of knowledge points:

1. The sequence is a shared call, and all users with reference permission can reference it.

2. When the transaction is rolled back, the data added, deleted and modified in the transaction can be rolled back, but the sequence will not be rolled back! ! !

3. In a table, the field value of the increasing sequence may be interrupted and discontinuous because other objects refer to this sequence, even if it is a reference to a dual virtual table.

4. When NOCACHE is set, each reference sequence needs to be calculated, which will consume performance; for CACHE, it will only be calculated once when caching; if you want to quickly generate sequence values, please increase the CACHE value for actual application scenarios, such as orders number.

5. If you want to use the sequence value as the primary key, the sequence must be defined as NOCYCLE.

 

 

 

Guess you like

Origin blog.csdn.net/Ezreal_XLove/article/details/112850902