实验学时:2学时
实验类型:设计
实验要求:必做
一、实验目的
通过编写存储过程,加深对存储过程功能和数据库编程的意义的理解。
二、实验内容
1、创建存储过程,显示所有学生所有信息,并执行查看结果(无参存储过程) 。
操作方法如下:
① 使用create procedure语句创建存储过程stu_info1,存储过程无参数。
② 在过程体中查询所有学生信息。
③ 调用存储过程查看结果。
2、 创建存储过程,根据给定学号显示学生的所有信息,并执行查看结果(带输入参数的参存储过程)
操作方法如下:
① 使用create procedure语句创建存储过程stu_info2,存储过程以字符型变量stu_sno为输入参数。
② 在过程体根据stu_sno的值显示该学生的所有信息。
③ 调用存储过程查看学号为“201910121”的学生的查询结果。
3、创建存储过程,给定一个学生的学号,查询该生的最低成绩和最高成绩,并执行查看结果
操作方法如下:
① 使用create procedure语句创建存储过程stu_info3,存储过程以字符型变量stu_sno为输入参数,以min_grade和max_grade为输出参数分别存放最低成绩和最高成绩。
② 在过程体根据stu_sno的值查询该学生的最低成绩和最高成绩,放入输出变量中。
③ 调用存储过程查询学号为“201910121”的学生的最低成绩和最高成绩,将结果显示在客户端。
4、查看存储过程
运用show procedures like “名称”\G;命令查看某一个存储过程。
5、删除存储过程
运用drop procedures “名称”\G;命令删除某一个存储过程。
三、课后练习题
1、在shop数据库中创建一个存储过程,以订单编号为参数,输出该订单的商品信息。
--无参数存储过程
delimiter $$
create procedure proc1()
begin
select * from student where ssex='女';
end
$$
delimiter ;
--带有in参数的存储过程
delimiter $$
create procedure proc2(in stusex char(2))
begin
select * from student where ssex=stusex;
end
$$
delimiter ;
--带有out参数的存储过程
delimiter $$
create procedure proc3(in stusex char(2),out sexcount int)
begin
select count(*) from student group by ssex having ssex=stusex into sexcount;
end
$$
delimiter ;
call proc3('女',@sexcount);
--带有inout参数的存储过程
delimiter $$
create procedure proc4(inout n int)
begin
select count(*) from student group by sage having sage=n into n;
end
$$
delimiter ;
set @n=19;
call proc4(@n);
--查看游标的创建语句
show create procedure proc1;
-查看游标
show procedure status like 'proc%';
-删除游标
drop procedure proc1;