文章出处:https://sdu0rj.axshare.com/%E4%BA%8C%E7%BA%A7%E5%AE%A2%E6%88%B7%E7%AE%A1%E7%90%86.html
mysql没有像oracle一样的sequence自增序列,需要我们自己维护一个。
CREATE TABLE sys_sequence ( NAME varchar(50) NOT NULL, CURRENT_VALUE int(11) NOT NULL DEFAULT '0', INCREMENT int(11) NOT NULL DEFAULT '1', PRIMARY KEY (NAME) ); INSERT INTO SYS_SEQUENCE(NAME,CURRENT_VALUE,INCREMENT) VALUES('diablo_seq', 1,1); DROP FUNCTION IF EXISTS fun_currval; DELIMITER $$ CREATE FUNCTION fun_currval(seq_name VARCHAR(50)) RETURNS INT(11) BEGIN DECLARE VALUE INTEGER; SET VALUE=0; SELECT current_value INTO VALUE FROM sys_sequence WHERE NAME=seq_name; RETURN VALUE; END$$ DELIMITER ; DROP FUNCTION IF EXISTS fun_nextval; DELIMITER $$ CREATE FUNCTION fun_nextval(seq_name varchar(50)) RETURNS int(11) BEGIN UPDATE sys_sequence SET CURRENT_VALUE = CURRENT_VALUE + INCREMENT where name=seq_name; return fun_currval(seq_name); END$$ DELIMITER ;