让oracle数据库的表的id自动递增

时间:2021-07-16 00:29:16

1、创建递增序列

CREATE SEQUENCE  ID_ADD(序列名称)
INCREMENT BY 1
START WITH 1
MINVALUE 1
NOMAXVALUE;

2、创建触发器绑定到表上
create or replace trigger ID_ADD_TRI (触发器名称)
before insert on BI_PROVINCENAME(表名)
for each row
declare
-- local variable here
begin
-- Column "ID" uses sequence di_test_id
select ID_ADD.nextval into :new.ID(绑定的字段) from dual;
end ID_ADD_TRI;