触发器分为DML触发器和DDL触发器
DML触发器包含After触发器,执行insert update delete语句后会触发after触发器,会事务回滚
DML触发器还包含instead of触发器,执行insert update delete语句之前触发instead of触发器,适用于
表或视图
举例:after触发器
create trigger Borrow_Insert//往借阅表增加一条数据时,将图书表中的数量-1
on Borrow
with encryption//触发器加密
for insert
as
declare @count int
select @count=BookCount from Book
where BookId=(select BookId from inserted)
if @count>0
update Book set BookCount=BookCount-1
where BookId=(select BookId from inserted)
else
rollback tran
instead of触发器:删除视图中的数据 以 删除 借阅表中的数据 代替
update函数
create trigger Borrow_Update//往借阅表增加一条数据时,将图书表中的数量-1
on Borrow
with encryption//触发器加密
for update
as
if update(BorrowDate)
begin不允许更新BorrowDate'
print ''
end
DDL触发器
create trigger DDL_Operate_Table//禁止操作表
on database
for alter_table,drop_table
as
print '禁止操作表'
rollback
drop table Admin
create trigger DDL_Operate_database//禁止对服务器上的数据库操作
on all server
for drop_database
as
print '禁止删除数据库'
rollback
drop database HRManage