Mysql-Mysql database trigger execution failed

Source: Internet
Author: User
The Mysql database has three tables: order, orderitem, and goods. the order and orderitem are associated with the orderid field of orderitem through the order ID field; goods and orderitem are associated with the goodsid field of orderitem through the ID field of goods. now you want to create a trigger. When an order is confirmed, the remaining quantity and sale quantity of all products in this order are changed accordingly. when the value of the isvalid field in the order table is changed to Y, the store and salecount values in the goods table are updated, write trigger... mysql

The Mysql database has three tables: order, orderitem, and goods. the order and orderitem are associated with the orderid field of orderitem through the order ID field; goods and orderitem are associated with the goodsid field of orderitem through the ID field of goods. now you want to create a trigger. When an order is confirmed, the remaining quantity and sale quantity of all products in this order are changed accordingly. when the value of the isvalid field in the order table is changed to Y, the store and salecount values in the goods table are updated, why can't I implement the writing trigger? In addition, the script prompts how to use SELECT * FROM information_schema.TRIGGERS; No trigger found?

Delimiter |
Drop trigger if exists updatestore |
Create trigger updatestore after update on order for each row
Begin
Declare numorder int (11 );
Set numorder = (select count (count) from orderitem where orderid = old. id );
If new. isvalid = 'y' and old. isvalid! = 'Y' then
Update goods set store = store-@ numorder, salecount = salecount + @ numorder where id in (select goodsid from zsorderitem where orderid = old. id );
End if;
If new. isvalid! = 'Y' and old. isvalid = 'y' then
Update goods set store = store + @ numorder, salecount = salecount-@ numorder where id in (select goodsid from orderitem where orderid = old. id );
End if;
End |

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.