(数据库实验3触发器报告.doc
文本预览下载声明
数据库技专题训练I(2014年春)
序号:
数据库专题训练------触发器
实验报告
系别:计算机科学与技术
班级:计11-3班
姓名:黄娟娟
学号:11101020324
成绩:
评语:
指导教师签字: 日期:
实验二 触发器
实验环境及要求
触发器是一种特殊的存储过程,不能被用户直接调用。可以包含复杂的 SQL语句。在特定事件发生时自动触发执行,通常用于实现强制业务规则和数据完整性。DML触发器分为两种类型:AFTER 触发器和 INSTEAD OF触发器。
通过本次实验掌握触发器的创建方法以及使用方法。
实验步骤及结果
创建一个名为tri_Insert_S的触发器,测试改触发器的执行情况,并给出实验结果。当插入的新记录中Sage的值不是18至25之间的数值时,就激活该触发器,撤销该插入操作,并给出错误提示。
use SXCJ
go
create trigger tri_Insert_S on S
after insert
as
if exists (select * from inserted
where Sage=18 and Sage=25)
print添加成功!
else
begin
print无法添加!
rollback transaction
end
go
insert into S values(S8,黄丽,女,26,计算机)
insert into S values(S8,黄丽,女,20,计算机)
select *
from S
go
显示如下:
insert into S values(S8,黄丽,女,26,计算机)
insert into S values(S8,黄丽,女,20,计算机)
2) 创建一个名为tri_Update_SC的触发器,要求:(1)首先判断数据库中是否已经存在名为tri_Update_SC的触发器,如果存在,首先删除,再创建。(2)当试图修改SC表中的学生成绩时,给出不能随便修改成绩的信息提示。
use SXCJ
go
if exists(select name from sysobjects
where name=tri_Update_SC and type=TR)
begin
drop trigger tri_Update_SC
end
else
print不存在该触发器,可新建。
Go
显示如下:
create trigger tri_Update_SC on SC
after update
as
if update(Score)
begin
print不能随意修改成绩!
rollback transaction
end
go
update SC
set score=90
Go
显示如下:
3)使用系统存储过程sp_help,sp_helptext,sp_helptrigger查看触发器相关信息。给出显示结果。
use SXCJ
go
exec sp_help tri_Insert_S
exec sp_helptext tri_Insert_S
exec sp_helptrigger S
go
显示如下:
4) 禁用tri_Update_SC触发器,并演示禁用该触发器后Update的执行情况。
use SXCJ
go
alter table SC disable trigger tri_Update_SC
update SC
set Score=95
where Sno=S1 and Cno=C3
print修改成功!
select *
from SC
Go
显示如下:
5)在SC表上创建一个instead of类型的触发器,触发器名称为:tri__Insert_SC。实现:当向表SC插入记录时检查分数的合理性,分数必须在0到100之间,如果不合理就拒绝插入,并给出信息提示,
use SXCJ
go
if exists(select name from sysobjects
where name=tri__InseCrt_SC and type=TR)
begin
drop trigger tri__InseCrt_SC
end
else
begin
create trigger tri__InseCrt_SC on SC
i
显示全部