PhpMyAdmin不处理触发器

时间:2014-01-14 13:54:41

标签: mysql triggers phpmyadmin

我目前有:

delimiter //

CREATE TRIGGER blog_creator AFTER INSERT ON news

IF (EXISTS(SELECT * FROM news WHERE headline = 'LastItem' 
THEN 
INSERT INTO `blog` (`id`, `title`, `content`) VALUES (500, 'Hi', 'An item was posted!');

delimiter ;

Phpmyadmin没有接受这个。没有错误消息,只是一个加载符号,然后什么也没SHOW TRIGGERS什么都没有显示,插入新闻也没有做任何事情。我做错了什么?

1 个答案:

答案 0 :(得分:2)

你缺少一些东西。 试试这个:

delimiter //

CREATE TRIGGER blog_creator AFTER INSERT ON news
FOR EACH ROW --always add this line
BEGIN -- start code block
    -- I think it would be faster to do the count instead of exists
    IF ((SELECT COUNT(*) FROM news WHERE headline = 'LastItem' ) > 0) 
    THEN 
    INSERT INTO `blog` (`id`, `title`, `content`) VALUES (500, 'Hi', 'An item was posted!');
    END IF; --you need to end if
END// --you need to end trigger using the new delimiter

delimiter ;

你可以在这里试试SQLFiddle:

http://sqlfiddle.com/#!2/524b4/1