首先,创建一张表:

 

create table test(
   id number(32) not null primary key,
   name varchar2(32)
);

 

CREATE SEQUENCE test_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE -- 不建缓冲区

 

CREATE TRIGGER test_trigger
   BEFORE INSERT ON test
   FOR EACH ROW
   WHEN (new.id is null) --只有在id为空时,启动该触发器生成id号
begin
   select test_sequence.nextval into :new.id from sys.dual;
end;


insert into test(name) values('zhangsan');
insert into test(id, name) values(101,'lisi');

 

\

 

PS:创建trigger时into :new.id from sys.dual中的":"是靠近new的 而不是into: new.id from sys.dual