在更新MySQL触发器之前将旧记录插入到其他表中

时间:2013-04-10 13:48:14

标签: mysql triggers insert

我想在我的数据库中复制旧记录。我尝试过使用触发器,但是出了点问题。 我正在使用MySQL Workbench,所以我发布了这段代码:

USE `secus_21`;
DELIMITER $$

CREATE TRIGGER `tsm_tasks_BUPD` BEFORE UPDATE ON tsm_tasks FOR EACH ROW
INSERT INTO `tsm_tasks_history`
    ( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
    ( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` );

更新活动,添加新记录,并尝试更改内容。我没有收到任何错误,所以我确信成功添加成功,但在tsm_tasks_history表中仍然没有。

编辑:

以下是所有触发器声明

-- Full Trigger DDL Statements
-- Note: Only CREATE TRIGGER statements are allowed
DELIMITER $$

USE `secus_21`$$

CREATE TRIGGER `tsm_tasks_BUPD` BEFORE UPDATE ON tsm_tasks FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
INSERT INTO `tsm_tasks_history`
    ( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
    ( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` )

$$

CREATE TRIGGER `tsm_tasks_BDEL` BEFORE DELETE ON tsm_tasks FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
INSERT INTO `tsm_tasks_history`
    ( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
    ( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` )

$$

1 个答案:

答案 0 :(得分:2)

您已将分隔符更改为$$,但未使用$$完成触发器的创建。所以声明没有完成。

USE `secus_21`;
DELIMITER $$

DROP TRIGGER IF EXISTS tsm_tasks_BUPD;
CREATE TRIGGER `tsm_tasks_BUPD` BEFORE UPDATE ON tsm_tasks FOR EACH ROW
BEGIN
INSERT INTO `tsm_tasks_history`
    ( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
    ( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` );
END $$

DROP TRIGGER IF EXISTS tsm_tasks_BDEL;
CREATE TRIGGER `tsm_tasks_BDEL` BEFORE DELETE ON tsm_tasks FOR EACH ROW
BEGIN
INSERT INTO `tsm_tasks_history`
    ( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
    ( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` );
END $$

DELIMITER ;