Trigger overview in the previous sections we introduce general stored procedures, that is, user-defined stored procedures and system stored procedures. This section describes a special stored procedure, that is, a trigger.
In the remaining sections, we will give a detailed introduction to the concepts, functions, and usage of the trigger, so that readers can understand how to define the trigger and create and use various trigger with different complexity.
12.5.1 concepts and functions of triggers
A trigger is a special type of stored procedure, which is different from the stored procedure we described earlier. A trigger is executed when an event is triggered, and a stored procedure can be directly called by the stored procedure name. When performing operations such as UPDATE, INSERT, and DELETE on a table, SQL Server automatically runs the SQL statement defined by the trigger, this ensures that the data processing must comply with the rules defined by these SQL statements.
The trigger is mainly used to achieve complex integrity of reference and data consistency that cannot be guaranteed by the primary key and foreign key. In addition, the trigger has many other functions:
(1) Enforce restriction)
Triggers implement more complex constraints than CHECK statements.
(2) tracking change Auditing changes
Trigger can detect operations in the database, so that you cannot specify updates or changes in the database without permission.
(3) Cascaded operation ).
The trigger can detect operations in the database and automatically cascade operations to affect the entire database. For example, a table trigger contains data operations (such as deletion, update, and insertion) on another table, which triggers the table trigger.
(4) Stored procedure invocation ).
In response to the database update trigger, the sender can call one or more stored procedures, or even operate outside the DBMS (database management system) itself by calling external processes.
It can be seen that triggers can solve advanced business rules or complex behavior restrictions and achieve custom records. For example, a trigger can identify the differences between the statuses of a table before and after data modification, and perform some processing based on the differences. In addition, multiple triggers of the same type (INSERT, UPDATE, and DELETE) of a table can process the same data operation in a variety of ways.
In general, the trigger performance is usually relatively low. When a trigger is run, the system spends most of its processing time referring to other tables because these tables are neither in memory nor on database devices, deleting and inserting tables are always in memory. It can be seen that the position of other tables referenced by the trigger determines the duration of the operation.
12.5.2 types of triggers
SQL Server 2000 supports two types OF triggers: AFTER trigger and INSTEAD trigger. The AFTER trigger is the one introduced before SQL Server 2000. This type of trigger requires that the trigger be triggered only after an operation (insert update delete) is executed and can only be defined on the table. You can define multiple triggers for the same operation on tables. For an AFTER trigger, you can define which trigger is first triggered and which one is last triggered. The system process sp_settriggerorder is usually used to complete this task.
The instead of trigger does not execute the defined operations (INSERT, UPDATE, DELETE), but only executes the trigger itself. An instead of trigger can be defined on a table or in a view, but only one instead of trigger can be defined for the same operation.