Search CTRL + K

Trigger

触发器SQL 中的一种特殊的存储过程,监听相关的 数据库 事件发生后将自动调用。

分类

从事件类型上看,触发器分为:

从触发动作上看 [1]触发器 分为:


Microsoft

A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server.[2]

There are two classes of triggers in SQL Server:

  • DDL (Data Definition Language) triggers. This class of triggers fires upon events that change the structure (like creating, modifying or dropping a table), or in certain server related events like security changes or statistics update events.
  • DML (Data Modification Language) triggers. This is the most used class of triggers. In this case the firing event is a data modification statement; it could be an insert, update or delete statement either on a table or a view.

Additionally, DML triggers have different types:

  • FOR or AFTER [INSERT, UPDATE, DELETE]: These types of triggers are executed after the firing statement ends (either an insert, update or delete).
  • INSTEAD OF [INSERT, UPDATE, DELETE]: Contrary to the FOR (AFTER) type, the INSTEAD OF triggers executes instead of the firing statement. In other words, this type of trigger replaces the firing statement. This is very useful in cases where you need to have cross database referential integrity.

  1. https://www.postgresql.org/docs/current/sql-createtrigger.html ↩︎

  2. https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-ver16 ↩︎