-- 用户表
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(30) DEFAULT NULL,
`age` int DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_age` (`age`)
) ;
-- 随机字符串函数
# SET GLOBAL log_bin_trust_function_creators = 1;
CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255)
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;
-- 存储过程
CREATE PROCEDURE `add_user`(IN n int)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= n ) DO
INSERT into user (name,age,create_time ) VALUEs (rand_string(20), FLOOR(RAND() * 100) ,now() );
set i=i+1;
END WHILE;
END;
-- 调用
CALL add_user(100000)