MySQL用随机数据填充表

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

先创建数字辅助表

create table nums(id int not null primary key);

delimiter 
$$

create procedure pCreateNums(cnt int)
begin
    declare s int default 1;
    truncate table nums;
    while s<=cnt do
        insert into nums select s;
        set s=s+1;
    end while;
end 
$$

delimiter ;

delimiter 
$$

create procedure pFastCreateNums(cnt int)
begin
    declare s int default 1;
    truncate table nums;
    insert into nums select s;
    while s*2<=cnt do
        insert into nums select id+s from nums;
        set s=s*2;
    end while;
end 
$$

delimiter ;
call pFastCreateNums(100000);

http://blog.itpub.net/29254281/viewspace-1362897/

然后创建一个生成随机字符的函数

DROP FUNCTION IF EXISTS rand_string; 
delimiter // 
CREATE FUNCTION rand_string(l_num int UNSIGNED,l_type tinyint UNSIGNED) 
RETURNS varchar(2000) 
BEGIN 
 -- Function : rand_string 
 -- Author : dbachina#dbachina.com 
 -- Date : 2010/5/30 
 -- l_num : The length of random string 
 -- l_type: The string type 
 -- 1.0-9 
 -- 2.a-z 
 -- 3.A-Z 
 -- 4.a-zA-Z 
 -- 5.0-9a-zA-Z 
 -- : 
  -- mysql> select rand_string(12,5) random_string; 
  -- +---------------+ 
  -- | random_string | 
  -- +---------------+ 
  -- | 3KzGJCUJUplw  | 
  -- +---------------+ 
  -- 1 row in set (0.00 sec) 
 DECLARE i int UNSIGNED DEFAULT 0; 
 DECLARE v_chars varchar(64) DEFAULT '0123456789'; 
  DECLARE result varchar (2000) DEFAULT ''; 
 
  IF l_type = 1 THEN 
    SET v_chars = '0123456789'; 
  ELSEIF l_type = 2 THEN 
    SET v_chars = 'abcdefghijklmnopqrstuvwxyz'; 
  ELSEIF l_type = 3 THEN 
    SET v_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
  ELSEIF l_type = 4 THEN 
    SET v_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
  ELSEIF l_type = 5 THEN 
    SET v_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
  ELSE 
    SET v_chars = '0123456789'; 
  END IF; 
 
  WHILE i < l_num DO 
      SET result = concat( result,substr(v_chars,ceil(rand()*(length(v_chars)-1)),1) ); 
    SET i = i + 1; 
  END WHILE; 
  RETURN result; 
END; 
// 
delimiter ;

然后执行如下命令

set @table_schema='songod';
set @table_name='test';
set @row_count=10;
set @sql=concat('insert into ',@table_schema,'.',@table_name,' select ');
select 
nullif ('please stand by...',
    @sql:=concat(@sql,
        case 
                when data_type='int' then 'round(rand()*2147483647),'
                when data_type='bigint' then 'round(rand()*9223372036854775807),'
                when data_type='smallint' then 'round(rand()*32767),'
                when data_type='tinyint' then 'round(rand()*127 ),'
                when data_type='varchar' then concat('rand_string(',CHARACTER_MAXIMUM_LENGTH,',5),')
                when data_type='date' then 'now()-interval round(90*rand()) day,'
                when data_type='datetime' then 'now()-interval round(90*rand()) day,'
                when data_type='timestamp' then 'now()-interval round(90*rand()) day,'
        end
    )
) info
from information_schema.columns where table_schema=@table_schema and table_name=@table_name;
set @sql=left(@sql,char_length(@sql)-1);
select nullif ('please stand by...',@sql:=concat(@sql,' from nums where id<=',@row_count,';')) info;
prepare statement from @sql;
execute statement;
commit;

其中

@table_schema是数据库名
@table_name是目标表名
@row_count是生成的行数

这个不支持boolean类型,因为mysql在内部作为tinyint存储.
也有一些业务要求,比如State只有0,1两个取值.

可以在生成数据之后,再批量修改一下。
update test set c2=round(rand());

本文转自ICT时空 dbasdk博客,原文链接:MySQL用随机数据填充表 ,如需转载请自行联系原博主。

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
14天前
|
缓存 NoSQL 关系型数据库
13- Redis和Mysql如何保证数据⼀致?
该内容讨论了保证Redis和MySQL数据一致性的几种策略。首先提到的两种方法存在不一致风险:先更新MySQL再更新Redis,或先删Redis再更新MySQL。第三种方案是通过MQ异步同步以达到最终一致性,适用于一致性要求较高的场景。项目中根据不同业务需求选择不同方案,如对一致性要求不高的情况不做处理,时效性数据设置过期时间,高一致性需求则使用MQ确保同步,最严格的情况可能涉及分布式事务(如Seata的TCC模式)。
37 6
|
21天前
|
SQL 关系型数据库 MySQL
轻松入门MySQL:保障数据完整性,MySQL事务在进销存管理系统中的应用(12)
轻松入门MySQL:保障数据完整性,MySQL事务在进销存管理系统中的应用(12)
|
28天前
|
关系型数据库 MySQL
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
21 0
|
30天前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
20 1
|
30天前
|
Java 关系型数据库 数据库连接
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
Mybatis+MySQL动态分页查询数据经典案例(含代码以及测试)
26 1
|
22天前
|
存储 SQL 关系型数据库
【MySQL】4. 表的操作
【MySQL】4. 表的操作
21 0
|
30天前
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)
15 1
|
30天前
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
21 1
|
30天前
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
Mybatis+mysql动态分页查询数据案例——房屋信息的实现类(HouseDaoMybatisImpl)
22 2
|
21天前
|
存储 关系型数据库 MySQL
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)