2024年4月27日发(作者:)

实验11 触发器

一、实验目的

1.掌握触发器的创建、修改、删除及其使用方法。

2.掌握触发器的功能。

二、实验内容

(1)打开查询分析器,创建触发器。

例1:创建触发器trigger_Student, 如修改Student 表中某一学生的学号,则表SC

中与该学生相关的学号自动别更新。

create trigger trigger_Student

on Student for update

as

if update(Sno)

begin

declare @newSno int,@oldSno int

select @newSno=Sno from inserted

select @oldSno=Sno from deleted

update SC set Sno=@newSno

where Sno=@oldSno

end

激活触发器update Student set Sno='201015121' where Sno='200215121'

select * from SC

例2:创建一限制更新触发器trigger_SC,若修改表SC中一记录的学号,则要检查S

中是否存在于该学生学号相同的记录,若有则不许修改,如没有则可修改

create trigger trigger_SC

on SC for update

as

if update(Sno)

begin