MySQL trigger: differentiation between after and before, mysqltrigger

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.