oracle–触发器+序列实现自增

 1 create table test_table( 2 ID NUMBER PRIMARY KEY, 3 NAME VARCHAR2(10), 4 NICKNAME VARCHAR2(10) 5 ) 6  7 create sequence SEQ_TEST_TRIGGER 8 minvalue 1 9 maxvalue 99999999999999999999999999910 start with 111 increment by 1;12 13 create trigger test_trigger14 before insert on test_table15 for each row16 when (new.ID is null)17 begin18 select SEQ_TEST_TRIGGER.nextval19 into :new.ID20 from dual;21 end;22 23 24 create trigger tri_test_id25 before insert on test_table --S_Depart 是表名26 for each row27 declare28 nextid number;29 begin30 IF :new.ID IS NULL or :new.ID=0 THEN --DepartId是列名31 select SEQ_TEST_TRIGGER.nextval --S_S_DEPART正是刚才创建的32 into nextid33 from dual;34 :new.ID:=nextid;35 end if;36 end;

两种create  trigger方式,创建完成后,就可以insert数据了

相关文章