mysql插入大数据

时间:2023-03-09 22:29:44
mysql插入大数据
/*部门表*/
CREATE TABLE dept(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, /*id*/
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT , /*编号*/
dname VARCHAR() NOT NULL DEFAULT "",/*名称*/
loc VARCHAR() NOT NULL DEFAULT "" /*地点*/
)ENGINE=MyISAM DEFAULT CHARSET=utf8; /*EMP雇员表*/
CREATE TABLE emp(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, /*id*/
empno MEDIUMINT UNSIGNED NOT NULL DEFAULT ,/*编号*/
ename VARCHAR() NOT NULL DEFAULT "",/*名字*/
job VARCHAR() NOT NULL DEFAULT "",/*工作*/
mgr MEDIUMINT UNSIGNED NOT NULL DEFAULT ,/*是哪个及编号*/
hiredate DATE NOT NULL,/*入职时间*/
sal DECIMAL(,) NOT NULL,/*薪水*/
comm DECIMAL(,) NOT NULL,/*红利*/
deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT /*部门编号*/
)ENGINE=MyISAM DEFAULT CHARSET=utf8; /*工资级别表*/
CREATE TABLE salgrade(
grade MEDIUMINT UNSIGNED NOT NULL DEFAULT ,
losal DECIMAL(,) NOT NULL,
hisal DECIMAL(,) NOT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8; show variables like 'log_bin_trust_function_creators';
set global log_bin_trust_function_creators = ;
-- 注意命令结束符 #定义一个新的命令结束符
delimiter $$
#rand_string(n INT) rand_string 是函数名(n INT) //该函数传参一个整数
create function rand_string(n INT)
returns varchar()
begin
declare chars_str varchar() default
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
declare return_str varchar() default '';
declare i int default ;
while i < n do
set return_str=concat(return_str,substring(chars_str,floor(+rand()*),));
set i = i +;
end while;
return return_str;
end $$ #定义一个新的命令结束符
delimiter $$
-- 生成部门号函数
create function rand_num( )
returns int()
begin
declare i int default ;
set i = floor(+rand()*);
return i;
end $$ #定义一个新的命令结束符
delimiter $$
create procedure insert_emp(in start int(),in max_num int())
begin
declare i int default ;
#set autocommit = //把autocommit设置成0,这样可以只提交一次,否则。。。。。
set autocommit = ;
repeat
set i = i +;
insert into emp (empno, ename,job,mgr,hiredate,sal,comm,deptno) values ((start+i),rand_string(),'SALESMAN',,curdate(),,,rand_num());
until i = max_num
end repeat;
commit;
end $$ call insert_emp(,);