当前位置:网站首页>实验七 触发器
实验七 触发器
2022-06-22 18:01:00 【m0_54853503】
一.实验目的
(1)理解触发器的用途、类型和工作原理。
(2)掌握利用T-SQL语句创建和维护触发器的方法。
(3)掌握利用SQL Server Management Studio创建、维护触发器的方法。
二.实验内容
创建AFTER触发器
(1)创建一个在插入时触发的触发器sc_insert,当向SC表插入数据时,须确保插入的学号已在Student表中存在,并且还须确保插入的课程号在Course表中存在;若不存在,则给出相应的提示信息,并取消插入操作,提示信息要求指明插入信息是学号不满足条件还是课程号不满足条件。(注:Student表与SC表的外键约束要先取消。)
if exists(select name from sysobjects where name='sc_insert' and type='TR') drop trigger sc_insert go CREATE TRIGGER sc_insert ON SC AFTER INSERT AS BEGIN declare @sno char(8),@cno char(8) select @sno=Sno , @cno=Cno from inserted if not exists(select Sno from Student where @sno=Sno) begin rollback tran print('学号不存在') end else if not exists(select Cno from Course where @cno=Cno) begin rollback tran print('课程号不存在') end END GO
(2)为Course表创建一个触发器Course_del,当删除了Course表中的一条课程信息 时,同时将表SC中相应的学生选课记录也删除。
CREATE TRIGGER Course_del ON Course AFTER DELETE AS begin transaction declare @cno char(8) select @cno=Cno from deleted delete from SC where [email protected] commit transaction GO(3)在Course表中添加一个平均成绩avg_grade字段(记录每门课程的平均成绩),创建一个触发器grade_modify,当SC表中的某学生的成绩发生变化时,则Course表中的平均成绩也能及时相应发生改变。
alter table Course add avg_grade int; update Course set avg_grade=( select avg(Grade) from SC where SC.Cno=Course.Cno);
CREATE TRIGGER grade_modify ON SC AFTER UPDATE AS BEGIN if update(Grade) begin update Course set avg_grade=( select avg(Grade) from SC where SC.Cno=Course.Cno group by Cno) end END GO(4)测试上述三个触发器。
1.测试sc_insert触发器insert into SC values('20220002','011',99)
删除外键约束alter table SC drop constraint FK__SC__Sno__6477ECF3; alter table SC drop constraint FK__SC__Cno__656C112C;
insert into SC values('20110002','006',71);
insert into SC values('20110005','001',71);
insert into SC values('20110002','011',71);
2.测试Course_del触发器delete from Course where Cno='010';
3.测试grade_modify触发器update SC set Grade=70 where Sno='20110001' and Cno='001';
创建INSTEAD OF 触发器
(1)创建一视图student_view,包含学号、姓名、课程号、课程名、成绩等属性,在student_view上创建一个触发器grade_modify,当对student_view中的学生的成绩进行修改时,实际修改的是SC中的相应记录。
创建视图create view student_view as select Student.Sno,Sname,Course.Cno,Cname,Grade from Student,Course,SC where Student.Sno=SC.Sno and Course.Cno=SC.Cno;创建触发器
CREATE TRIGGER grade_modify2 ON student_view INSTEAD OF UPDATE AS BEGIN if update(Grade) begin update student_view set Grade=(select Grade from inserted) where Sno=(select Sno from inserted) and Cno=(select Cno from inserted) end END GO(2)在Student表中插入一个getcredit字段(记录学生所获学分的情况),创建一个触发器ins_credit,当更改(注:含插入时)SC表中的学生成绩时,如果新成绩大于等于60分,则该生可获得这门课的学分,如果新成绩小于60分,则该生未能获得这门课的学分。
--添加字段 alter table Student add getcredit tinyint; go --更新Student表 update Student set getcredit=( select sum(Credit) from Course,SC where Course.Cno=SC.Cno and Student.Sno=SC.Sno) go --创建触发器 CREATE TRIGGER ins_credit ON SC AFTER UPDATE,INSERT AS BEGIN declare @sno char(8),@credit tinyint select @sno=Sno,@credit=Credit from Course,inserted where Course.Cno=inserted.Cno and inserted.Grade>=60 update Student set [email protected] where [email protected] END GO(3)测试上述两个触发器。
1.测试grade_modify2update student_view set Grade=50 where Sno='20110002' and Cno='001'
2.测试ins_credit触发器update SC set Grade=59 where Sno='20110001' and Cno='002';
这里就是在表结构修改之后的添加或者插入数据才会在Student表中生效
使用T-SQL语句管理和维护触发器
(1)用系统存储过程sp_helptrigger查看触发器grade_modify的相关信息。sp_helptrigger 'student_view';
(2)使用sp_helptext查看触发器grade_modify中的定义内容。
sp_helptext 'grade_modify2';
(3)使用select语句查看触发器grade_modify的定义内容。
select name,text from sysobjects,syscomments where sysobjects.id=syscomments.id and name='grade_modify2' and type='TR'
(4)用系统存储过程sp_depends查看触发器grade_modify的相关性(即该触发器涉及哪些基本表)。
sp_depends 'grade_modify2';
(5)将sc_insert触发器改为instead of 触发器,实现的功能不变。
drop trigger sc_insert; CREATE TRIGGER sc_insert ON SC INSTEAD OF INSERT AS BEGIN declare @sno char(8),@cno char(8) select @sno=Sno , @cno=Cno from inserted if not exists(select Sno from Student where @sno=Sno) begin rollback tran print('学号不存在') end if not exists(select Cno from Course where @cno=Cno) begin rollback tran print('课程号不存在') end END GO测试:

(6)将触发器sc_insert删除。
drop trigger sc_insert;
使用SQL Server Management Studio管理触发器
(1)在SQL Server Management Studio中重新创建刚删除的触发器sc_insert。
展开要创建触发器的数据库,在数据库目录下选择要创建触发器的表,展开数据表,在数据表结点下右击"触发器",然后选择"新建触发器"
在此位置编写触发器的语句:
如下:
最后执行即可创建成功(2)查看触发器sc_insert 的内容。


(3)删除触发器sc_insert。

边栏推荐
猜你喜欢

5GC和卫星融合通信方案

Shell script explanation (VII) -- regular expression, sort, uniq, tr

shell脚本详解(七)——正则表达式、sort、uniq、tr

wpa_ State machine migration of supplicant

Pull down refresh and pull up to load more listviews

Service practice: use service to complete a download task

集群、分布式、微服务概念和区别

Redis中的布隆过滤器与布谷鸟过滤器,你了解多少?
![Programmer's tool encyclopedia [continuous update]](/img/7e/b7fbc030f4bbded3ee6188360d7d54.png)
Programmer's tool encyclopedia [continuous update]

Shell programming specification and variables
随机推荐
Typescript (7) generic
wpa_ State machine migration of supplicant
Niuke.com: judge whether it is palindrome string
如何在 FlowUs和Notion 等笔记软件中进行任务管理?
基于云架构的业务稳定性建设思路
Zynq UltraScale + RFSoC ZCU111 RF时钟树学习 1
How MySQL deletes a column in a database table
Iplook 5gc successfully connected with CICA international CHF (billing function)
如何提高工作效率?苹果电脑效率工具合集
shell脚本详解(十)——sed编辑器的使用方法
Flutter系列-搭建Flutter开发环境
UE4_ Ue5 make 3dui follow the camera orientation (attached works)
同花顺容易开户么?手机开户安全么?
Chrome suddenly can't copy and paste
Activity跳转到Fragment的方法(Intent)
Niuke network: minimum coverage substring
Robotframework installation tutorial
Some technical ideas:
Dynamically changing the style of label elements in a loop
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“