Database Video Chapter 10 Triggers

1. What is a trigger

A trigger is a stored procedure that is executed when the data in the specified table is modified. Triggers are often created to enforce the referential integrity or consistency of logically related data in different tables. Since users cannot bypass triggers, they can be used to enforce complex business rules to ensure data integrity.

Triggers are different from the stored procedures we introduced earlier. Triggers are mainly triggered by events to be executed, and stored procedures can be directly called by the name of the stored procedure. When operations such as UPDATE, INSERT, and DELETE are performed on a table, SQL Server will automatically execute the SQL statements defined by the trigger to ensure that the data processing must comply with the rules defined by these SQL statements.

2. Create an insert trigger

Insert picture description here

3. Create a delete trigger

Insert picture description here

4. Create an update trigger

Insert picture description here

5. Create a DDL trigger

Insert picture description here

6. Nested triggers

Insert picture description here

7. Recursive trigger

●Data modification at any point will trigger a series of triggers. Although it provides the ability to handle complex relationships, if the table requires the user's table to be updated in a specific order, the use of recursive triggers will cause problems

● All triggers together constitute a big transaction. Anywhere in any trigger

ROLLBACK command will cancel all data input. All data is erased, and no data will be placed in the table.

●The trigger can only recursively up to 16 levels. In other words, if the 16th flip-flop in the recursive chain activates the 17th flip-flop, then as a result, the ROLLBACK command is issued, and all data will be erased.

Guess you like

Origin blog.csdn.net/aqiuisme/article/details/113761294