如果父表中不存在行,我只是尝试将该行插入父表中。我得到“声明不完整。应为“;”在“插入”行。
CREATE TRIGGER SellsInsertAlcoholAdd
AFTER INSERT ON Sells
FOR EACH ROW
BEGIN
IF(NEW.`Alcohol name` NOT IN
(SELECT `Alcohol name` FROM Alcohol))
THEN INSERT INTO Alcohol VALUES(NEW.`Alcohol Name`, NEW.`Type`, NEW.ABV, NEW.Manufacturer);
END IF;
END
2条答案
按热度按时间xriantvc1#
你不必要得到所有的钱
Alcohol name
值,然后检查NEW
内在价值。相反,您可以改为使用NOT EXISTS()
.你也需要改变
DELIMITER
去别的地方(除了;
)一开始。最后,将其重新定义为;
```DELIMITER $$
CREATE TRIGGER SellsInsertAlcoholAdd
AFTER INSERT ON Sells
FOR EACH ROW
BEGIN
IF NOT EXISTS(SELECT 1 FROM Alcohol
WHERE
Alcohol name
= NEW.Alcohol name
) THENINSERT INTO Alcohol
VALUES(NEW.
Alcohol Name
, NEW.Type
, NEW.ABV, NEW.Manufacturer);END IF;
END $$
DELIMITER ;
9ceoxa922#
此外(除了madhur的答案之外),您不需要任何
IF
. 简单地做INSERT IGNORE ...
. (这是假设Alcohol name
是UNIQUE
或者PRIMARY
.)