2

我想在我的数据库中复制旧记录。我尝试使用触发器,但有问题。我正在使用 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` )

$$
4

1 回答 1

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 ;
于 2013-04-10T13:54:16.287 回答