标签:mgr bst unsigned func art date engine 数据库 curd
#创建数据库
CREATE DATABASE bigdata
#使用数据库
USE bigdata
#创建表
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 ‘‘
) 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%‘
#创建插入的存储过程
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) VALUES((START+i),rand_string(10),rand_string(8));
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(51,100000000);
SELECT * FROM dept;
CALL insert_emp(1500000,10000000);
SELECT * FROM emp;
SELECT COUNT(id) FROM emp;
SELECT FLOOR(1+RAND()*52);
SELECT RAND()*52;
标签:mgr bst unsigned func art date engine 数据库 curd
原文地址:https://www.cnblogs.com/jdy1022/p/10931953.html