Mysql实现序列化

下面是实现序列化的代码

先建一个序列表,如下:

CREATE TABLE IF NOT EXISTS `sequence` (  
  `name` varchar(50) NOT NULL,  
  `current_value` int(11) NOT NULL,  
  `increment` int(11) NOT NULL DEFAULT ‘1’  
) ENGINE=MyISAM DEFAULT CHARSET=utf8 CHECKSUM=1       DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT=‘序    列表,命名s_[table_name]’;  
INSERT INTO `sequence` (`name`, `current_value`, `increment`) VALUES  
(’s_blog_account’, 0, 1) 
DROP FUNCTION IF EXISTS `curial`;  
DELIMITER //  

CREATE  FUNCTION `curial`(seq_name VARCHAR(50)) RETURNS int(11)  
READS SQL DATA  
DETERMINISTIC  
BEGIN  
DECLARE VALUE INTEGER;  
SET VALUE = 0;  
SELECT current_value INTO VALUE FROM sequence WHERE NAME = seq_name;  
 RETURN VALUE;  
END//  

DELIMITER ;
DROP FUNCTION IF EXISTS `nextval`;  
DELIMITER //  

CREATE  FUNCTION `nextval`(seq_name VARCHAR(50))   RETURNS int(11)  
DETERMINISTIC  
BEGIN 
UPDATE sequence SET current_value = current_value + increment WHERE NAME = seq_name;  
RETURN currval(seq_name);  
END//  

DELIMITER ;

没有错误之后就;可以使用了.
如想要实现 id 序列,输入

    alter table tb_user modify id integer auto_increment;

你可能感兴趣的:(Mysql实现序列化)