oracle--觸發器+序列實現自增
阿新 • • 發佈:2018-07-12
tid 表名 dual 方式 spa tar num declare end
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 999999999999999999999999999 10 start with 1 11 increment by 1; 12 13 create trigger test_trigger 14 before inserton test_table 15 for each row 16 when (new.ID is null) 17 begin 18 select SEQ_TEST_TRIGGER.nextval 19 into :new.ID 20 from dual; 21 end; 22 23 24 create trigger tri_test_id 25 before insert on test_table --S_Depart 是表名 26 for each row 27 declare 28 nextid number; 29 begin 30 IF:new.ID IS NULL or :new.ID=0 THEN --DepartId是列名 31 select SEQ_TEST_TRIGGER.nextval --S_S_DEPART正是剛才創建的 32 into nextid 33 from dual; 34 :new.ID:=nextid; 35 end if; 36 end;
兩種create trigger方式,創建完成後,就可以insert數據了
oracle--觸發器+序列實現自增