部门表dept
CREATE TABLE `dept` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`deptno` mediumint unsigned NOT NULL DEFAULT '0',
`dname` varchar(20) NOT NULL DEFAULT '',
`ioc` varchar(13) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '楼层位置',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
员工表
CREATE TABLE `emp` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`empno` mediumint unsigned NOT NULL DEFAULT '0' COMMENT '编号',
`ename` varchar(20) NOT NULL DEFAULT '' COMMENT '名字',
`job` varchar(9) NOT NULL DEFAULT '' COMMENT '工作',
`mgr` mediumint unsigned NOT NULL DEFAULT '0' COMMENT '上级编号',
`hiredate` date NOT NULL COMMENT '入职时间',
`sal` decimal(7,2) NOT NULL COMMENT '薪水',
`comm` decimal(7,2) NOT NULL COMMENT '红利',
`deptno` mediumint unsigned NOT NULL DEFAULT '0' COMMENT '部门编号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
创建函数,假如报错: This function has none of DETERMINISCI…
#由于开启过慢查询日志,因为我们开启了bin-log, 我们就必须为我们的function指定一个参 数。
show variables like 'log_ bin_ trust function creators';
set global log_ bin_ _trust_ _function_ creators=1;
#这样添加了参数以后,如果mysqld重启,,上述参数又会消失,永久方法:
windows下my.ini[mysqld]加上log_ _bin_ _trust_ function_ _creators=1
linux下/etc/my.cnf 下my.cnf[mysqld]加上
log_ bin_ _trust_ function_ _creators=1
修改:
docker exec -it 《id》 bash
cd /etc/mysql
vim my.cnf(没安装要先安装vim)
apt-get update
apt-get install vim
添加
log_ bin_ trust function__creators=1
重启
随机产生字符串:
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 $$
注意:此时mysql的结束符由原来的;变为$$
DELIMITER $$
CREATE FUNCTION rand_num()
RETURNS INT(5)
BEGIN
DECLARE i INT DEFAULT 0;
SET i=FLOOR(100+RAND()*10);
RETURN i;
END $$
假如要删除:drop function rand
创建往emp表中插入数据的存储过程
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 ((STARTS+i),rand_string(6),'SALESMAN',001,CURDATE(),2000,400,rand_num());
UNTIL i=max_num
END REPEAT;
COMMIT;
END $$
创建往dept表中插入数据的存储过程
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,Ioc)VALUES ((START+i),rand_string(10),rand_string(8));
UNTIL i=max_num
END REPEAT;
COMMIT;
END $$
select * from dept$$
DELIMITER;
CALL insert_dept(100,10);
执行存储过程,往emp表添加50w条数据
DELIMITER;
CALL insert_emp(100001,500000);