不同数据库中的主键自增(Mysql,Oracle,SQL Server,DB2,)

时间:2020-11-30 13:53:23

之前在建表的时候写主键自增实现,发现关键字不对,查看文档才知道不同的版本有不同的关键字,在这里总结一下

Mysql: 

   

id bigint  primarykeyAUTO_INCREMENT,


Oracle:

create table tablename{
id number primary key,
....
}


主键Sequence:
create sequense number_sequence
incrementby1 //每次加几个
startwith1 //从1开始计数 (minvalue 1)
nomaxvalue // 不设置最大值 (maxvalue 99999999)
nocycle//不循环
nocache//不建缓冲区 (cache 20)

    主键自增触发器 Trigger:

<pre name="code" class="sql">   create trigger number_increase before
insert on tablename for each each row
begin
select number_sequence.nextval into:New.userid from dual;
end;

 


SQL Server:

 
 id int primary key IDENTITY,

DB2:

  
id integer not null generated always as IDENETITY (start with 1, increment by 1),