简体   繁体   English

从另一个表插入触发器减去值

[英]Insert trigger subtract value from another table

I am a newbie at sql.我是 sql 的新手。 I made a php website that user can make item purchases.我做了一个 php 网站,用户可以购买物品。 And now I want to make a trigger for when a receipt is created, it subtracts the quantity in the item stock table with the quantity sold in the receipt.现在我想在创建收据时触发一个触发器,它将项目库存表中的数量与收据中的销售数量相减。

create table if not exists Store.Receipt (
ReceiptID varchar(10) NOT NULL DEFAULT '0',
ItemID varchar(10) NOT NULL,
PriceSold int NOT NULL,
QtySold int NOT NULL,
RDate DATETIME NOT NULL,
BranchID varchar(10) NOT NULL,
EmployeeID varchar(10) NOT NULL,
MemberID varchar(10) DEFAULT "NotMember",
primary key (ReceiptID, ItemID));

alter table Store.Receipt
add constraint FK_Receipt_Member foreign key (MemberID)
references Store.MemberShip (MemberID),
add constraint FK_Receipt_Employee foreign key (EmployeeID)
references Store.Employee (EmployeeID),
add constraint FK_Receipt_Branch foreign key (BranchID)
references Store.Branch (BranchID)
on delete restrict
on update cascade;

The sql script above is for the receipt table.上面的sql脚本是针对收据表的。 The id for this table is auto generated using autoincrement.此表的 id 是使用自动增量自动生成的。 One ReceiptID can contain many items.一个 ReceiptID 可以包含多个项目。 So I am using a composite of ReceiptID and ItemID sold as the primary key.所以我使用 ReceiptID 和 ItemID 的组合作为主键。 And here is the Script for the table.这是表格的脚本。

create table if not exists Store.Item (
ItemID varchar(10) NOT NULL DEFAULT '0',
ItemName varchar(45) NOT NULL,
UnitPrice int NOT NULL,
QtyStock int NOT NULL,
SupplierID varchar(10) NOT NULL,
primary key (ItemID));

alter table Store.Item
add constraint FK_Supplier foreign key (SupplierID)
references Store.Supplier (SupplierID)
on delete restrict
on update cascade;

I have been trying to make the trigger on my own, but I am still quite unfimiliar with sql triggers.我一直在尝试自己制作触发器,但我对sql触发器仍然很不熟悉。 Here is what I came up with.这是我想出的。

DELIMITER $$
CREATE TRIGGER update_stock_trigger
AFTER INSERT ON Store.Receipt
FOR EACH ROW
BEGIN
DECLARE 
@qty numeric
@itemid varchar

@itemid = SELECT ItemID FROM Receipt WHERE ReceiptID = INSERTED.ItemID;
@qty = SELECT QtySold FROM Receipt WHERE ReceiptID = INSERTED.QtySold;

  UPDATE Item SET QtyStock = QtyStock - @qty WHERE ItemID = @itemid;
END$$
DELIMITER ;

Thank you for your help in advance.提前谢谢你的帮助。

I don't think the trigger needs to be quite so complicated as you can access the values required directly by prefixing the column name with NEW我不认为触发器需要非常复杂,因为您可以通过在列名前加上NEW直接访问所需的值

create trigger `update_stock_trigger` 
    after insert on `receipt` 
    for each row 
    begin
        update `item` set `qtystock` = `qtystock` - new.qtysold where `itemid` = new.itemid;
    end

Using a slightly simplified schema ( remove FK dependency upon Supplier table )使用稍微简化的模式(删除对Supplier表的 FK 依赖)

mysql> describe item;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| ItemID     | varchar(10) | NO   | PRI | 0       |       |
| ItemName   | varchar(45) | NO   |     | NULL    |       |
| UnitPrice  | int(11)     | NO   |     | NULL    |       |
| QtyStock   | int(11)     | NO   |     | NULL    |       |
| SupplierID | varchar(10) | NO   |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+


mysql> describe receipt;
+------------+-------------+------+-----+-----------+-------+
| Field      | Type        | Null | Key | Default   | Extra |
+------------+-------------+------+-----+-----------+-------+
| ReceiptID  | varchar(10) | NO   | PRI | 0         |       |
| ItemID     | varchar(10) | NO   | PRI | NULL      |       |
| PriceSold  | int(11)     | NO   |     | NULL      |       |
| QtySold    | int(11)     | NO   |     | NULL      |       |
| RDate      | datetime    | NO   |     | NULL      |       |
| BranchID   | varchar(10) | NO   |     | NULL      |       |
| EmployeeID | varchar(10) | NO   |     | NULL      |       |
| MemberID   | varchar(10) | YES  |     | NotMember |       |
+------------+-------------+------+-----+-----------+-------+

And populated a single Product in the item table并在item表中填充单个产品

mysql> select * from item;
+--------+----------+-----------+----------+------------+
| ItemID | ItemName | UnitPrice | QtyStock | SupplierID |
+--------+----------+-----------+----------+------------+
| 1      | bob      |        23 |      100 | 404-blah   |
+--------+----------+-----------+----------+------------+

And an initial record in the receipt table以及receipt表中的初始记录

mysql> select * from receipt;
+-----------+--------+-----------+---------+---------------------+----------+------------+-----------+
| ReceiptID | ItemID | PriceSold | QtySold | RDate               | BranchID | EmployeeID | MemberID  |
+-----------+--------+-----------+---------+---------------------+----------+------------+-----------+
| 1         | 1      |        43 |      17 | 2020-01-30 09:28:29 | Dundee   | Senga-X    | NotMember |
+-----------+--------+-----------+---------+---------------------+----------+------------+-----------+

and a subsequent second record ( showing used sql )以及随后的第二条记录(显示使用过的 sql)

INSERT INTO `Store`.`receipt` (`ReceiptID`, `ItemID`, `PriceSold`, `QtySold`, `RDate`, `BranchID`, `EmployeeID`) 
    VALUES ('2', '1', '35', '10', '2020-01-30 09:32:00', 'Glasgow', 'Senga-Y');

The trigger has updated the item table accordingly触发器已相应地更新了item

mysql> select * from item;
+--------+----------+-----------+----------+------------+
| ItemID | ItemName | UnitPrice | QtyStock | SupplierID |
+--------+----------+-----------+----------+------------+
| 1      | bob      |        23 |       73 | 404-blah   |
+--------+----------+-----------+----------+------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM