MySQL trigger: differentiation between after and before, mysqltrigger
After: add, delete, modify, and then trigger the data. The triggered statement is later than the added, deleted, and modified statement, which cannot affect the previous add, delete, modify, or insert order records first, then update the product quantity. Database explosion occurs when the number of products is less than the order quantity. Before. Case operation: trigger use after: when the quantity of an order exceeds the inventory quantity, the inventory is negative. This causes the so-called database explosion problem.
# Create a TRIGGER test4CREATE TRIGGER test4AFTERINSERTON 'ord 'for each rowbeginupdate goods SET num = num-new. much WHERE goods_id = new. gid; END $
Trigger using before: trigger first. After processing, add, delete, and modify the trigger. The maximum number of orders is modified based on the inventory. Of course, I just fixed a value. In fact, I can use a statement to get the dynamic inventory value.
# Create the TRIGGER test5CREATE TRIGGER test5BEFOREINSERTON 'ord 'for each rowbegin if new. much> 26 then set new. much = 26; end if; UPDATE goods SET num = num-new. much WHERE goods_id = new. gid; END $
What is the difference between the trigger before and after?
You are not familiar with triggers.
Whether it is an insert trigger or an update trigger, it has a before or after keyword.
Before is used to execute the trigger before insert or update.
After is the trigger executed after insert or update.
Example:
Create or replace trigger TR_1 before UPDATE ON TABLE
This is irrelevant to using inserted and updated in the trigger.
Oracle triggers before and after
1. The trigger is used to process each record before and after processing. That is to say, each time you process an emp record, the drop and creare emp_c tables are executed... Do not do this. Otherwise, if you insert 1000 records, the emp_c table will be rebuilt for 1000 times.
2. commit does not commit the current transaction in the trigger. In fact, the insert emp and trigger are in the same thing. On the contrary, only after this transaction ends, that is, after your insert emp commit, emp_c will actually submit.