日期:2014-05-16 浏览次数:20657 次
DROP TABLE IF EXISTS `seq`; CREATE TABLE `seq` ( `id` BIGINT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL COMMENT 'sequence名称', `max` BIGINT(11) NOT NULL DEFAULT '1' COMMENT '最大id', `length`INT(2) NOT NULL DEFAULT '1' COMMENT '生成序列后的长度,以0补全', `next` INT(2) NOT NULL DEFAULT '1' COMMENT '增长的长度', `rules` VARCHAR(255) DEFAULT NULL COMMENT '规则以###max_id###做为替换', PRIMARY KEY (`id`), UNIQUE KEY `fk_name` (`name`) ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
DROP FUNCTION IF EXISTS nextval; DELIMITER $ CREATE FUNCTION nextval (seq_name VARCHAR(50)) RETURNS VARCHAR(20) CONTAINS SQL BEGIN UPDATE seq SET `max` = `max` + NEXT WHERE NAME = seq_name; RETURN bj(seq_name); END$ DELIMITER ;
INSERT INTO `seq` (`name`,`max`,`length`,`next`,`rules`) VALUES ('complaint_seq_b', '0', '7', '1', 'B###max_id###' ); INSERT INTO `seq` (`name`,`max`,`length`,`next`,`rules`) VALUES ('complaint_seq_a', '0', '7', '1','A###max_id###');
SELECT nextval('complaint_seq_b'); SELECT nextval('complaint_seq_a');