In oracle, sequence is the so-called serial number, which is automatically increased every time it is obtained. It is generally used in places where the sequence numbers need to be sorted.
Assume that there is such a table:
create table S_Depart ( DepartId INT not null, DepartName NVARCHAR2(40) not null, DepartOrder INT default 0, constraint PK_S_DEPART primary key (DepartId));
In oracle, sequence is the so-called serial number, which is automatically increased every time it is obtained. It is generally used in places where the sequence numbers need to be sorted.
1. Create Sequence
First, you must have the create sequence or create any sequence permission,
Create sequence emp_sequence
Increment by 1 -- add several
Start with 1 -- count from 1
NOMAXvalue -- do not set the maximum value
NOCYCLE -- always accumulate without repeating
CACHE 10; -- set the cache sequence. If the system goes down or otherwise, the sequence will be discontinuous. You can also set it to --------- NOCACHE.
The sequence created for S_Depart is as follows:
create sequence S_S_DEPARTminvalue 1maxvalue 999999999999999999999999999start with 1increment by 1nocache;
Once emp_sequence is defined, you can use CURRVAL, NEXTVAL
CURRVAL = returns the current sequence Value
NEXTVAL = increase the sequence value, and then return the sequence Value
For example:
emp_sequence.CURRVAL emp_sequence.NEXTVAL
Where sequence can be used:
-SELECT statements that do not contain subqueries, snapshot, and VIEW
-The INSERT statement is in the subquery.
-In the value of the NSERT statement
-UPDATE in SET
See the following example:
insert into S_Depart(departId,Departname,Departorder)values(S_S_Depart.Nextval,'12345',1);
SELECT empseq. currval from dual;
Note that:
-The first NEXTVAL returns the initial value. The subsequent NEXTVAL automatically increases the value of your defined increment by and then returns the added value. CURRVAL always returns the value of the current SEQUENCE, but CURRVAL can be used only after the first NEXTVAL initialization; otherwise, an error will occur. NEXTVAL increases the SEQUENCE value once. Therefore, if you use multiple NEXTVAL values in the same statement, their values are different. Understand?
-If the CACHE value is specified, ORACLE can place some sequence in the memory in advance, so that the access speed is faster. After the cache is obtained, oracle automatically retrieves another group to the cache. The cache may be skipped. For example, if the database suddenly fails to be shut down (shutdown abort), the sequence in the cache will be lost. Therefore, nocache can be used to prevent this situation when creating sequence.
2. Alter Sequence
You are either the owner of the sequence, or you have the alter any sequence permission to modify the sequence. you can alter all sequence parameters except start. if you want to change the start value, you must drop sequence and re-create.
Alter sequence example
Alter sequence emp_sequence increment by 10 MAXvalue 10000 CYCLE -- start NOCACHE from the beginning after 10000;
Initialization parameters that affect Sequence:
SEQUENCE_CACHE_ENTRIES = sets the number of sequence that can be simultaneously cached.
It's easy to Drop Sequence.
Drop sequence order_seq;
A simple example:
Create SequenceSEQ_ID
Minvalue1
Maxvalue99999999
Start With1
Increment By1
Nocache
Order;
The code of the producer is:
Create or replace trigger tri_test_id before insert on S_Depart -- S_Depart is the table name for each rowdeclare nextid number; begin IF: new. departId IS NULLor: new. departId = 0 THEN -- DepartId is the column name select SEQ_ID.nextval -- SEQ_ID, which is exactly the into nextid from sys. dual;: new. departId: = nextid; end if; end tri_test_id;
OK. The above code can be used to implement the auto-increment function.
The above describes how to create an auto-increment field in Oracle. For more information, see the PHP Chinese website (www.php1.cn )!