Skip to content
Advertisement

Insert trigger subtract value from another table

I am a newbie at sql. I made a php website that user can make item purchases. 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. The id for this table is auto generated using autoincrement. One ReceiptID can contain many items. So I am using a composite of ReceiptID and ItemID sold as the primary key. 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. 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.

Advertisement

Answer

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

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 )

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

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

And an initial record in the receipt table

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 )

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

mysql> select * from item;
+--------+----------+-----------+----------+------------+
| ItemID | ItemName | UnitPrice | QtyStock | SupplierID |
+--------+----------+-----------+----------+------------+
| 1      | bob      |        23 |       73 | 404-blah   |
+--------+----------+-----------+----------+------------+
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement