MySQL trigger trigger after and before distinguish

Source: Internet
Author: User
Tags ord

After: is the data before and after the deletion and modification, and then triggered. Trigger the statement later than the monitoring of additions and deletions, can not affect the previous additions and deletions to modify. This means that the order record is inserted first. Update the item quantity again. When the quantity of goods is less than the number of orders caused explosion.

Before: First, after the trigger, in the additions and deletions, trigger statements before the monitoring of the additions and deletions, we have the opportunity to infer, change the action that is about to happen. Case Action:Triggerusing after:Inventory is negative when the quantity of an order exceeds the quantity in stock. caused by the so-called problem of bomb-bank.
#创建触发器test4CREATE TRIGGER Test4afterinserton ' ord ' for each rowbeginupdate goods SET num= num-new.much WHERE goods_id = n Ew.gid; end$$


Triggerusing before:Trigger first, after inference processing and then the increase and deletion will not, according to inventory changes the maximum number of orders.

Of course, I'm just simply fixing a value that can actually get a dynamic inventory value with a statement.

#创建触发器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$$


MySQL trigger trigger after and before distinguish

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.