Mysql:在将复制内容插入另一个表并从触发表中删除行后触发

时间:2013-04-22 04:31:36

标签: mysql triggers

这里我想将X表的内容复制到Y表中,并且在同一行上我想从X表中删除该行。我按照以下方式尝试了它但是它没有工作。

Drop trigger if exists myTrigger;

delimiter |
create trigger myTrigger 
after insert on X 
for each row
BEGIN
    IF STRCMP(NEW.SysLogTag,"kernel:") = 0 THEN
        INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,1,1,100,NEW.Message);
    ELSEIF NEW.SysLogTag like 'ntpd[%]:' THEN
        INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,6,1,100,NEW.Message);
    ELSE
         INSERT INTO Y(logtime,moduleid,severity,messageid,message) values(NULL,4,1,100,NEW.Message);
    END IF;
    delete from X where ID=NEW.ID; //Not working.
END;|
delimiter ;

1 个答案:

答案 0 :(得分:0)

来自http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html#c8808

我的解决方案是在我要插入的表上创建一个唯一/主键(在本例中为S_ID),然后如果我的完整性检查失败,我将s_id更改为0.此方法只会创建一个duff记录的s_id为0.显然你需要INSERT忽略!

CREATE TRIGGER sanityCheck
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF something THEN
SET NEW.S_ID = 0 ;
END IF;
END;