Differences between after and before in triggers

Source: Internet
Author: User

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.

 

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.