[英]MySQL trigger insert after insert into another table
I want to add a foreign key into tblprowareinventory
whenever I insert into tblprowareproducts
:每当我插入tblprowareinventory
时,我都想将外键添加到tblprowareproducts
:
tblProwareproducts 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 tblProware库存
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:但我收到此错误:
You can use the following CREATE TRIGGER
statement:您可以使用以下CREATE TRIGGER
语句:
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;
注意:您必须在 phpMyAdmin 上或使用以下语句删除当前触发器: DROP TRIGGER AddToInventory;
to successfully run this CREATE TRIGGER
statement.成功运行此CREATE TRIGGER
语句。
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.该TRIGGER
成功添加一个新行tblprowareinventory
与表NEW.ItemID
,但你定义的PRIMARY KEY
上inventoryID
的tblprowareinventory
表。 That's OK, but after trying INSERT
a second row on tblprowareinventory
table, you should get an error:没关系,但是在tblprowareinventory
表上尝试INSERT
第二行后,您应该收到错误消息:
#1062 - Duplicate entry '0' for key 'PRIMARY' #1062 - 键 'PRIMARY' 的重复条目 '0'
The TRIGGER
tries to INSERT
a second row on tblprowareinventory
table with 0 on the inventoryID
column. TRIGGER
尝试在tblprowareinventory
表上INSERT
第二行, tblprowareinventory
在inventoryID
列上tblprowareinventory
0。 This is not possible because 0 can only exists once on the inventoryID
column.这是不可能的,因为 0 在inventoryID
列中只能存在一次。
You can solve this issue using a AUTO_INCREMENT
on the inventoryID
column too:您也可以在inventoryID
AUTO_INCREMENT
列上使用AUTO_INCREMENT
解决此问题:
ALTER TABLE `tblprowareinventory` MODIFY `inventoryID` INT(11) NOT NULL AUTO_INCREMENT;
To INSERT
a new row on tblprowareproducts
table I used the following statement:要在tblprowareproducts
表上INSERT
新行,我使用了以下语句:
INSERT INTO `tblprowareproducts` (`ItemID`, `ItemCode`, `itemDescription`, `Strand`, `UnitCost`, `SaleCost`, `CategoryID_fk`)
VALUES (NULL, '111', '111', '111', '1', '1', '1')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.