# 部門表 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 "" );
# 員工表 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 );
2. 設(shè)置參數(shù):
創(chuàng)建函數(shù)的時候,可能會報錯:
this function has none of deterministic……
我們得開啟一個參數(shù),首先執(zhí)行如下語句可以查看該參數(shù):
show variables like 'log_bin_trust_function_creators';
delimiter $$ create function rand_string(n int) returns varchar(255) begin declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyz'; 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 $$ create function rand_num() returns int(5) begin declare i int default 0; set i = floor(100 + rand() * 10); return i; end $$
假如要刪除rand_num函數(shù),那么就是執(zhí)行:
drop function rand_num;
4. 創(chuàng)建存儲過程:
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, 4000, 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; 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 $$