Concept
The trigger is a special kind of transaction, which listens for adding and deleting operations, and triggers additions and deletions. It is mainly used to deal with some complex business logic to ensure the linkage of data. It contains four elements: 1. Monitoring location (table) 2. Monitoring Events (Insert/update/delete) 3. Trigger Time (After/before) 4. Trigger Event (Insert/update/delete)
Grammar
CREATE TRIGGER ‘触发器名称‘ ‘触发时间(after|before)‘ ‘监听的事情(insert|update|delete) ‘ON ‘触发地点‘ FOR EACH ROW BEGIN sql1; sql2; sql3;END$
The For each row is used to identify the type of trigger because MySQL supports row-level triggers and does not support statement triggers, so you can only use the for each row. Because the trigger uses '; ' Distinguish multiple event statements, so you need to redefine the delimiter.
Instance
Modify delimiter to $
Create a Trigger
View triggers
Before you perform the action
T4 table data is as follows
T3 table data is as follows
After performing the appropriate action
Use of triggers in MySQL