转自:https://www.aliyun.com/jiaocheng/1396184.html

首先,建立部门表和员工表:

部门表:

  1.  
    create table dept(
  2.  
    id int unsigned primary key auto_increment,
  3.  
    deptno mediumint unsigned not null default 0,
  4.  
    dname varchar(20) not null default "",
  5.  
    loc varchar(13) not null default ""
  6.  
    )ENGINE=INNODB DEFAULT CHARSET=GBK

员工表:

  1.  
    create table emp(
  2.  
    id int unsigned primary key auto_increment,
  3.  
    empno mediumint unsigned not null default 0,
  4.  
    ename varchar(20) not null default "",
  5.  
    job varchar(9) not null default "",
  6.  
    mgr mediumint unsigned not null default 0,
  7.  
    hiredate date not null,
  8.  
    sal decimal(7,2) not null,
  9.  
    comm decimal(7,2) not null,
  10.  
    deptno mediumint unsigned not null default 0
  11.  
    )ENGINE=INNODB DEFAULT CHARSET=GBK

开启二进制日志:

通过

show variables like 'log_bin_trust_function_creators'

可以看到默认的二进制日志时关闭的

  1.  
    Variable_name |Value |
  2.  
    --------------------------------|------|
  3.  
    log_bin_trust_function_creators |OFF |

通过下面的命令进行设置:

set global log_bin_trust_function_creators=1
  1.  
    Variable_name |Value |
  2.  
    --------------------------------|------|
  3.  
    log_bin_trust_function_creators |ON |

1.创建一个生成随机字符串的函数:

  1.  
    DELIMITER $$
  2.  
    create FUNCTION rand_string(n int) returns varchar(255)
  3.  
    BEGIN
  4.  
    declare chars_str varchar(100) default 'abcdefghijklmnoprstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  5.  
    declare return_str varchar(255) default '';
  6.  
    declare i int default 0;
  7.  
    while i<n DO
  8.  
    set return_str=CONCAT(return_str,substring(chars_str,floor(1+rand()*52),1));
  9.  
    set i=i+1;
  10.  
    end while;
  11.  
    return return_str;
  12.  
    END $$

通过DELIMITER定义mysql语句的结束符,因为函数中多处有分号,如果不修改掉默认的结束符;那么函数将会产生错误

随后,创建生成随机数的函数:

  1.  
    delimiter $$
  2.  
    create function rand_num() RETURNS int(5)
  3.  
    begin
  4.  
    declare i int default 0;
  5.  
    set i=floor(100+rand()*10);
  6.  
    return i;
  7.  
    end $$

插入员工表函数:

  1.  
    delimiter $$
  2.  
    create procedure insert_emp(IN START INT(10),IN max_num int(10))
  3.  
    begin
  4.  
    declare i int default 0;
  5.  
    set autocommit=0;
  6.  
    repeat
  7.  
    set i=i+1;
  8.  
    insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values((start+i),rand_string(6),'SALESMAN',0001,CURDATE(),2000,400,rand_num());
  9.  
    until i=max_num
  10.  
    end repeat;
  11.  
    commit;
  12.  
    end $$

插入部门的函数:

  1.  
    delimiter $$
  2.  
    create procedure insert_dept(IN START INT(10),IN max_num INT(10))
  3.  
    begin
  4.  
    declare i int default 0;
  5.  
    set autocommit=0;
  6.  
    repeat
  7.  
    set i=i+1;
  8.  
    insert into dept(deptno,dname,loc) values((start+i),rand_string(10),rand_string(8));
  9.  
    until i=max_num
  10.  
    end repeat;
  11.  
    commit;
  12.  
    end $$
  13.  
    end $$

调用上边的两个插入函数

call insert_dept(100,10);
  1.  
    +----+--------+------------+----------+
  2.  
    | id | deptno | dname | loc |
  3.  
    +----+--------+------------+----------+
  4.  
    | 1 | 101 | OVnDatOMsA | BbGYWOaO |
  5.  
    | 2 | 102 | PHQffkLYGl | mEgXmxza |
  6.  
    | 3 | 103 | IljlEhcRXn | xvJjUSGz |
  7.  
    | 4 | 104 | EwuFUElxBk | zNrtSdVl |
  8.  
    | 5 | 105 | vtHaksNIb | mdGUBVar |
  9.  
    | 6 | 106 | FamifbRZyr | ljmJDQso |
  10.  
    | 7 | 107 | tYLKrAAbHd | ziuuQRKZ |
  11.  
    | 8 | 108 | SpXNXzEvmc | ByJZUzNo |
  12.  
    | 9 | 109 | hXlpSoXPfj | HDUNcbT |
  13.  
    | 10 | 110 | sfBoRlLUlA | OtCCdPIU |
  14.  
    +----+--------+------------+----------+

在员工表中插入50W条数据,

mysql> call insert_emp(100001,500000);
这样就实现了大量数据的插入。

最新文章

  1. C# 用原生JS进行文件的上传
  2. Scrum会议3
  3. MongoVUE的使用
  4. hdu 1885 Key Task(bfs+位运算)
  5. IDL绘制黑体辐射曲线
  6. xUtils3源码分析(一):view的绑定
  7. rsyslog Properties 属性:
  8. Lucene.net常见功能实现知识汇总
  9. C#/VB.NET 给Word文档添加/撤销书签
  10. 09-JS的事件流的概念(重点)
  11. OkHttp的缓存
  12. Spring Security测试代码
  13. PHP计算两个经纬度地点之间的距离
  14. python 面向对象编程 之 反射
  15. Python3基础 while 阶乘
  16. linux系统管理的基本命令
  17. 等比例缩放图片(C#)
  18. 转:攻击JavaWeb应用[7]-Server篇[1]
  19. 子域名扫描器 - aquatone
  20. Python ssh 远程执行shell命令

热门文章

  1. url取值乱码问题,url加中文导致页面不能加载问题 js unicode转码,以及解码
  2. kvm virt-install 使用小结
  3. css 样式 解释
  4. Linux下的find命令
  5. BOM对象和DOM对象
  6. 18.match_phrase的用法
  7. vim学习2-文档编辑
  8. 【codeforces 796D】Police Stations
  9. Python之路【第一篇】:Python基础1
  10. [bzoj3524/2223][Poi2014]Couriers_主席树