SQL触发器应用解析:关联表操作与实例演示

需积分: 18 6 下载量 137 浏览量 更新于2024-07-29 收藏 161KB DOC 举报
"数据库_SQL触发器实例讲解" 在数据库管理中,SQL触发器是一种重要的数据库对象,它允许数据库管理员在特定的数据操作(如INSERT、UPDATE或DELETE)发生时执行预定义的逻辑。触发器本质上是存储在数据库中的特殊类型的存储过程,当满足特定条件时自动调用,以确保数据的一致性和完整性。 在SQL Server中,触发器分为三类:AFTER INSERT、AFTER UPDATE和AFTER DELETE,分别对应于插入新记录、更新已有记录和删除记录时触发。这些触发器可以在表级别的操作上定义,以实现复杂的业务规则和约束。 例如,考虑以下两个表: 1. Student表 - 包含学生信息,如学号(StudentID)等。 2. BorrowRecord表 - 记录学生的借书信息,包括流水号(BorrowRecord)、学号(StudentID)、借书日期(BorrowDate)和归还日期(ReturnDate)。 在某些场景下,我们可能需要确保当Student表中的数据发生变化时,BorrowRecord表中的相关记录也随之更新或删除。这可以通过触发器来实现。 对于情况1,即更新Student表中的学号时,我们需要同步更新BorrowRecord表中的学号。为此,我们可以创建一个AFTER UPDATE触发器,如下所示: ```sql CREATE TRIGGER truStudent ON Student FOR UPDATE AS BEGIN IF UPDATE(StudentID) BEGIN UPDATE BorrowRecord SET StudentID = i.StudentID FROM BorrowRecord br, Deleted d, Inserted i WHERE br.StudentID = d.StudentID END END ``` 在这个触发器中,`Deleted`和`Inserted`是SQL Server提供的两个系统临时表。`Deleted`表保存了更新前的旧记录,而`Inserted`表则包含更新后的新记录。当更新操作发生时,触发器会检查`StudentID`是否发生变化,如果发生变化,则更新BorrowRecord表中的相应记录。 对于情况2,即删除已毕业学生的学号时,我们也希望删除其在BorrowRecord表中的所有记录。这可以通过一个AFTER DELETE触发器来完成: ```sql CREATE TRIGGER truStudentDelete ON Student FOR DELETE AS BEGIN DELETE FROM BorrowRecord WHERE StudentID IN (SELECT StudentID FROM Deleted) END ``` 在这个触发器中,`Deleted`表包含了被删除的记录,触发器将根据这些记录的学号删除BorrowRecord表中的对应记录。 通过这种方式,SQL触发器可以帮助我们实现数据的联动更新和维护,确保业务规则得到正确执行,即使在大量数据操作的情况下也能保持数据一致性。但需要注意的是,过度使用触发器可能导致性能下降,因此应谨慎设计和使用触发器。在实际应用中,应结合业务需求和性能优化策略来合理利用触发器。
2011-09-01 上传
sqlserver触发器例子 一﹕ 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活。所以触发器可以用来实现对表实施复杂的完整性约`束。 二﹕ SQL Server为每个触发器都创建了两个专用表﹕Inserted表和Deleted表。这两个表由系统来维护﹐它们存在于内存中而不是在数据库中。这两个表的结构总是与被该触发器作用的表的结构相同。触发器执行 完成后﹐与该触发器相关的这两个表也被删除。 Deleted表存放由于执行Delete或Update语句而要从表中删除的所有行。 Inserted表存放由于执行Insert或Update语句而要向表中插入的所有行。 三﹕Instead of 和 After触发器 SQL Server2000提供了两种触发器﹕Instead of 和After 触发器。这两种触发器的差别在于他们被激活的同﹕ Instead of触发器用于替代引起触发器执行的T-SQL语句。除表之外﹐Instead of 触发器也可以用于视图﹐用来扩展视图可以支持的更新操作。 After触发器在一个Insert,Update或Deleted语句之后执行﹐进行约束检查等动作都在After触发器被激活之前发生。After触发器只能用于表。 一个表或视图的每一个修改动作(insert,update和delete)都可以有一个instead of 触发器﹐一个表的每个修改动作都可以有多个After触发器。 四﹕触发器的执行过程 如果一个Insert﹑update或者delete语句违反了约束﹐那幺After触发器不会执行﹐因为对约束的检查是在After触发器被激动之前发生的。所以After触发器不能超越约束。 Instead of 触发器可以取代激发它的操作来执行。它在Inserted表和Deleted表刚刚建立﹐其它任何操作还没有发生时被执行。因为Instead of 触发器在约束之前执行﹐所以它可以对约束进行一些预处理。 五﹕使用T-SQL语句来创建触发器 基本语句如下﹕ create trigger trigger_name on {table_name | view_name} {for | After | Instead of } [ insert, update,delete ] as sql_statement 六﹕相关示例﹕ 1﹕在Orders表中建立触发器﹐当向Orders表中插入一条订单记录时﹐检查goods表的货品状态status是否为1(正在整理)﹐是﹐则不能往Orders表加入该订单。 create trigger orderinsert on orders after insert as if (select status from goods,inserted where goods.name=inserted.goodsname)=1 begin print 'the goods is being processed' print 'the order cannot be committed' rollback transaction --回滚﹐避免加入 end 2﹕在Orders表建立一个插入触发器﹐在添加一条订单时﹐减少Goods表相应的货品记录中的库存。 create trigger orderinsert1 on orders after insert as update goods set storage=storage-inserted.quantity from goods,inserted where goods.name=inserted.goodsname 3﹕在Goods表建立删除触发器﹐实现Goods表和Orders表的级联删除。 create trigger goodsdelete on goods after delete as delete from orders where goodsname in (select name from deleted) 4﹕在Orders表建立一个更新触发器﹐监视Orders表的订单日期(OrderDate)列﹐使其不能手工修改. create trigger orderdateupdate on orders after update as if update(orderdate) begin raiserror(' orderdate cannot be modified',10,1) rollback transaction end 5﹕在Orders表建立一个插入触发器﹐保证向Orders表插入的货品名必须要在Goods表中一定存在。 create trigger orderinsert3 on orders after insert as if (select count(*) from goods,inserted where goods.name=inserted.goodsname)=0 begin print ' no entry in goods for this order' rollback transaction end --insert 触发器 create trigger tri_infoDetails_i on info_details after insert as declare @id int begin --delete from info_details where id= select @id=id from inserted; insert into info_details_index(TYPE,TITLE,content,POST_TIME,FLAG) select type,title,content,getdate(),1 from info_details where id=@id; --update info_details_index set content=content end; -- update触发器 --select top 0 type,title,content,getdate() as post_time,1 as flag into info_details_index from info_details; create trigger tri_infoDetails_u on info_details after update as declare @id int begin if exists(select 1 from inserted) if exists(select 1 from deleted) begin select @id=id from inserted; insert into info_details_index(TYPE,TITLE,content,POST_TIME,FLAG)select type,title,content,getdate(),-1 from info_details where id=@id; insert into info_details_index(TYPE,TITLE,content,POST_TIME,FLAG)select type,title,content,getdate(),1 from info_details where id=@id; end --update info_details_index set content=content end --delete触发器 create trigger tri_infoDetails_d on info_details after delete as declare @id int begin if exists(select 1 from deleted) begin insert into info_details_index(TYPE,TITLE, POST_TIME,FLAG) select type,title, getdate(),-1 from deleted info_details ; end end