Oracle 数据库——创建序列和触发器命令

时间:2021-09-13 05:12:32

创建序列SQL语句:

CREATE SEQUENCE TABLENAME_SEQ
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT by 1
NOCACHE;


创建触发器SQL语句:

CREATE OR REPLACE TRIGGER "TABLENAME_TRI" BEFORE
INSERT ON jxy_program.TABLENAME FOR EACH ROW
declare
next_checkup_no number;
begin
select TABLENAME_SEQ.nextval
into next_checkup_no
from dual;
:NEW.ID := next_checkup_no;
end;


先建序列,再建触发器。这样在保存新记录时,主键ID会自动增加。