(transfer) CURRVAL and NEXTVAL usage in oracle

 Reprinted from: http://blog.csdn.net/qianyiyiding/article/details/51592689

 

1. What is a sequence? what is the function?

In Oracle database , what is a sequence? What is the role of it? In fact, sequence is 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 the primary key value of the table, which can be referenced in the insert statement. Before inserting, obtain the serial number nextval value, and then insert it. The current value can also be checked with a query, or the sequence can be incremented to the next value.

2. How to define a sequence?

  In oracle , the sequence is the so-called sequence number, which will automatically increase each time it is fetched. It is generally used in places that need to be sorted by sequence number or in actual development. For example, the requirement ID in a requirement table is the unique primary key. Use sequence to get it. 
  First, before using the Oracle serial number, we first have to create a sequence and then we can get the current value of the returned sequence and the value of the next sequence in the current table through CURRY and NEXTVAL. Squence can be created by the following statement:

create sequence INR_REQUIRMENT_SQUENCE    
INCREMENT BY 1 -- 每次加几个  
START WITH 1 -- 从1开始计数  
NOMAXVALUE -- 不设置最大值  
NOCYCLE -- 一直累加,不循环  
CACHE 10;  

The above statement creates an INR_REQUIRMENT_SQUENCE sequence

3. How to get the value of a sequence?

  SELECT INR_REQUIRMENT_SQUENCE.CURRVAL FROM dual – Get the current sequence value, 
   the first NEXTVAL returns the initial value; the 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 sequence value once, so if you use multiple NEXTVALs in the same statement.

4. Where can Sequence be used?

  • SELECT statement without subqueries, snapshots, VIEWs
  • In a subquery of an INSERT statement
  • In VALUES of NSERT statement
  • In the actual development of the SET of UPDATE 
    , for example, the primary key in a table needs to obtain the primary key ID first, and NEXTVAL can be used to obtain the next sequence value, and then perform other operations.

5. How to delete Sequence?

  DROP sequence policy_id_seq; –policy_id_seq is the name of the sequence. If you want to change the initial value in the sequence, you must delete the sequence number and then create it. You are either the owner of the sequence, or you have the ALTER ANY sequence permission to change the sequence, and update the sequence directly with the Alter statement.

 

In oracle, the sequence is the sequence number, and it will automatically increase each time it is fetched. Sequences have nothing to do with tables. 

1、Create Sequence

    First, you must have the CREATE SEQUENCE or CREATE ANY SEQUENCE permission.

    The creation statement is as follows: 

 

 
CREATE  SEQUENCE seqTest
INCREMENT 
BY   1   --  每次加几个
START  WITH   1   --  从1开始计数
NOMAXvalue  --  不设置最大值
NOCYCLE  --  一直累加,不循环
CACHE  10 -- 设置缓存cache个序列,如果系统down掉了或者其它情况将会导致序列不连续,也可以设置为---------NOCACHE
 

 

 

2、得到Sequence值 

定义好sequence后,你就可以用currVal,nextVal取得值。
    CurrVal:返回 sequence的当前值 
    NextVal:增加sequence的值,然后返回 增加后sequence值 

  得到值语句如下:
SELECT  Sequence名称.CurrVal  FROM  DUAL; 

  如得到上边创建Sequence值的语句为:

select  seqtest.currval  from  dual

 

 

在Sql语句中可以使用sequence的地方: 
    - 不包含子查询、snapshot、VIEW的 SELECT 语句 
    - INSERT语句的子查询中 
    - INSERT语句的values中 
    - UPDATE 的 SET中

如在插入语句中

 

insert   into  表名(id,name) values (seqtest.Nextval, ' sequence 插入测试 ' );

 

 

 注:

    - 第一次NEXTVAL返回的是初始值;随后的NEXTVAL会自动增加你定义的INCREMENT BY值,然后返回增加后的值。

      CURRVAL 总是返回当前SEQUENCE的值,但是在第一次NEXTVAL初始化之后才能使用CURRVAL,否则会出错。

       一次NEXTVAL会增加一次 SEQUENCE的值,所以如果你在同一个语句里面使用多个NEXTVAL,其值就是不一样的。
    - 如果指定CACHE值,ORACLE就可以预先在内存里面放置一些sequence,这样存取的快些。cache里面的取完后,oracle自动再取一组 到cache。 使用cache或许会跳号, 比如数据库突然不正常down掉(shutdown abort),cache中的sequence就会丢失. 所以可以在create sequence的时候用nocache防止这种情况。

 

 

 

 

3、Alter Sequence 
    拥有ALTER ANY SEQUENCE 权限才能改动sequence. 可以alter除start至以外的所有sequence参数.如果想要改变start值,必须 drop sequence 再 re-create。

 

例:

alter  sequence SEQTEST maxvalue  9999999 ;
    另: SEQUENCE_CACHE_ENTRIES参数,设置能同时被cache的sequence数目。

 

4、Drop Sequence
DROP  SEQUENCE seqTest; 

 

 

5、一个例子

 

 
create  sequence SEQ_ID
minvalue 
1
maxvalue 
99999999
start 
with   1
increment 
by   1
nocache
order ;

建解发器代码为:

create   or   replace   trigger  tri_test_id
  before 
insert   on  S_Depart    -- S_Depart 是表名
   for  each row
declare
  nextid 
number ;
begin
  
IF  :new.DepartId  IS  NULLor :new.DepartId = 0   THEN   -- DepartId是列名
     select  SEQ_ID.nextval  -- SEQ_ID is just created into  nextid from  sys.dual;     :new.DepartId: = nextid; end if ; end  tri_test_id;
    
    


  
 

 

 

     OK, the above code can realize the function of automatic increment.

 

 

 

    Note: :new represents the new value after the data is changed, corresponding to the original value of :old

          := stands for assignment

          :nextid means to refer to the variable defined in sqlplus 

                      

Guess you like

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