create table dept(
id int UNSIGNED PRIMARY KEY auto_increment,
depto MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
dname VARCHAR(20) NOT NULL DEFAULT '',
loc VARCHAR(13) NOT NULL DEFAULT ''
)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,
sal DECIMAL(7,2) NOT NULL,
comm DECIMAL(7,2) NOT NULL,
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0
)ENGINE=INNODB DEFAULT CHARSET=GBK;
show VARIABLES LIKE 'log_bin_trust_function_creators';
SET GLOBAL log_bin_trust_function_creators=1;
SELECT * from emp
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
SET i = i+1;
END WHILE;
RETURN return_str;
END $$
#随机产生编号
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_num;
#创建存储过程
DELIMITER $$
CREATE PROCEDURE insert_emp(IN START INT(10),IN max_num INT(10))
BEGIN
DECLARE i INT DEFAULT 0;
#SET autocommit=0 自动提交设置为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 $$
CREATE PROCEDURE insert_dept(IN START INT(10),IN max_num INT(10))
BEGIN
DECLARE i INT DEFAULT 0;
#SET autocommit=0 自动提交设置为0
SET autocommit=0;
REPEAT
SET i=i+1;
INSERT INTO dept(depto,dname,loc) values((START+i),rand_string(10),rand_string(8));
UNTIL i= max_num
END REPEAT;
COMMIT;
END $$
#删除存储过程
drop PROCEDURE if exists insert_emp;
DELIMITER;
CALL insert_dept(100,10);
CALL insert_emp(1,1000000);
SELECT * FROM dept;