Can't Create MySQL TRIGGER when i use IS NOT NULL

白昼怎懂夜的黑 提交于 2020-01-05 10:04:49

问题


CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
    INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;

First post here after hours of searching.

I'm trying to insert into book_in_out using trigger after updating booking table, I was able to do this but if I update bookName the trigger will try to run.

I only want the trigger to run only when status column changes. default value for status column is NULL

Below is the error i keep getting:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5

回答1:


You may try this

delimiter //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
    IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
        INSERT INTO book_in_out (`astatus`, `bid`, `time`) VALUES(NEW.status, OLD.id, NOW());
    END IF;
END;//
delimiter ;



回答2:


Try to manually specify delimeter for your trigger so it won't be confused with semicolons used within BEGIN/END, i.e.

DELIMITER //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
    INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//


来源:https://stackoverflow.com/questions/23769837/cant-create-mysql-trigger-when-i-use-is-not-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!