快速创造百万测试数据

首先创建一个表
drop table if exists score;
create table score(
id int primary key auto_increment,
name varchar(50),
phone varchar(11),
score tinyint(1)
);

目标:
1)造1000000数据
2)名字,手机号,分数各不相同

两种方法
第一种使用insert命令,复制插入
select into score (name,phone,score) select name,phone,score from score;
数据插入完成后,修改重复数据 update
修改name,利用id的自增长来生成新的name
update score set name=concat('tom',id);
修改phone
update phone set phone=13800000000+id
修改score,score本身是整型,所以数据随机生成以后会自动截取
update score set score=rand()*100


第二种
创建存储过程 ---》功能 ---》使用存储过程---》查询

#如果这个存储过程存在,先删除
drop procedure if exists batchInsert;
#创建存储过程
create procedure batchInsert();
begin
#定义一个循环初始变量
declare i int;
#定义一个手机号变量
declare _phone char(11)
#定义一个昵称变量
declare _name varchar(10)
#定义一个分数变量
declare _score int(11);
#设置初始变量值为1
set i=1;
while i<=1000000 do
#修改手机号 使用加法
_phone=13800000000+i;
#修改昵称 使用concat函数连接字符串,可以传多个字符串拼接
_name=concat("Rose_",i);
#修改分数 使用rand函数随机生成
_score=rand()*100;
#使用插入一条记录命令
insert into score (name,phone,score) values (_name,_phone,_score);
#让id递增
set i=i+1
end while
end

#调用存储过程
--call bathInsert();

--select count(1) from score;

猜你喜欢

转载自www.cnblogs.com/ss1311/p/12404746.html