oracle实现自增列

时间:2023-03-10 02:02:38
oracle实现自增列

手动创建了一个表格,但是id字段无法实现自增,查看了一下网上的信息,没有找到满意的答案。一下是自己总结摸索的,仅供参考

第一步:手动创建表和列中的字段 (本例中,表明 T_VIDEO,第一个字段:ID)

第二步:创建sequence,如图oracle实现自增列oracle实现自增列

第三步:创建触发器,如图oracle实现自增列

触发器的代码如下

create or replace trigger tri_video before insert
on T_video for each row
declare
integrity_error exception;
errno integer;
errmsg char(200);
dummy integer;
found boolean; begin
select SQ_T_Video.NEXTVAL INTO :new.ID from dual; -- Errors handling
exception
when integrity_error then
raise_application_error(errno, errmsg);
end;

复制上面代码,(1)更改 触发器的名字(本例中是tri_video,可以随意命名)(2)更改 表明(本例中是:T_video),改成你想影响的表明(3)改序列的名字(本例中是,SQ_T_video)(4)更改 要当主键的字段(本例中是 ID ,在New.ID中的id)

然后编译触发器即可。

仅供参考,如果有疑问,请留言