批量插入数据库脚本
1.建表
dept表
create table dept(
id int unsigned primary key auto_increment,
deptno mediumint unsigned not null default 0 comment "部门编号",
dname varchar(20) not null default "" comment "部门名称",
loc varchar(20) not null default "" comment "部门位置"
);
emp表
create table emp(
id int unsigned primary key not null auto_increment,
empno int unsigned not null default 0 comment "编号",
ename varchar(20) not null default "" comment "名字",
job varchar(20) not null default "" comment "工作",
mgr int 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 int unsigned not null default 0 comment "部门编号"
);
2.设置参数log_bin_trust_function_creators
set global log_bin_trust_function_creators = 1;
3.创建函数,保证每条数据不同
mysql自定义函数
一、基本语法
delimiter 自定义符号 -- 如果函数体只有一条语句, begin和end可以省略, 同时delimiter也可以省略
create function 函数名(形参列表) returns 返回类型 -- 注意是retruns
begin
函数体 -- 函数内定义的变量如:set @x = 1; 变量x为全局变量,在函数外面也可以使用
返回值
end 自定义符号
delimiter ;
二、函数相关
1. show function status [like 'pattern']; -- 查看所有自定义函数, 自定义函数只能在本数据库使用。
2. show create function 函数名; -- 查看函数创建语句
3. drop function 函数名; -- 删除函数
创建函数报错1418
参考地址SQL报错1418
随机产生字符串
# 声明sql语句以$$为终结符号
delimiter $$
# 创建一个函数,名为rand_string,需要一个int类型参数,返回一个varchar类型
create function rand_string(n int) returns varchar(255)
begin
# 声明chars_ste变量、return_str变量、i
declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
declare return_str varchar(255) default '';
declare i int default 0;
while i < n do
# RAND() 函数被调用时,可以产生一个在 0 和 1 之间的随机数
# FLOOR(x) 函数返回小于 x 的最大整数值
# CONCAT(sl,s2,...) 函数返回结果为连接参数产生的字符串,或许有一个或多个参数。
# SUBSTRING(s,n,len) 带有 len 参数的格式,从字符串 s 返回一个长度同 len 字符相同的子字符串,起始于位置 n。
set return_str = concat(return_str,substring(chars_str,floor(1+rand()*52),1));
set i = i + 1;
end while;
return return_str;
end $$
# 声明sql语句以;为终结符号,mysql中默认以分号结尾,创建完函数之后,切换回默认设置
delimiter ;
随机产生部门编号
delimiter $$
create function rand_num() returns int(5)
begin
declare i int default 0;
set i = floor(100 + rand() * 10);
return i;
end $$
delimiter ;
4.创建存储过程
存储过程相关
1. show procedure status [like 'pattern']; -- 查看所有自定义存储过程, 自定义存储过程只能在本数据库使用。
2. show create procedure 函数名; -- 查看存储过程创建语句
3. drop procedure 函数名; -- 删除存储过程
创建往emp表中插入数据的存储过程
delimiter $$
# 创建一个名为insert_emp的存储过程,入参为start与max_num均是int类型,in代表入参
create procedure insert_emp(in start int(10),in max_num int(10))
begin
declare i int default 0;
# set autocommit = 0把autocommit设置为0,即关闭自动提交
set autocommit = 0;
# repeat循环
repeat
set i = i + 1;
insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values ((start+i),rand_string(6),'programmer',0001,curdate(),2000,400,rand_num());
until i = max_num end repeat;
commit;
end $$
# 声明sql语句以;为终结符号,mysql中默认以分号结尾,创建完函数之后,切换回默认设置
delimiter ;
创建往demp表中插入数据的存储过程
delimiter $$
# 创建一个名为insert_dept的存储过程,入参为start与max_num均是int类型,in代表入参
create procedure insert_dept(in start int(10),in max_num int(10))
begin
declare i int default 0;
# set autocommit = 0把autocommit设置为0,即关闭自动提交
set autocommit = 0;
# repeat循环
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 $$
# 声明sql语句以;为终结符号,mysql中默认以分号结尾,创建完函数之后,切换回默认设置
delimiter ;
5.调用存储过程
dept
call insert_dept(1,100)
emp
call insert_emp(1000000000,5000000);