Differences between after and before in triggers
After is triggered after data is added, deleted, and modified.
The statement that is triggered is later than the monitor's add, delete, and modify operations.
Before is triggered first, and then added, deleted, and modified.
The triggered statement takes place prior to the addition, deletion, and modification of monitoring. We have the opportunity to determine the forthcoming operation.
Typical cases:
Judge the order. If the order quantity is greater than 5, the order is regarded as a malicious order and the number of goods ordered is forcibly changed to 5.
Demo
1. Create a inventory table as follows:
2. Create an order table without data.
Third, first change the terminator to $ Java code
Delimiter $
4. Create a trigger: Java code
Create trigger t1
Before insert on o
For each row
Begin
If new. much> 5 then
Set new. much = 5;
End if;
Update g set number = number-new. much where id = new. gid;
End $
5. place an order and view the order table and inventory table
Even if you place an order for 10 cats, there are a maximum of 5 cats in the order table, and 5 fewer cats in the inventory table.