oracle中利用序列和触发器创建自增长的表

来源:互联网 发布:linux怎么进入vim 编辑:程序博客网 时间:2024/06/07 18:29

step1: 创建表

create table test(

   id number,

   name varchar(25)

);

 

step2:创建序列

create sequence seq_test

 increment by 1

 start with 1  

 nomaxvalue   

 nocycle    

 cache 10; 

如果不能创建, 则可能是用户授权的问题, 用sys登录, 执行grant create view to 当前用户名;

执行完成后切换回此用户即可.

 

step3:创建触发器.

create or replace trigger trig_test_autoincre

before insert on test

for each row

 begin

    select seq_test.nextval into :new.id from dual;

 end;

如果不能创建, 则可能是用户授权的问题, 用sys登录, 执行grant create triggle to 当前用户名;

执行完成后切换回此用户即可.

 

step4:测试

insert into test(name) values('abc');

insert into test(name) values('abc');

insert into test(name) values('abc');

step5:测试结果

SQL> select * from test;

        ID NAME
---------- -------------------------
         1 abc
         2 abc
         3 abc

转载自:http://blog.sina.com.cn/s/blog_9191910f01018jvx.html

阅读全文
0 0