#创建数据库
CREATE DATABASE bigdata
#使用数据库
USE bigdata
#关闭二进制日志
SET sql_log_bin=0
#创建表部门表
CREATE TABLE dept(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
dname VARCHAR(20) NOT NULL DEFAULT '',
loc VARCHAR(13) NOT NULL DEFAULT '',
num INT
) ENGINE = INNODB DEFAULT CHARSET =GBK;
#创建表员工表
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=gbk;
#创建产生随机数的函数
DELIMITER $$
CREATE FUNCTION rand_num()
RETURNS INT(5)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = FLOOR(100+RAND()*10);
RETURN i;
END $$
DELIMITER ;
#创建产生随机字符串的函数
DELIMITER $$
CREATE FUNCTION rand_string(n INT) RETURNS VARCHAR(255)
BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
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 $$
DELIMITER ;
SHOW GLOBAL VARIABLES LIKE '%log_bin%'
SET GLOBAL log_bin_trust_function_creators =ON
#创建插入的存储过程
DELIMITER $$
CREATE PROCEDURE insert_dept(IN START INT(10),IN max_num INT(10))
BEGIN
DECLARE i INT DEFAULT 0;
REPEAT
SET i=i+1;
INSERT INTO dept (deptno,dname,loc,num) VALUES((START+i),rand_string(10),rand_string(8),RAND()*10+RAND()*100+RAND()*1000+RAND()*10000+RAND()*100000+RAND()*1000000+RAND()*10000000+RAND()*100000000);
UNTIL i=max_num
END REPEAT;
COMMIT;
END $$
DELIMITER ;
#创建插入的存储过程
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 $$
DELIMITER ;
#调用存储过程
CALL insert_dept(1,100000000);
CALL insert_emp(1,10000000);
来源:https://www.cnblogs.com/jdy1022/p/10931953.html