在oracle数据库里创建自增ID字段的步骤

在oracle里使用自增id字段的步骤比较繁琐,总结如下:

— 创建表

create table advice
(
id int not null,
active int default 1 not null,
type int not null, 
msg varchar2(512) not null,
advice varchar2(4000) not null,
primary key(id),
constraint advice_uni unique(type,msg)
)tablespace mydb;

--创建自增id,名称为:表名_字段名_seq
create sequence advice_id_seq minvalue 1 nomaxvalue increment by 1 start with 1 nocache;


-- 为insert操作创建触发器,无需在sql语句里写nextval,名称为表名_ins_trg
create or replace trigger advice_ins_trg before insert on advice for each row when(new.id is null)
begin
select advice_id_seq.nextval into :new.id from dual;
end;
(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐