mysql设置自动增长序列sequence

create table tb_sequence(name varchar(50) not null,current_value int not null,_increment int not null default 1, primary key(name));

 =========================================

insert into tb_sequence values('ENUMERATE_ID',100,1);

 =========================================

DELIMITER //  
create function _nextval(n varchar(50)) returns integer   
begin  
declare _cur int;  
set _cur=(select current_value from tb_sequence where name= n);  
update tb_sequence  
 set current_value = _cur + _increment  
 where name=n ;  
return _cur;  
end;  
//

 =========================================

执行完毕以上几步之后,就可以执行以下sql进行验证了

扫描二维码关注公众号,回复: 1172468 查看本文章
select _nextval('ENUMERATE_ID');

 第一次结果是100,之后每次查询都会自动加1。

每次增加多少取决于第二次的insert into tb_sequence values('ENUMERATE_ID',100,1),这个1,如果你设成2,那么每次查询之后就增加2。设置不同的name值,为不同的表业务使用。

猜你喜欢

转载自minyongcheng.iteye.com/blog/2150206