失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Oracle创建自增主键ID(序列+触发器)

Oracle创建自增主键ID(序列+触发器)

时间:2023-02-04 09:49:45

相关推荐

Oracle创建自增主键ID(序列+触发器)

根据需求创建了一张表,主要做插入操作,需要主键自增;把我的建表详情分享如下:

-- Create tablecreate table auto_incre(auto_id NUMBER(10) not null,auto_des VARCHAR2(20),auto_sort VARCHAR2(20))tablespace CREDITpctfree 10initrans 1maxtrans 255storage(initial 64Knext 8Kminextents 1maxextents unlimited);-- Add comments to the tablecomment on table auto_increis '主键自增表';comment on column auto_incre.auto_id is 'id,自增长主键';comment on column auto_incre auto_des is '描述';comment on column auto_incre auto_sort is '类别'; -- Create/Recreate primary,unique and foreign key constraintsalter table auto_increadd constraint PK_auto_incre primary key (auto_id)using indextablespace CREDITpctfree 10initrans 2maxtrans 255storage(initial 64Knext 1Mminextents 1maxextents unlimited);--Create sequencecreate sequence seq_autoincrement by 1 -- 自增1start with 1001 -- 从1001开始nomaxvalue-- 无最大值minvalue 1001 ; -- 最小值1001--创建触发器将表与序列关联create or replace trigger trg_autobefore insert on auto_incre for each rowbegin select seq_auto.nextval into :new.auto_id from dual;end;

触发器已启用

执行插入语句

ID已实现了自增长

-------------------------------

触发器也不是必要的,建序列就可以了, 在编写Insert语句时,使用序列

如上图所示seq_adv是序列名

如果觉得《Oracle创建自增主键ID(序列+触发器)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。