实验九 触发器的创建与使用
一、实验目的
本实验的目的是使学生进一步掌握SQL Server触发器的创建及使用方法,加深SQL触发器的理解。通过对数据的更新操作体会其触发器的作用。 二、实验要求
1. 在实验之前做好准备
2. 试验之后提交实验报告,并验收实验结果 三、实验内容
1. 在班级表class中增加班级人数(c_total)字段 Alter table class
2. 创建INSERT触发器t_inst_stu
3. 创建DELETE触发器t_dele_stu:在学生表(student)中删除一名学生,若其班级编号非空,则将班级表(class)中相应班级的人数(c_total)自动减1。 4. 创建UPDATE触发器t_update_stu。 5. 体会触发器的作用。
四、实验步骤
1. 在班级表class中增加班级人数(c_total)字段,默认值是0; 并手动统计人数作为该字段的值;
alter table class
add c_total int default 0;
update class set c_total = '3'
where class.clsno = 'cs01'; update class set c_total = '1'
where class.clsno = 'gl01'; update class set c_total = '1'
where class.clsno = 'mt04';
2. 创建INSERT触发器t_inst_stu:在学生表(student)中每新增一名学生,若其班级编号非空,则将班级表(class)中相应班级的人数(c_total)自动加1。
create trigger update_stu after insert on student for each row begin
if(:new.clsno is not null)then update class
set c_total = c_total + 1 where :new.clsno = class.clsno; end if; end;
insert
into student(sno,sname,scsex,clsno,saddr,sage,height) values('20071102','李四','男','cs01',null,'17',null);
3. 创建DELETE触发器t_dele_stu:在学生表(student)中删除一名学生,若其班级编号非空,则将班级表(class)中相应班级的人数(c_total)自动减1。
create trigger t_dele_stu after delete on student for each row begin
if(:old.clsno is not null)then update class
set c_total = c_total - 1 where :old.clsno = class.clsno; end if; end;
4. 创建UPDATE触发器t_update_stu:当某学生所在班号发生变化时(即调到另一班级后),将其原先所在班级的人数(c_total)减1,将新调入的班级班级的人数(c_total)加1。
create trigger t_update_stu after update on student for each row begin
if(:old.clsno <> :new.clsno)then update class
set c_total=c_total-1
where :old.clsno = class.clsno; update class
set c_total=c_total+1
where :new.clsno = class.clsno; end if; end;
五、实验总结
通过本次实验,我了解了触发器的相关原理,掌握触发器的定义,理解了触发器的使用方法及其特点。
实验十 存储过程的创建与使用
一、实验目的
本实验的目的是使学生进一步掌握SQL Server存储过程的创建及使用方法,加深对SQL存储过程的理解。通过对存储过程的调用体会其作用。 二、实验要求
1. 在实验之前做好准备
2. 试验之后提交实验报告,并验收实验结果 三、实验内容
1、创建一个不带参数的存储过程p_stu_info1,实现对满足要求的学生基本信息的查询。 2、创建一个带有参数的存储过程p_stu_info2,实现对满足要求的学生基本信息的查询。 3、创建一个存储过程p_stu_info3,根据输入的学号,查询某学生的基本信息。 4、创建一个存储过程p_stu_grade,根据输入的学号,返回其选课及其成绩。
5.使用SQL语句分别执行p_stu_info1、p_stu_info2、p_stu_info3和p_stu_grade,并将结果显示出来。 四、实验步骤
1.建立不带参数的存储过程p_stu_info1(选择所有年龄<21岁的男同学)。
create procedure p_stu_info1 as begin
select * from student where sage<21 and ssex='男'
end
执行语句:exec p_stu_info1
2.建立带有参数的存储过程p_stu_info2,根据传递的参数只显示与指定学号的学生同龄的所有同学(如:20071101,自己可以选别的学号,下同)。
create procedure p_stu_info2 @sno char(8) as begin
declare @sage numeric(3,0)
select @sage=sage from student where sno=@sno select * from student where sage=@sage end 执行:
exec procedure '20071101'
3.创建一个存储过程p_stu_info3,根据传入的学号(如:20071101),查询某学生的基本
信息。
create procedure sc_avg @sno char(10),@avg int output AS begin
select @avg=avg(grade) from sc where sno=@sno group by sno; print('ok ! 学号:' + @sno+' 的数据已处理!'); end; declare
@tmp char(10),@avg_out int set @tmp='20090001'
exec sc_avg @tmp,@avg_out output
4. 创建一个存储过程p_stu_grade,根据输入的学生学号(如:20071101),返回其选课及其成绩。
create procedure p_getgrade(sid sc.sno%type, cid sc.cno%type) is
res number; begin
res:=get_grade(sid,cid); case
when res>=0 then
dbms_output.put_line('课程成绩 = ' || res); when res=-1 then
dbms_output.put_line('指定的数据不存在' ); else
dbms_output.put_line(sqlcode || '-------' || sqlerrm); end case; end;
5.使用SQL语句分别执行p_stu_info1、p_stu_info2、p_stu_info3和p_stu_grade,并将结果显示出来。
五、实验总结
通过本次实验,我了解了存储过程的相关知识,掌握存储过程的定义,理解了存储过程的调用及其参数的使用方法。