Mysql插入触发器不起作用

时间:2016-03-05 08:26:15

标签: mysql triggers

你能帮我解决以下问题: 我试图在Mysql 5.5中创建以下触发器时出错 触发器如下:

CREATE OR REPLACE TRIGGER bookTakenDate  
AFTER INSERT ON readers_books  
for each row  
begin  
    update readers_books  
    set date_supposed_taken = new.date_given  
    where given_book_id = new.given_book_id;  
end;

错误如下:

  

SQL(1064):SQL语法中存在错误;在第1行。

表的结构如下:

CREATE TABLE IF NOT EXISTS readers_books (
    GIVEN_BOOK_ID int(11) NOT NULL AUTO_INCREMENT,
    books_ID int(11) DEFAULT NULL,
    readers_ID int(11) DEFAULT NULL,
    date_given date DEFAULT NULL,
    date_supposed_taken date DEFAULT NULL,
    PRIMARY KEY (GIVEN_BOOK_ID),
    KEY books_ID (books_ID),
    KEY readers_ID (readers_ID),
    CONSTRAINT readers_books_ibfk_1 FOREIGN KEY (books_ID) REFERENCES BOOKS (ID),
    CONSTRAINT readers_books_ibfk_2 FOREIGN KEY (readers_ID) REFERENCES READERS (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

实际上,触发器应该执行以下操作。它应该以自动方式将+7天插入date_supposed_taken列

2 个答案:

答案 0 :(得分:0)

对文档https://dev.mysql.com/doc/refman/5.5/en/create-trigger.html的反应看起来像是"创建或替换"语法不受支持。

应该有效

delimiter //
CREATE TRIGGER bookTakenDate
AFTER INSERT ON readers_books
for each row
begin
update readers_books
set date_supposed_taken = new.date_given
where given_book_id = new.given_book_id;
end;//

答案 1 :(得分:0)

您需要定义不同的分隔符,并DROP TRIGGER代替CREATE而不是CREATE OR REPLACE。{/ p>

DELIMITER $$ -- change delimiter

DROP TRIGGER /*!50032 IF EXISTS */ `bookTakenDate`$$

CREATE TRIGGER bookTakenDate
AFTER INSERT ON readers_books
FOR EACH ROW
BEGIN
    UPDATE readers_books
    SET date_supposed_taken = new.date_given
    WHERE given_book_id = new.given_book_id;

END;$$

DELIMITER ; -- set original delimiter