MySQL basic three triggers

Source: Internet
Author: User

Product List--Table "goods" DDL:

CREATE TABLE goods (
gidInt (one) is not NULL,
namevarchar (DEFAULT) NULL,
numsmallint (6) DEFAULT NULL,
PRIMARY KEY ( gid )
) Engine=innodb DEFAULT Charset=utf8;

Order table -table "orders" DDL:

CREATE TABLE orders (
oidInt (one) is not NULL,
gidInt (one) DEFAULT NULL,
muchsmallint (6) DEFAULT NULL,
PRIMARY KEY ( oid )
) Engine=innodb DEFAULT Charset=utf8;

One, insert trigger:

Demand: When a customer buys a product, the inventory quantity of the item is automatically reduced.

Create trigger Trigerinsertorder after insert on orders for each row

Begin

Update goods set Num=num-new.much where Gid=new.gid;

End

Note: New represents a new row for the Orders table.

Second, delete trigger:

Requirements: After the customer cancels the order, the inventory quantity of the commodity table increases automatically.

Create trigger Triggerdeleteorder after delete on orders for each row

Begin

Update goods set Num=num+old.much where Gid=old.gid

End

Note: Old indicates that the Orders table deletes rows.

MySQL basic three triggers

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.