图书管理系统触发器讲解学习文档格式.docx
《图书管理系统触发器讲解学习文档格式.docx》由会员分享,可在线阅读,更多相关《图书管理系统触发器讲解学习文档格式.docx(8页珍藏版)》请在冰豆网上搜索。
不能修改书库号:
createtriggerupdate_Lib_tr
onLibrary
afterupdate
as
ifupdate(LibraryNo)
begin
raiserror('
不能修改书库号'
16,2)
rollback
end
触发器验证:
Select*fromLibrary
UpdateLibrarysetLibraryNo='
4'
whereLibraryNo='
1'
(外键约束)
2、创建触发器:
为管理员表创建一个触发器,用来禁止修改管理员编号:
不能修改管理员编号:
createtriggerupdate_Wor_tr
onWorker
ifupdate(WorkerID)
不能修改管理员编号'
Select*fromWorker
UpdateWorkersetWorkerID='
103'
whereWorkerID='
104'
3、创建触发器:
为图书表创建一个触发器,用来禁止修改图书编号:
不能修改图书编号:
createtriggerupdate_Boo_tr
onBook
ifupdate(BookID)
不能修改图书编号'
Select*fromBook
UpdateBooksetBookID='
1014'
whereBookID='
1016'
4、创建触发器:
为学生表创建一个触发器,用来禁止修改学号:
不能修改学号:
createtriggerupdate_Stu_tr
onStudent
ifupdate(StudentID)
不能修改学号'
Select*fromStudent
UpdateStudentsetStudentID='
whereStudentID='
5、创建触发器:
为借阅表创建一个触发器,用来禁止删除“未还”的记录:
不能删除“未还”的记录:
createtriggerdelete_Bor_tr
onBorrow
insteadofdelete
ifexists(select*fromBorrowwhereState='
未还'
)
begin
raiserror('
不能删除“未还”的记录'
rollback
end
验证:
select*fromBorrow
deletefromBorrowwhereBorrowNo='
3'
6、创建触发器:
为借阅表创建一个触发器,当借阅表中数据被删除时,学生表中的相关数据也会被删除:
当Borrow表中数据被删除时,Student表中的相关数据也会被删除:
createtriggerdelete_Bor_Stu_tr
afterdelete
print'
当删除借阅表中记录时,学生表中的相关数据也被删除'
deletefromStudent
whereStudentIDin(selectStudentIDfromdeleted)
select*fromStudent
deletefromBorrowwhereStudentID='
2015012'
7、创建触发器:
为书籍表创建一个触发器,当书籍表中数据被删除时,删除的数据备份到另外一张表中:
备份:
select*intoBook_backupfromBookwhere1=2
createtriggerBook_delete_backup_tr
ifexists(select*fromdeleted)
insertintoBook_backupselect*fromdeleted
删除数据在Book_backup中做备份'
end
deletefromBookwhereBookID='
1011'
select*fromBook_backup