"更新后#34;在mySQL中插入后触发器触发

时间:2014-09-16 13:05:04

标签: mysql triggers

我的桌子上有2个触发器,如定义...

DELIMITER $$
CRATE TRIGGER newRecordToHistory AFTER INSERT ON myTable FOR EACH ROW
BEGIN
    IF (new.recordType = 1) THEN
        INSERT INTO myTableHistory
        (
            myTableId,
            someInformation,
            reason,
            mytimestamp,
            status
        )
        VALUES
        (
            new.myTableId,
            new.someInformation,
            new.reason,
            now(),
            'NEW'
        );
    END IF;
END$$
DELIMITER ;

DELIMITER $$
CRATE TRIGGER updateRecordToHistory AFTER UPDATE ON myTable FOR EACH ROW
BEGIN
    IF (new.recordType = 1) THEN
        INSERT INTO myTableHistory
        (
            myTableId,
            someinformation,
            reason,
            mytimestamp,
            status
        )
        VALUES
        (
            new.myTableId,
            new.someInformation,
            new.reason,
            now(),
            'UPDATED'
        );
    END IF;
END$$
DELIMITER ;

当我在myTable中插入新记录时,我在myHistoryTable中得到2条记录......

ID       someInformation     reason              mytimestamp          status
1        'This is new'       'Needed new record' 09/12/14 08:00:00    'NEW'
1        'This is new'       'Needed new record' 09/12/14 08:00:00    'UPDATED'

删除时,我也会在表格中找到记录。但是当我从myTable中删除(因此用户可以指定删除它的原因)时,我的代码处理插入到历史表中。

我只希望插入时myHistoryTable中只有一条记录,而删除时myHistoryTtable中没有额外记录(除了我手动放置的记录)。为什么会这样?我怎么能避免这个?

谢谢!

0 个答案:

没有答案