Mysql测试批量数据生成脚本

一、前言
在开发过程中经常会碰到去预演一些组件或者技术,常常会用到网DB数据库中插入批量数据。本文主要目的为了记录插入Mysql中测试数据的方式。

二、操作脚本

点击查看代码
-- 创建测试数据库;
create database tuoguan_db;

-- 创建测试表
CREATE TABLE `employees` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(24) NOT NULL DEFAULT '' COMMENT '姓名',
 `age` int(11) NOT NULL DEFAULT '0' COMMENT '年龄',
 `position` varchar(20) NOT NULL DEFAULT '' COMMENT '职位',
 `hire_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '入职时间',
 PRIMARY KEY (`id`),
 KEY `idx_name_age_position` (`name`,`age`,`position`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='员工记录表';


-- 手工导入几条测试数据
INSERT INTO employees(name,age,position,hire_time) VALUES('LiLei',22,'manager',NOW());
INSERT INTO employees(name,age,position,hire_time) VALUES('HanMeimei', 23,'dev',NOW());
INSERT INTO employees(name,age,position,hire_time) VALUES('Lucy',23,'dev',NOW());


-- 通过脚本插入一些测试数据,一百万条数据
drop procedure if exists insert_emp;
delimiter ;;
create procedure insert_emp()       
begin
  declare i int;                   
  set i=1;                         
  while(i<=1000000)do                
    insert into employees(name,age,position) values(CONCAT('testvalue',i),i,'dev'); 
    set i=i+1;                      
  end while;
end;;
delimiter ;
call insert_emp();
posted @ 2023-07-28 16:15  Steven_Jiang  阅读(189)  评论(0编辑  收藏  举报