1)在mysql中创建一张表 CREATE TABLE tbl_sequence ( name VARCHAR(50) NOT NULL, currentValue INT(11) NULL DEFAULT NULL, increment INT(11) NULL DEFAULT NULL, PRIMARY KEY (name) )
2)创建一个函数 DROP FUNCTION IF EXISTS nextval;
DELIMITER $
CREATE FUNCTION nextval (seq_name VARCHAR(50))
RETURNS INTEGER
BEGIN
DECLARE value INTEGER; SET VALUE = (SELECT currentValue from tbl_sequence where name = seq_name); if (VALUE IS NULL) then SET VALUE = 10000; INSERT INTO tbl_sequence(name, currentValue, increment) value(seq_name, VALUE, 1); END IF; UPDATE tbl_sequence SET currentValue = currentValue + increment WHERE name = seq_name; RETURN value;
END
$
DELIMITER ;
3)生成唯一键 select nextval('20160708');