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