Concept
A trigger is a special type of stored procedure that is not called directly by the user. When you create a trigger, you define it to execute when a specific type of data modification is made to a specific table or column.
Triggers can query other tables and can contain complex SQL statements. They are primarily used to enforce compliance with complex business rules or requirements. For example, you can control whether new orders are allowed to be inserted based on the customer's current account status.
Triggers can also be used to enforce referential integrity so that when rows are added, updated, or deleted in multiple tables, the relationships defined between those tables are preserved.
Function
1) Triggers can achieve cascading changes through related tables in the database; these changes can be executed more efficiently through cascading referential integrity constraints.
2) Triggers can enforce more complex constraints than those defined with CHECK constraints. Unlike CHECK constraints, triggers can reference columns in other tables. For example, a trigger can use a SELECT from another table to compare inserted or updated data, as well as perform other operations, such as modifying data or displaying user-defined error messages.
3) Triggers can also enforce business rules
4) Triggers can also evaluate the table status before and after data modification and take countermeasures based on the differences.
Practical Application
Although triggers have many advantages, in actual project development, especially with the deepening of OOP thinking, the disadvantages of triggers have gradually become prominent. The main ones are:
1. Too many triggers make the data logic It becomes complicated
2. Data operations are relatively implicit and difficult to adjust and modify
3. The function of triggers is gradually implemented instead in code logic or transactions, which is more in line with OO ideas.
Recommendation:
Be careful when using triggers.
Syntax
CREATE TRIGGER trigger_name ON {table_name | view_name} {FOR | After | Instead of } [ insert, update,delete ] AS sql_statement
Trigger types
SQL Server includes two general types of triggers: Data Manipulation Language (DML) triggers and Data Definition Language (DDL) triggers. DML triggers can be used when an INSERT, UPDATE, or DELETE statement modifies data in a specified table or view. DDL triggers fire stored procedures in response to various DDL statements, which mainly begin with CREATE, ALTER, and DROP. DDL triggers can be used for administrative tasks such as auditing and controlling database operations.
The trigger commonly referred to is the DML trigger.
DML triggers operate on INSERT, UPDATE and DELETE statements and help enforce business rules, extending data integrity when data is modified in a table or view.
DDL triggers were added after SQL Server 2005.
DDL triggers will fire stored procedures in response to events. But unlike DML triggers, they do not fire in response to an UPDATE, INSERT, or DELETE statement against a table or view. Instead, they will fire in response to various data definition language (DDL) events. These events correspond primarily to Transact-SQL statements that begin with the keywords CREATE, ALTER, and DROP. System stored procedures that perform DDL-style operations can also fire DDL triggers.
DDL trigger usage scenarios:
To prevent certain changes to the database schema.
Want something to happen in the database in response to a change in the database schema.
To log changes or events in the database schema.
Here we only talk about DML triggers. DML triggers are divided into the following categories:
1. After trigger
After trigger requires that the trigger will be triggered only after a certain operation insert, update, delete is performed, and can only be defined on the table.
1) insert trigger
2) update trigger
3) delete trigger
2. Instead of trigger
Instead of trigger means that it does not perform its defined operations (insert, update, delete) Instead, only the trigger itself is executed. Instead of triggers, you can define them on the table or on the view.
Comparison between inserted and deleted
触发器有两个特殊的表:插入表(instered表)和删除表(deleted表)。这两张是逻辑表也是虚表。有系统在内存中创建者两张表,不会存储在数据库中。而且两张表的都是只读的,只能读取数据而不能修改数据。这两张表的结果总是与被改触发器应用的表的结构相同。当触发器完成工作后,这两张表就会被删除。Inserted表的数据是插入或是修改后的数据,而deleted表的数据是更新前的或是删除的数据。
具体应用
在触发器实际应用中,主要还是建立约束以及级联更新。在这里主要通过简单实例予以说明。
1、触发器新增
原理:
当触发INSERT触发器时,新的数据行就会被插入到触发器表和inserted表中。inserted表是一个逻辑表,它包含了已经插入的数据行的一个副本。inserted表包含了INSERT语句中已记录的插入动作。inserted表还允许引用由初始化INSERT语句而产生的日志数据。触发器通过检查inserted表来确定是否执行触发器动作或如何执行它。inserted表中的行总是触发器表中一行或多行的副本。
场景:增加学生信息时,要校验其年龄,暂定其年龄必须大于18,否则新增失败
作用:校验约束
具体实例:
--触发器新增:只允许录取18岁以上学生 IF OBJECT_ID (N'TRIGER_Students_Insert', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Students_Insert; GO CREATE TRIGGER TRIGER_Students_Insert ON Students FOR INSERT AS declare @age int select @age=COUNT(Students.ID) FROM Students INNER JOIN inserted ON Students.ID =inserted.ID PRINT @age if(@age<18) begin raiserror('学生年龄必须要大于18哦',16,8) rollback tran end
执行insert:
INSERT INTO Students(ID,Name,Age,City,MajorID) VALUES(105,'李四',16,'BeiJing',11)
执行结果:
会直接异常,返回错误信息
消息 50000,级别 16,状态 8,过程 TRIGER_Students_Insert,第 10 行 学生年龄必须要大于18哦 消息 3609,级别 16,状态 1,第 1 行 事务在触发器中结束。批处理已中止。
2、触发器更新
原理:
可将UPDATE语句看成两步操作:即捕获数据前像(before image)的DELETE语句,和捕获数据后像(after image)的INSERT语句。当在定义有触发器的表上执行UPDATE语句时,原始行(前像)被移入到deleted表,更新行(后像)被移入到inserted表。
触发器检查deleted表和inserted表以及被更新的表,来确定是否更新了多行以及如何执行触发器动作。
可以使用IF UPDATE语句定义一个监视指定列的数据更新的触发器。这样,就可以让触发器容易的隔离出特定列的活动。当它检测到指定列已经更新时,触发器就会进一步执行适当的动作,例如发出错误信息指出该列不能更新,或者根据新的更新的列值执行一系列的动作语句。
场景:
专业信息ID修改,对应的学生信息中专业ID也相应进行修改
实例实现:
--更新触发器:更新专业ID时,同时更新学生的专业信息 IF OBJECT_ID (N'TRIGER_Majors_Update', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Majors_Update; GO CREATE TRIGGER TRIGER_Majors_Update ON Majors FOR UPDATE AS IF UPDATE(ID) UPDATE Students Set MajorID=inserted.ID FROM Students,deleted,inserted WHERE Students.MajorID = deleted.ID
原始数据:
执行更新操作:
UPDATE Majors SET ID=12 WHERE ID=11
执行结果:
3、触发器删除
原理:
当触发DELETE触发器后,从受影响的表中删除的行将被放置到一个特殊的deleted表中。deleted表是一个逻辑表,它保留已被删除数据行的一个副本。deleted表还允许引用由初始化DELETE语句产生的日志数据。
使用DELETE触发器时,需要考虑以下的事项和原则:
当某行被添加到deleted表中时,它就不再存在于数据库表中;因此,deleted表和数据库表没有相同的行。
创建deleted表时,空间是从内存中分配的。deleted表总是被存储在高速缓存中。
为DELETE动作定义的触发器并不执行TRUNCATE TABLE语句,原因在于日志不记录TRUNCATE TABLE语句。
场景:学校某选修课取消。
处理逻辑:在删除课程的同时,需要删除该课程的选课信息。
触发器:
--删除触发器:删除课程时,同时删除该课程的选课信息 IF OBJECT_ID (N'TRIGER_Courses_Delete', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Courses_Delete; GO CREATE TRIGGER TRIGER_Courses_Delete ON Courses FOR DELETE AS DELETE SC FROM SC,deleted WHERE SC.CourseID = deleted.ID
原始数据:
执行课程删除操作:
DELETE FROM Courses WHERE ID=10
执行结果:
可以看到,删除课程的同时,选修课程10的选课记录也被删除。
4、Instead Of 触发器
用Instead Of触发器实现与实例3相同的功能,具体实现代码如下:
--Instead Of触发器:删除课程时,同时删除该课程的选课信息 IF OBJECT_ID (N'TRIGER_Courses_Instead_Delete', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Courses_Instead_Delete; GO CREATE TRIGGER TRIGER_Courses_Instead_Delete ON Courses Instead Of DELETE AS declare @courseId int --获取要删除的课程ID SELECT @courseId=ID FROM deleted --删除选课信息 DELETE FROM SC WHERE CourseID = @courseId --删除课程信息 DELETE FROM Courses WHERE ID=@courseId
执行删除:
--测试用例DELETE FROM Courses WHERE ID=10
测试结果:
其测试结果与实例3相同。
本文测试用例脚本:
--数据准备 --学生信息表 IF OBJECT_ID (N'Students', N'U') IS NOT NULL DROP TABLE Students; GO CREATE TABLE Students( ID int primary key not null, Name nvarchar(50), Age int, City nvarchar(50), MajorID int ) --专业信息表 IF OBJECT_ID (N'Majors', N'U') IS NOT NULL DROP TABLE Majors; GO CREATE TABLE Majors( ID int primary key not null, Name nvarchar(50) ) --课程表 IF OBJECT_ID (N'Courses', N'U') IS NOT NULL DROP TABLE Courses; GO CREATE TABLE Courses( ID int primary key not null, Name nvarchar(50) not null ) IF OBJECT_ID (N'SC', N'U') IS NOT NULL DROP TABLE SC; GO --选课表 CREATE TABLE SC( StudentID int not null, CourseID int not null, Score int ) /* 基础数据 */ --专业信息 DELETE FROM Majors INSERT INTO Majors(ID,Name) VALUES(10,'法律') INSERT INTO Majors(ID,Name) VALUES(11,'美学') --课程信息 DELETE FROM Courses INSERT INTO Courses(ID,Name) VALUES (10,'太极拳') INSERT INTO Courses(ID,Name) VALUES (11,'摄影入门') INSERT INTO Courses(ID,Name) VALUES (12,'生命科学导论') --学生信息 DELETE FROM Students INSERT INTO Students(ID,Name,Age,City,MajorID) VALUES(101,'Tom',20,'BeiJing',10) INSERT INTO Students(ID,Name,Age,City,MajorID) VALUES(103,'李明',20,'BeiJing',11) INSERT INTO Students(ID,Name,Age,City,MajorID) VALUES(104,'王涛',18,'ShangHai',11) --选课信息 DELETE FROM SC INSERT INTO SC(StudentID,CourseID) VALUES(101,10) INSERT INTO SC(StudentID,CourseID) VALUES(101,11) INSERT INTO SC(StudentID,CourseID) VALUES(102,12) --触发器新增:只允许录取18岁以上学生 IF OBJECT_ID (N'TRIGER_Students_Insert', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Students_Insert; GO CREATE TRIGGER TRIGER_Students_Insert ON Students FOR INSERT AS declare @age int select @age=COUNT(Students.ID) FROM Students INNER JOIN inserted ON Students.ID =inserted.ID PRINT @age if(@age<18) begin raiserror('学生年龄必须要大于18哦',16,8) rollback tran end --测试用例 INSERT INTO Students(ID,Name,Age,City,MajorID) VALUES(105,'李四',16,'BeiJing',11) SELECT * FROM Students --更新触发器:更新专业ID时,同时更新学生的专业信息 IF OBJECT_ID (N'TRIGER_Majors_Update', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Majors_Update; GO CREATE TRIGGER TRIGER_Majors_Update ON Majors FOR UPDATE AS IF UPDATE(ID) UPDATE Students Set MajorID=inserted.ID FROM Students,deleted,inserted WHERE Students.MajorID = deleted.ID --测试用例 UPDATE Majors SET ID=12 WHERE ID=11 SELECT * FROM Students SELECT * FROM Majors --删除触发器:删除课程时,同时删除该课程的选课信息 IF OBJECT_ID (N'TRIGER_Courses_Delete', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Courses_Delete; GO CREATE TRIGGER TRIGER_Courses_Delete ON Courses FOR DELETE AS DELETE SC FROM SC,deleted WHERE SC.CourseID = deleted.ID --测试用例 DELETE FROM Courses WHERE ID=10 --执行结果 SELECT * FROM Students SELECT * FROM Courses SELECT * FROM SC --Instead Of触发器:删除课程时,同时删除该课程的选课信息 IF OBJECT_ID (N'TRIGER_Courses_Instead_Delete', N'tr') IS NOT NULL DROP TRIGGER TRIGER_Courses_Instead_Delete; GO CREATE TRIGGER TRIGER_Courses_Instead_Delete ON Courses Instead Of DELETE AS declare @courseId int --获取要删除的课程ID SELECT @courseId=ID FROM deleted --删除选课信息 DELETE FROM SC WHERE CourseID = @courseId --删除课程信息 DELETE FROM Courses WHERE ID=@courseId --测试用例 DELETE FROM Courses WHERE ID=10 --执行结果 SELECT * FROM Students SELECT * FROM Courses SELECT * FROM SC