Alpha
Alpha

Reputation: 127

MySQL trigger insert after insert into another table

I want to add a foreign key into tblprowareinventory whenever I insert into tblprowareproducts:

phpmyadmin tblProwareproducts

CREATE TABLE `tblprowareproducts` (
  `ItemID` int(11) NOT NULL,
  `ItemCode` varchar(30) NOT NULL,
  `itemDescription` varchar(60) NOT NULL,
  `Strand` varchar(30) NOT NULL,
  `UnitCost` double NOT NULL,
  `SaleCost` double NOT NULL,
  `CategoryID_fk` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `tblprowareproducts`
  ADD PRIMARY KEY (`ItemID`),
  ADD KEY `CategoryID_fk` (`CategoryID_fk`);


ALTER TABLE `tblprowareproducts`
  MODIFY `ItemID` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `tblprowareproducts`
  ADD CONSTRAINT `tblprowareproducts_ibfk_1` FOREIGN KEY (`CategoryID_fk`) REFERENCES `tblprowarecategory` (`PCategoryID`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

tblProwareinventory

CREATE TABLE `tblprowareinventory` (
  `inventoryID` int(11) NOT NULL,
  `ItemID_FK` int(11) NOT NULL,
  `DateOfInventory` date NOT NULL,
  `CurrentQuantity` int(11) NOT NULL,
  `TotalQuantity` int(11) NOT NULL,
  `DeliveredQuantity` int(11) NOT NULL,
  `PhysicalCount` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `tblprowareinventory`
  ADD PRIMARY KEY (`inventoryID`),
  ADD KEY `ItemID_FK` (`ItemID_FK`);


ALTER TABLE `tblprowareinventory`
  ADD CONSTRAINT `tblprowareinventory_ibfk_2` FOREIGN KEY (`ItemID_FK`) REFERENCES `tblprowareproducts` (`ItemID`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

INSERT INTO tblprowareinventory(ItemID_FK) 
VALUES ((SELECT ItemID FROM tblprowareproducts))

but I get this error:

error

Upvotes: 3

Views: 9377

Answers (1)

Sebastian Brosch
Sebastian Brosch

Reputation: 43564

You can use the following CREATE TRIGGER statement:

DELIMITER //
CREATE DEFINER = `root`@`localhost` TRIGGER AddToInventory AFTER INSERT ON tblprowareproducts
   FOR EACH ROW
   BEGIN
       INSERT INTO tblprowareinventory (ItemID_FK) VALUES (NEW.ItemID);
   END;//
DELIMITER ;

Note: You have to remove your current trigger on phpMyAdmin or with the following statement: DROP TRIGGER AddToInventory; to successfully run this CREATE TRIGGER statement.


The TRIGGER successfully add a new row to the tblprowareinventory table with the NEW.ItemID, but you defined a PRIMARY KEY on the inventoryID of tblprowareinventory table. That's OK, but after trying INSERT a second row on tblprowareinventory table, you should get an error:

#1062 - Duplicate entry '0' for key 'PRIMARY'

The TRIGGER tries to INSERT a second row on tblprowareinventory table with 0 on the inventoryID column. This is not possible because 0 can only exists once on the inventoryID column.

You can solve this issue using a AUTO_INCREMENT on the inventoryID column too:

ALTER TABLE `tblprowareinventory` MODIFY `inventoryID` INT(11) NOT NULL AUTO_INCREMENT;

To INSERT a new row on tblprowareproducts table I used the following statement:

INSERT INTO `tblprowareproducts` (`ItemID`, `ItemCode`, `itemDescription`, `Strand`, `UnitCost`, `SaleCost`, `CategoryID_fk`) 
    VALUES (NULL, '111', '111', '111', '1', '1', '1')

Upvotes: 2

Related Questions