mysql seq

Submitted by Lizhe on Fri, 05/11/2018 - 10:58

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('userid',0,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 for update);

update tb_sequence set current_value = _cur + _increment where name=n ;

return _cur; end;

//

 

DELIMITER ;

 

 

select _nextval('userid');