实验六-存储过程和触发器_第1页
实验六-存储过程和触发器_第2页
实验六-存储过程和触发器_第3页
实验六-存储过程和触发器_第4页
实验六-存储过程和触发器_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上实验六 存储过程和触发器一、目的与要求1. 掌握编写数据库存储过程的方法。2. 掌握建立数据库触发器的方法,通过实验观察触发器的作用和触发条件设置等相关操作。二、实验准备1. 了解编写存储过程和调用的T-SQL语法; 2. 了解触发器的作用;3. 了解编写触发器的T-SQL语法。三、实验内容(一) 存储过程在studentdb数据库中建立存储过程getPractice,查询指定院系(名称)(作为存储过程的输入参数)中参与“实践”课程学习的所有学生学号、姓名、所学课程编号和课程名称,若院系不存在,返回提示信息。提示:D_Info表中存储了院系代码D_ID,而St_Inf

2、o表中学号字段St_ID的前两位与之对应,则D_Info表与St_Info表之间的联系通过这两个字段的运算构成连接条件。1. 分别执行存储过程getPractice,查询“法学院”和“材料科学与工程学院”的学生中参与“实践”课程的所有学生学号、姓名、所学课程编号和课程名称。create procedure getPracticed_name varchar(30)asselect st_info.st_id,st_info.st_name,s_c_info.c_no,c_info.c_namefrom st_info,d_info,s_c_info,c_infowhere d_info.d_n

3、ame=d_name and st_info.st_id=s_c_info.st_id andd_info.d_id=left(st_info.st_id,2)and s_c_info.c_no=c_info.c_no and c_info.c_type='实践'Goexec getPractice '法学院'exec getPractice '材料科学与工程学院'2. 利用系统存储过程sp_rename将getPractice更名为getPctStusp_rename getpractice,getPctStu3. 修改存储过程getPctSt

4、u,返回指定院系中参与实践课程的学生人次数,并利用该存储过程以“法学院”为输入参数验证执行的结果alter procedure getPctStud_name varchar(30)asselect count(s_c_info.st_id)from s_c_info,c_info,d_info,st_infowhere d_info.d_name=d_name and st_info.st_id=s_c_info.st_id and d_info.d_id=left(st_info.st_id,2) and s_c_info.c_no=c_info.c_no and c_info.c_typ

5、e='实践'goexec getPctStu '法学院'4. 再修改存储过程getPctStu,返回指定院系中参与实践课程的学生人数。注:“人数”和“人次数”是不同的,对某一学生而言,如果参与了多门实践课程,则“人次数”是指其参与的课程门数,而“人数”仍为1。alter procedure getPctStud_name varchar(30)asselect count(distinct s_c_info.st_id)from s_c_info,c_info,d_info,st_infowhere d_info.d_name=d_name and st_inf

6、o.st_id=s_c_info.st_id and d_info.d_id=left(st_info.st_id,2)and s_c_info.c_no=c_info.c_no and c_info.c_type='实践' goexec getPctStu '法学院'(二) 触发器1.在studentdb数据库中建立一个具有审计功能的触发器:触发器名为tr_sc,功能要求:审计在s_c_info表中对score字段的更新和插入操作,将这些操作记录到sc_log表中,sc_log表中有如下字段:操作类型type,学号st_id,课程号c_no,旧成绩oldsco

7、re,新成绩newscore,操作员uname,操作时间udate,其中操作员设定默认值为user,操作时间默认值为系统时间。create table sc_log( type char(6), st_id char(8), c_no char(8), oldscore numeric(5,1), newscore numeric(5,1), uname varchar(20) default user, udate datetime default getdate()create trigger tr_sc on s_c_info for insert,updateasif update(s

8、core)begin if(select count(*) from deleted)<>0 insert into sc_log(type,st_id,c_no,oldscore,newscore) select 'update',i.st_id,i.c_no,d.score,i.score from inserted i ,deleted d where i.st_id=d.st_id and i.c_no=d.c_no else insert into sc_log(type,st_id,c_no,newscore) select 'insert

9、9;,st_id,c_no,score from insertedend2. 在s_c_info表上建立一个触发器tr_updasc,用于监控对成绩的更新,要求更新后的成绩不能比更新前低,如果新成绩低则取消操作,给出提示信息,否则允许更新。create trigger tr_updasc on s_c_infofor updateas declare newscore numeric(5,1),oldscore numeric(5,1) select newscore=score from inserted select oldscore=score from deleted if (newscore<oldscore) begin print('新成绩不能比旧成绩低!') rollback transaction end(三)查看存储过程和触发器的信息1.用sp_help查看以上建立的存储过程和触发器的基本信息sp_help getPctStusp_help tr_updasc2. 用sp_helptext查看存储过程和触发器的代码sp_helptext getPctStusp_helptext tr_updasc四、思考与练习1存储过程如何加密?在创建存储过程的语句as前添加语句with encry

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论