问题描述
我正在尝试创建一个触发器以根据另一张表上的插入有条件地插入新行...我似乎无法确定语法.到目前为止,这是我所拥有的:
I am trying to create a trigger to insert a new row conditionally based on an insert on another table...I can't seem to nail the syntax.Here is what I have thus far:
DELIMETER $$
CREATE TRIGGER overPricedCar
AFTER INSERT ON cars
FOR EACH ROW
BEGIN
IF (new.sellPrice > '80000' )THEN
INSERT INTO listings VALUES(new.carName,'GOLD','0',' ');
END IF;
END$$
DELIMETER ;
由于某种原因,我总是收到错误消息,它们的语法似乎还可以,我不确定哪里可能出错了.
For some reason I keep getting an error, they syntax seems to be OK, I'm not sure where I may have gone wrong.
编辑
纠正错字后,触发器有效".
After correcting the typo, the trigger 'works'.
我添加了要在触发发生时输出的注释.我已经对其进行了测试,并且输出消息被打印到屏幕上,但是触发器实际上并未完成插入操作:
I have added a comment to be output when the trigger happens.I have tested it, and the output message gets printed to the screen but the trigger does not actually complete the inserts:
DELIMITER $$
CREATE TRIGGER overPricedCar
BEFORE INSERT ON cars
FOR EACH ROW
BEGIN
IF (new.sellPrice > '80000' )THEN
INSERT INTO listings VALUES(new.carName,'GOLD','0',' ');
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = "New Gold car!"; // this line throws it off
END IF;
END$$
DELIMITER ;
此触发器运行时,我要在哪里将要打印的消息放置在屏幕上?
Where can I place the messages I want to be printed to the screen when this trigger runs?
推荐答案
错别字:
DELIMETER
^--- should be an I: DELIMITER
这篇关于mysql触发器不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!