oracle触发器实现主键自增
直接上代码解释
CREATE SEQUENCE **"SHINE"."S_USER_DETAIL"** MINVALUE 0 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 **CACHE 20** ORDER NOCYCLE ;
create or replace trigger **T_USER_DETAIL**
before
insert or update
on **USER_DETAIL**
for each row
declare
nextid number;
now_date date;
begin
select systimestamp into now_date from dual;
case
when inserting then
IF :new.ID IS NULL or :new.ID = 0 THEN
select **S_USER_DETAIL**.nextval into nextid from sys.dual;
:new.ID := nextid;
end if;
:new.create_time := now_date;
:new.opetation_time := now_date;
when updating then
:new.opetation_time := now_date;
end case;
end **T_USER_DETAIL**;
“SHINE”."S_USER_DETAIL 数据库.序列化表名(需要自己起一般在原表名前加S)
USER_DETAIL 数据库中原始的表名
T_USER_DETAIL 临时表名(需要自己起一般在原表名前加T)
每张表创建触发器都要改我加粗几个地方,另外如果表中有create_time,opetation_time两个字段,这个触发器设置这两个字段默认值(符合逻辑的)