当前位置:网站首页>Experiment 7 trigger
Experiment 7 trigger
2022-06-22 19:31:00 【m0_ fifty-four million eight hundred and fifty-three thousand f】
One . The experiment purpose
(1) Understand the purpose of triggers 、 Type and working principle .
(2) Master the use of T-SQL Statement to create and maintain triggers .
(3) Master the use of SQL Server Management Studio establish 、 Methods of maintaining triggers .
Two . Experimental content
establish AFTER trigger
(1) Create a trigger that triggers when inserting sc_insert, Direction SC Table insert data , Ensure that the student number inserted is already in Student Presence in table , And also make sure that the inserted course number is in Course Presence in table ; If it does not exist , Then corresponding prompt information will be given , And cancel the insert operation , The prompt information is required to indicate whether the student number does not meet the conditions or the course number does not meet the conditions .( notes :Student Table and SC The foreign key constraint of the table must be cancelled first .)
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(' The student number doesn't exist ') end else if not exists(select Cno from Course where @cno=Cno) begin rollback tran print(' Course number does not exist ') end END GO
(2) by Course Table to create a trigger Course_del, When deleted Course A course information in the table when , At the same time, the table SC The corresponding student course selection records in the are also deleted .
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) stay Course Add an GPa to the table avg_grade Field ( Record the average grade of each course ), Create a trigger grade_modify, When SC When the grade of a student in the table changes , be Course The average score in the table can also be changed in time .
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) Test the above three triggers .
1. test sc_insert triggerinsert into SC values('20220002','011',99)
Delete foreign key constraintalter 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. test Course_del triggerdelete from Course where Cno='010';
3. test grade_modify triggerupdate SC set Grade=70 where Sno='20110001' and Cno='001';
establish INSTEAD OF trigger
(1) Create a view student_view, Including student ID 、 full name 、 Course no. 、 Course name 、 Performance and other attributes , stay student_view Create a trigger on grade_modify, When the student_view When modifying the grades of students in , What is actually modified is SC The corresponding record in .
Create viewcreate 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
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) stay Student Insert a getcredit Field ( Record the credits obtained by students ), Create a trigger ins_credit, When changed ( notes : Including insertion )SC The student's grades in the table , If the new score is greater than or equal to 60 branch , Then the student can get credits for this course , If the new score is less than 60 branch , The student fails to get credit for this course .
-- Add fields alter table Student add getcredit tinyint; go -- to update Student surface update Student set getcredit=( select sum(Credit) from Course,SC where Course.Cno=SC.Cno and Student.Sno=SC.Sno) go -- Create trigger 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) Test the above two triggers .
1. test grade_modify2update student_view set Grade=50 where Sno='20110002' and Cno='001'
2. test ins_credit triggerupdate SC set Grade=59 where Sno='20110001' and Cno='002';
Here is the addition or insertion of data after the table structure is modified Student Effective in the table
Use T-SQL Statement management and maintenance triggers
(1) Use system stored procedures sp_helptrigger Check triggers grade_modify Information about .sp_helptrigger 'student_view';
(2) Use sp_helptext Check triggers grade_modify Definition in .
sp_helptext 'grade_modify2';
(3) Use select Statement view trigger grade_modify The definition of .
select name,text from sysobjects,syscomments where sysobjects.id=syscomments.id and name='grade_modify2' and type='TR'
(4) Use system stored procedures sp_depends Check triggers grade_modify The relevance of ( That is, which basic tables are involved in the trigger ).
sp_depends 'grade_modify2';
(5) take sc_insert The trigger is changed to instead of trigger , The functions realized remain unchanged .
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(' The student number doesn't exist ') end if not exists(select Cno from Course where @cno=Cno) begin rollback tran print(' Course number does not exist ') end END GOtest :

(6) Set the trigger sc_insert Delete .
drop trigger sc_insert;
Use SQL Server Management Studio Manage triggers
(1) stay SQL Server Management Studio Recreate the trigger you just deleted sc_insert.
Expand the database where you want to create the trigger , In the database directory, select the table to create the trigger , Expand data table , Right click... Under the data table node " trigger ", And then choose " New trigger "
Write the statement of the trigger in this position :
as follows :
Finally, execute to create a successful(2) Check triggers sc_insert The content of .


(3) Delete trigger sc_insert.

边栏推荐
- wpa_ State machine migration of supplicant
- Implementing Domain Driven Design - using ABP framework - solution overview
- 有效的括号
- 同花顺难开户么?网上开户安全么?
- Makefile does not compile some files
- vs code突然无法进行代码跳转
- Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
- 数商云:数字化供应链系统搭建,赋能企业实现物流供应链的优化升级
- Do you use thread or service?
- 运维、监控、AIOps的几个重要观点
猜你喜欢

数字赋能机械制造业,供应链协同管理系统解决方案助力企业供应链再升级

Digital enabling machinery manufacturing industry, supply chain collaborative management system solution helps enterprises upgrade their supply chain

TypeScript(7)泛型

远程访问及控制——SSH远程管理及TCP Wrappers 访问控制

After reading the hated courage

Iplook 5gc successfully connected with CICA international CHF (billing function)

Cookie encryption 3+rpc solution

Notes on new reports

小波变换db4进行四层分解及其信号重构—matlab分析及C语言实现
![K个一组翻转链表[链表拆解/翻转/拼装]](/img/70/fb783172fa65763f031e6bd945cbd9.png)
K个一组翻转链表[链表拆解/翻转/拼装]
随机推荐
Flutter series -flutter route management
集群、分布式、微服务概念和区别
shell脚本详解(四)——循环语句之while循环和until循环(附加例题及解析)
Digital enabling machinery manufacturing industry, supply chain collaborative management system solution helps enterprises upgrade their supply chain
Niuke network: minimum coverage substring
泡泡玛特:空洞的灵魂需要故事
Flush difficult to open an account? Is it safe to open an account online?
运维、监控、AIOps的几个重要观点
【干货|接口测试必备技能-常见接口协议解析】
Is flush easy to open an account? Is it safe to open a mobile account?
5G 短消息解决方案
将一维数据(序列)转化为二维数据(图像)的方法汇总GAFS, MTF, Recurrence plot,STFT
Notes on Combinatorics (V) chains in distributive lattice
STM32 control matrix key, Hal library, cubemx configuration
贪心之区间问题(1)
远程访问及控制——SSH远程管理及TCP Wrappers 访问控制
2022 R2 mobile pressure vessel filling test question simulation test platform operation
使能伙伴,春节重大保障“不停歇”
[suggestions collection] common usage scenarios of message queue
Interview MySQL