建表语句
CREATE TABLE `student` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`createDate` date NOT NULL,
`userName` varchar(255) NOT NULL,
`phone` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`introduce` text,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
插入语句
DELIMITER //
CREATE PROCEDURE InsertRandomStudents(num_students INT)
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE random_username VARCHAR(255);
DECLARE random_phone VARCHAR(20);
DECLARE random_age INT;
DECLARE random_sex CHAR(1);
DECLARE random_introduce TEXT;
-- 设置随机数种子,可根据需要调整
SET @@rand_seed := RANDOM();
WHILE i < num_students DO
-- 生成随机用户名(示例:UserXXXXX)
SET random_username = CONCAT('User', LPAD(FLOOR(RAND() * 100000), 5, '0'));
-- 生成随机电话号码(示例:13800000000到13899999999之间)
SET random_phone = CONCAT('138', LPAD(FLOOR(RAND() * 100000000), 8, '0'));
-- 生成随机年龄(示例:18到30岁之间)
SET random_age = FLOOR(18 + RAND() * 13);
-- 随机分配性别
SET random_sex = IF(RAND() > 0.5, 'M', 'F');
-- 生成随机介绍文本(示例:简单文本)
SET random_introduce = CONCAT('这是用户', random_username, '的介绍。');
-- 插入数据到student表
INSERT INTO student (createDate, userName, phone, age, sex, introduce)
VALUES (CURDATE(), random_username, random_phone, random_age, random_sex, random_introduce);
-- 更新计数器
SET i = i + 1;
END WHILE;
END //
DELIMITER ;
CALL InsertRandomStudents(200000);