MySQL: Triggers

1 trigger

  A trigger is a special stored procedure related to a table event. Its execution is not invoked by a program or started manually, but triggered by an event. Triggers are often used to enforce data integrity constraints and business rules. 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. The complete grammatical structure of a trigger in MySQL is as follows:

CREATE
    [DEFINER = user]
    TRIGGER [IF NOT EXISTS] trigger_name
    trigger_time trigger_event
    ON tbl_name FOR EACH ROW
    [trigger_order]
    trigger_body

trigger_time: { BEFORE | AFTER }

trigger_event: { INSERT | UPDATE | DELETE }

trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
  • trigger_time: Indicates the trigger trigger time. Before refers to firing the trigger before executing the trigger statement; after refers to firing the trigger after executing the trigger statement.
  • trigger_event: Indicates the trigger event. delete refers to activating the trigger whenever a delete statement deletes a row from the table; insert refers to activating the trigger whenever an insert statement inserts a row into the table; update refers to activating the trigger whenever the update statement modifies the specified column.
  • trigger_order: This is an optional parameter. If multiple triggers with the same trigger event and trigger time are defined, the default trigger order is consistent with the order in which the triggers were created. Where follows means that the currently created trigger is activated after the existing trigger; precedes means that the currently created trigger is activated before the existing trigger.
References
  1. https://zhuanlan.zhihu.com/p/158670286

Guess you like

Origin blog.csdn.net/yeshang_lady/article/details/130800952