MySQL Trigger summary

Source: Internet
Author: User

1. Create a new two-sheet table first

CREATE TABLE Tb_goods (

ID Int (one) not null primary key,

Goods_name varchar (+) NOT NULL,

Num INT (one) default ' 0 ');

CREATE TABLE Tb_order (

OrderID Int (one) not null primary key,

goods_id Int (11),

Onum int)

2. Trigger syntax

Create Trigger Tname

After/before Insert/update/delete on Tb_goods

For each row//This sentence is fixed

Begin

SQL statements

The new row is represented by the new column name in New.

End

3.example:

Delimiter $//encounters $ before ending avoid conflicts with semicolons

Create Trigger TR2

After insert on Tb_order

For each row

Begin

Update tb_goods set num=num-new.onum where id = new.orderid;

end$

Create Trigger TR3

After delete on Tb_order

For each row

Begin

Update tb_goods Set num = num + old.onum where id = old.goods_id; Use old to modify the previous or deleted rows

end$

Update tb_goods set num=num+old.onum-new.onum where Id=old.orderid

MySQL Trigger summary

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.