一、创建DDL和DML
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
createDate DATE NOT NULL,
userName VARCHAR(255) NOT NULL,
phone VARCHAR(15),
age INT,
sex CHAR(1), -- 假设'M'代表男,'F'代表女
introduce TEXT
);
INSERT INTO student (createDate, userName, phone, age, sex, introduce) VALUES
('2023-10-23', '张三', '13800138000', 20, 'M', '我是张三,喜欢篮球。'),
('2023-10-22', '李四', '13900139000', 21, 'F', '我是李四,喜欢音乐。'),
('2023-10-21', '王五', '13700137000', 22, 'M', '我是王五,喜欢旅行。'),
('2023-10-20', '赵六', '13600136000', 23, 'F', '我是赵六,喜欢阅读。'),
('2023-10-19', '孙七', '13500135000', 24, 'M', '我是孙七,喜欢摄影。'),
('2023-10-18', '周八', '13400134000', 25, 'F', '我是周八,喜欢画画。'),
('2023-10-17', '吴九', '13300133000', 26, 'M', '我是吴九,喜欢游泳。');
DELIMITER //
二、根据DDL进行生成二十万数据的存储过程
CREATE PROCEDURE InsertRandomStudents(IN num_students INT)
BEGIN
DECLARE counter INT DEFAULT 0;
DECLARE random_username VARCHAR(255);
DECLARE random_phone VARCHAR(15);
DECLARE random_age INT;
DECLARE random_sex CHAR(1);
DECLARE random_introduce TEXT;
-- 设置随机数种子,可选,根据需要进行设置
-- SET @@rand_seed1 = RAND(NOW());
WHILE counter < 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('这是随机生成的介绍:', FLOOR(RAND() * 10000));
-- 插入数据
INSERT INTO student (createDate, userName, phone, age, sex, introduce)
VALUES (CURDATE(), random_username, random_phone, random_age, random_sex, random_introduce);
-- 更新计数器
SET counter = counter + 1;
END WHILE;
END //
DELIMITER ;
CALL InsertRandomStudents(200000);
三、等待几分钟,然后查询
select * from student;
以上内容均是基于VScode中的BaiDu Comate编码助手完成。