目录

批量数据脚本

建表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE dept
(
    deptno int unsigned primary key auto_increment,
    dname varchar(20) not null default "",
    loc varchar(8) not null default ""
)ENGINE=INNODB DEFAULT CHARSET=utf8;

CREATE TABLE emp
(
    id int unsigned primary key auto_increment,
    empno mediumint unsigned not null default 0,
    ename varchar(20) not null default "",
    job varchar(9) not null default "",
    mgr mediumint unsigned not null default 0,
    hiredate date not null,
    sal decimal(7,2) not null,
    comm decimal(7,2) not null,
    deptno mediumint unsigned not null default 0
)ENGINE=INNODB DEFAULT CHARSET=utf8;

设置参数

  • 创建函数,假如报错:This function has none of DETERMINISTIC………

  • 由于开启过慢查询日志,因为我们开启了bin-log,我们就必须为我们的function指定一个参数。

    如果没开启bin-log就不用管

    假设开启了bin-log, 我们就必须指定我们的函数是否是

    1 DETERMINISTIC 不确定的

    2 NO SQL 没有SQl语句,当然也不会修改数据

    3 READS SQL DATA 只是读取数据,当然也不会修改数据

    4 MODIFIES SQL DATA 要修改数据

    5 CONTAINS SQL 包含了SQL语句

    其中在function里面,只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。如果我们开启了 bin-log, 我们就必须为我们的function指定一个参数。

    解决方法

    开启log_bin_trust_function_creators

    • log_bin_trust_function_creators = OFF ,默认必须为 function 传递一个参数

      1
      2
      3
      4
      5
      6
      7
      
      mysql> show variables like 'log_bin_trust_function_creators'; 
      +---------------------------------+-------+
      | Variable_name                   | Value |
      +---------------------------------+-------+
      | log_bin_trust_function_creators | OFF   |
      +---------------------------------+-------+
      1 row in set (0.00 sec)
      
    • 通过 set global log_bin_trust_function_creators=1;我们可以不用为 function 传参

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      
      mysql> set global log_bin_trust_function_creators=1; 
      Query OK, 0 rows affected (0.00 sec)
          
      mysql> show variables like 'log_bin_trust_function_creators';
      +---------------------------------+-------+
      | Variable_name                   | Value |
      +---------------------------------+-------+
      | log_bin_trust_function_creators | ON    |
      +---------------------------------+-------+
      1 row in set (0.00 sec)
      
  • 这样添加了参数以后,如果mysqld重启,上述参数又会消失,永久方法在配置文件中修改‘

    • windows下:my.ini –> [mysqld] 节点下加上 log_bin_trust_function_creators=1
    • linux下:/etc/my.cnf –> [mysqld] 节点下加上 log_bin_trust_function_creators=1

创建函数,保证每条数据都不同

随机产生字符串的函数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
delimiter $$ # 两个 $$ 表示结束
create function rand_string(n int) returns varchar(255)
begin
    declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyz';
    declare return_str varchar(255) default '';
    declare i int default 0;
    while i < n do
        set return_str = concat(return_str,substring(chars_str,floor(1+rand()*52),1));
        set i=i+1;
    end while;
    return return_str;
end $$

随机产生部门编号的函数

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

创建存储过程

创建往emp表中插入数据的存储过程

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

创建往dept表中插入数据的存储过程

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

调用存储过程

向 dept 表中插入 10 条记录

1
2
3
DELIMITER ;
CALL insert_dept(100, 10);

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
mysql> select * from dept;
+--------+---------+--------+
| deptno | dname   | loc    |
+--------+---------+--------+
|    101 | aowswej | syrlhb |
|    102 | uvneag  | pup    |
|    103 | lps     | iudgy  |
|    104 | jipvsk  | ihytx  |
|    105 | hrpzhiv | vjb    |
|    106 | phngy   | yf     |
|    107 | uhgd    | lgst   |
|    108 | ynyl    | iio    |
|    109 | daqbgsh | mp     |
|    110 | yfbrju  | vuhsf  |
+--------+---------+--------+
10 rows in set (0.00 sec)

向 emp 表中插入 50w 条记录

1
2
DELIMITER ;
CALL insert_emp(100001, 500000);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
mysql> select * from emp limit 20;
+----+--------+-------+----------+-----+------------+---------+--------+--------+
| id | empno  | ename | job      | mgr | hiredate   | sal     | comm   | deptno |
+----+--------+-------+----------+-----+------------+---------+--------+--------+
|  1 | 100002 | ipbmd | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    101 |
|  2 | 100003 | bfvt  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    107 |
|  3 | 100004 |       | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    109 |
|  4 | 100005 | cptas | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    101 |
|  5 | 100006 | ftn   | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    108 |
|  6 | 100007 | gzh   | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    102 |
|  7 | 100008 | rji   | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    100 |
|  8 | 100009 |       | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    106 |
|  9 | 100010 | tms   | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    100 |
| 10 | 100011 | utxe  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    101 |
| 11 | 100012 | vbis  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    104 |
| 12 | 100013 | qgfv  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    104 |
| 13 | 100014 | wrvb  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    105 |
| 14 | 100015 | dyks  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    109 |
| 15 | 100016 | hpcs  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    101 |
| 16 | 100017 | fxb   | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    108 |
| 17 | 100018 | vqxq  | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    102 |
| 18 | 100019 | rq    | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    102 |
| 19 | 100020 | l     | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    106 |
| 20 | 100021 | lk    | salesman |   1 | 2020-08-05 | 2000.00 | 400.00 |    100 |
+----+--------+-------+----------+-----+------------+---------+--------+--------+
20 rows in set (0.00 sec)