oracle中的触发器

1.在数据库中更新数据时,有涉及到时间戳字段的,我们可以使用触发器来自动填充时间戳的值:

create or replace trigger timeuser_seq_tr
  before insert or update on timeuser for each row   --table name:timeuser
begin
  if updating then
  :new.modified := SYSTIMESTAMP;   --modified是我们表中的一个字段,   :new.modified表示新的"修改时间"字段的值
  end if;
END;

2.oracle中的id主键自增,需要先创建一个序列,然后再创建触发器,每次插入数据时触发得到递增的id。

创建sequence:

CREATE SEQUENCE WYH_USERS_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE;

创建触发器:

create or replace trigger wyh_users_seq_tr
   before insert or update on wyh_users for each row
begin
   if inserting then
   select wyh_users_seq.NEXTVAL into :new.id from dual;
   end if;
END;

猜你喜欢

转载自blog.csdn.net/QYHuiiQ/article/details/89645427