Oracle 11g 实现主键自增

问题:在数据库中,经常要设置主键自增,mysql可以用auto_increment实现,但是oracle数据库没有auto_increment,所以一般Oracle数据库可以使用序列(Sequence)实现
第一步:创建表

 create table test1(
 id number(8) primary key,
 username varchar2(20),
 password varchar2(20)
 );

第二步:创建序列

 create sequence test1_seq
 increment by 1
 start with 1
 maxvalue 1000
 minvalue 1
 nocache
 nocycle

第三步:使用序列

insert into test1 values(test1_seq.nextval, 'zhangsan', '123');

这样在插入数据时就能实现主键的自增。

附加:删除序列

drop sequence test1_seq;

猜你喜欢

转载自blog.csdn.net/weixin_44072535/article/details/108529723