MySQL函数编写和存储过程
MySQL函数编写和存储过程
建表
#创建表CLASS /*班级表*/
CREATE TABLE classes(
classno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
classname VARCHAR(20) NOT NULL DEFAULT "",
loc VARCHAR(13) NOT NULL DEFAULT ""
) ENGINE=innodb DEFAULT CHARSET=utf8;
#创建表STUDENTS学生表
CREATE TABLE students(
studentno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
studentname 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,
classno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=innodb DEFAULT CHARSET=utf8 ;
#学生薪水级别表(1-5000,2-5000-8000 3-8000+)
CREATE TABLE salgrade(
grade MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,
losal DECIMAL(17,2) NOT NULL,
hisal DECIMAL(17,2) NOT NULL
) ENGINE=innodb DEFAULT CHARSET=utf8;
INSERT INTO salgrade VALUES (1,1500,5000);
INSERT INTO salgrade VALUES (2,5001,8000);
INSERT INTO salgrade VALUES (3,8001,12000);
INSERT INTO salgrade VALUES (4,12001,15000);
INSERT INTO salgrade VALUES (5,15001,59999);
创建函数:
#创建一个随机产生字符串的函数:
create function rand_string(n INT)
returns varchar(255)
deterministic
begin
declare chars_str varchar(100) default 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
declare return_str varchar(255) default '';
declare i int default 0;
while i