在mysql中,一个触发器调用另一个触发器来更新第一个触发器调用表会产生错误。怎么解决?

时间:2014-11-11 13:47:03

标签: mysql triggers

我有三本桌子书(id,title,numofcopies),借书(bookid,id,isst,returndt,duedt),索赔(bookid,id)。书籍存储书籍详细信息,借用商店借用详细信息,索赔商店详细信息,用于想要特定书籍的用户但由于某种原因不可用,因此他们的详细信息存储在此表中。我想要的是,一旦一本书被退回(即从借阅表中删除),它的'numofcopies'在书籍表中增加1,那么如果这本书是“声称”某人在借阅表中插入相应的信息,即问题这本书给那个人。我实现了以下触发器

CREATE TRIGGER `issue` AFTER INSERT ON `borrows`
FOR EACH ROW begin  
update books set numofcopies = numofcopies - 1 where books.bookid = new.bookid;
end 

CREATE TRIGGER `retbook` AFTER DELETE ON `borrows`
FOR EACH ROW begin  
update books set numofcopies = numofcopies + 1 where books.bookid = old.bookid;
end


CREATE TRIGGER `claimtr` AFTER UPDATE ON `books`
FOR EACH ROW begin
declare done int default false;
declare vbookid varchar (10);
declare vid varchar (10);
declare var1 CURSOR FOR select bookid,id from claims where claims.bookid = new.bookid order by dateofclaim ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open var1;
fetch var1 into vbookid,vid;
if( new.numofcopies > 1)
  then
  if ( done = false)
    then  
    delete from request where request.id = vid and request.bookid = vbookid;
    insert into borrows (id,bookid,issuedt) values (vid, vbookid, now());
  end if;
end if;
end

所有触发器都已成功创建,但是当我从借用中删除条目时,我收到以下错误:

 error 1442:Can't update table 'borrows' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

我无法想到其他任何方法,请帮助!

0 个答案:

没有答案