mysql自定义生成随机主键函数

DELIMITER $$

USE test $$

DROP FUNCTION IF EXISTS `random_primary`$$

CREATE FUNCTION `rand_string`(n INT) RETURNS VARCHAR(255) CHARSET latin1
BEGIN
    DECLARE chars_str VARCHAR(100) DEFAULT 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    DECLARE return_str VARCHAR(255) DEFAULT '';
    DECLARE i INT DEFAULT 0;
    WHILE i < n DO
        SET return_str = CONCAT(return_str,SUBSTRING(chars_str , FLOOR(1 + RAND()*62 ),1));
        SET i = i +1;
    END WHILE;
    RETURN return_str;
END$$

你可能感兴趣的:(Mysql)