oracle怎么实现主键自增

本教程操作环境:Windows10系统、Oracle 11g版、Dell G3电脑。

oracle怎么实现主键自增

oracle在创建表的时候不能像mysql那样可以直接选择自增,但是在oracle中我们可以通过给表结构添加序列和触发器,去实现主键自增。

这是PolarDB数据库,但是和Oracle的用法还是很像。

这是新建的表结构,id为主键。

08.png

第一步:创建序列

create sequence sms_id minvalue 1 nomaxvalue  increment by 1  start with 1 nocache;

09.png

序列其他使用方法

查询序列

select sms_id.currval from dual //查询当前id是第几

删除序列

DROP SEQUENCE sms_id;

第二步:创建触发器

create or replace trigger sms_tg_insertId before insert on message.oa_doc_smsinfo  for each row  begin select sms_id.Nextval into:new.id from dual; end;

10.png

触发器其他使用方法

删除触发器

drop trigger sms_tg_insertId

第三步:添加数据测试

运行了三次,id自动增长。

11.png

12.png

推荐教程:《Oracle视频教程》

商匡云商
Logo
注册新帐户
对比商品
  • 合计 (0)
对比
0
购物车