首页 > 数据库技术 > 详细

oracle 实现主键id自增

时间:2019-09-16 18:29:38      阅读:80      评论:0      收藏:0      [点我收藏+]

公司现在项目数据库使用oracle,oracle实现表主键自增比mysql麻烦

mysql 在表主键auto_increment 打钩即可。oracle没有改属性,就相对麻烦。特此记录一下自增方法

测试案例如下

第一步创建一张测试表table1

sql语句

create table table1
(
id int not null,
name varchar2(20),
sex varchar2(4)
)

添加表注释、字段注释

comment on table table1 is ‘测试表 稍后会删除‘
comment on column table1.name is ‘姓名‘
comment on column table1.sex is ‘性别‘

第二步:创建序列

create sequence table1_id
minvalue 1             //自增字段最小值
nomaxvalue           //最大值 没有就算nomaxvalue
increment by 1      //每次增值1
start with 1           //起始值
nocache;             //不缓存

第三步:创建触发器

create or replace trigger table1_tg_insertId
before insert on table1 for each row
begin
select table1_id.nextval into:new.id from dual;
end;

第四步:测试开始  插入两条数据

insert into table1(name,sex) values (‘zhangsan‘,‘nan‘);
insert into table1(name,sex) values (‘lisi‘,‘nan‘);

 

查询数据

技术分享图片

 

oracle 实现主键id自增

原文:https://www.cnblogs.com/prettrywork/p/11528526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!