CREATE TABLE sys_sequence (
  seq_name varchar(20) unique not null,
  seq_current int unsigned not null,
  increment_val INT NOT NULL DEFAULT 1,
  primary key(seq_name)
);
DROP FUNCTION IF EXISTS `nextval`;
DELIMITER //
create function nextval (v_seq_name VARCHAR(50))  
    returns integer  
begin
    INSERT INTO sys_sequence (seq_name, seq_current) VALUES (v_seq_name, (@next := 1)) ON DUPLICATE KEY UPDATE seq_current = (@next := seq_current + 1); 
    return @next;  
end//
DELIMITER ;
select nextval('foo');

参考资料

https://www.coder.work/article/150330

https://stackoverflow.com/questions/805808/emulating-a-transaction-safe-sequence-in-mysql

[ 编辑 | 历史 ]
最近由“jilili”在“2023-08-20 05:34:38”修改