The use of SEQUENCES in Oracle

Oracle provides the sequence object, and the system provides the self-incrementing sequence number, which is usually used to generate the self-incrementing primary key or sequence number of the database data record.
  The following introduces common operations such as sequence generation, modification, and deletion:
 1. Create a Sequence Use the following command to create a new sequence (the user needs to have the CREATE SEQUENCE or CREATE ANY SEQUENCE permission):
CREATE SEQUENCE test_sequence
INCREMENT BY 1 -- data added each time
START WITH 1 -- count from 1
NOMAXVALUE -- do not set the maximum value
NOCYCLE -- keep accumulating, not looping
CACHE 10 ; [Note] If the CACHE value is set, ORACLE will pre-place some sequences in memory to make access faster. After the cache is finished, Oracle automatically fetches another set to the cache.
However, using the cache may skip numbers. When the database is suddenly shut down abnormally (shutdown abort), the sequence in the cache will be lost. Therefore, it is recommended to create a sequence when
Use the nocache option.
 2. Use sequence:
sequence.CURRVAL -- returns the current value of the sequence
sequence.NEXTVAL -- increment the sequence value, then return the sequence value
[Note] The first NEXTVAL returns the initial value; subsequent NEXTVAL will automatically increase the INCREMENT BY value you defined, and then return the increased value.
CURRVAL always returns the value of the current SEQUENCE, but CURRVAL can only be used after the first NEXTVAL initialization, otherwise an error will occur.
A NEXTVAL will increment the value of SEQUENCE once, so if you use multiple NEXTVALs in the same statement, the values ​​will be different.
The sequence is stored in the data dictionary and stored in the user_sequences table LAST_NUMBER is the final sequence number, which is the current position of the sequence cursor.
//get sequence last_number SELECT LAST_NUMBER FROM USER_SEQUENCES WHERE SEQUENCE_NAME=TEST_SEQNAME
// NEXTVAL makes the cursor point to the next bit (increase or decrease by one) SELECT SEQNAME.NEXTVAL FROM USER_SEQUENCES Get the value of the next cursor
 3. Modify Sequence Users must have the ALTER ANY SEQUENCE permission to modify the sequence.
You can alter all sequence parameters except start to. If you want to change the start value, you must drop the sequence and re-create.
The command format is as follows: ALTER SEQUENCE test_sequence
INCREMENT BY 10
MAXVALUE 10000
CYCLE -- start from scratch after 10000
NOCACHE ;
4. 删除 Sequence DROP SEQUENCE order_seq;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326613898&siteId=291194637