如果值已更改,请使用触发器插入行

时间:2013-04-06 13:46:25

标签: mysql sql triggers

我有以下“狗”表。

+----+------+-------+--------+-------+-----+
| id | Name | Color |  Size  | Hair  | Age |
+----+------+-------+--------+-------+-----+
|  1 | Fido | Red   | Big    | Long  |   4 |
|  2 | Bud  | Red   | Small  | Short |   7 |
|  3 | Sox  | Blue  | Medium | Short |   3 |
+----+------+-------+--------+-------+-----+

如果更改了一个或多个值,例如UPDATE dogs SET Name="Bud", Color="redish", Age=8 WHERE id=2;,我想在另外两个表中插入几行:

INSERT INTO audits(id,auditTable,auditPK,dateTime) VALUES (0,'dogs',2,NOW());
INSERT INTO auditFields(id,auditsId,changeField,oldValue,newValue) VALUES (0,123,'Color','Red','Redish');
INSERT INTO auditFields(id,auditsId,changeField,oldValue,newValue) VALUES (0,123,'Age',7,8);

auditsId是第一个插入的自动增量ID。请注意,名称没有插入更改,因为它仍然是“Bud”。我正在考虑规范化auditTable和changeField,但我不确定是否应该。

这可能与触发器有关吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:1)

试试这个

DELIMITER $$
CREATE TRIGGER tg_dogs_upd AFTER UPDATE ON dogs
FOR EACH ROW 
BEGIN
    SET @AID = 0;
    IF NEW.name <> OLD.name THEN
        IF @AID = 0 THEN
            INSERT INTO audits(auditTable,auditPK,`dateTime`) VALUES ('dogs', NEW.id, NOW());
            SELECT LAST_INSERT_ID() INTO @AID;
        END IF;
        INSERT INTO auditFields(auditsId,changeField,oldValue,newValue) VALUES (@AID,'Name',OLD.`name`,NEW.`name`);
    END IF;
    IF NEW.color <> OLD.color THEN
        IF @AID = 0 THEN
            INSERT INTO audits(auditTable,auditPK,`dateTime`) VALUES ('dogs', NEW.id, NOW());
            SELECT LAST_INSERT_ID() INTO @AID;
        END IF;
        INSERT INTO auditFields(auditsId,changeField,oldValue,newValue) VALUES (@AID,'Color',OLD.color,NEW.color);
    END IF;
    IF NEW.size <> OLD.size THEN
        IF @AID = 0 THEN
            INSERT INTO audits(auditTable,auditPK,`dateTime`) VALUES ('dogs', NEW.id, NOW());
            SELECT LAST_INSERT_ID() INTO @AID;
        END IF;
        INSERT INTO auditFields(auditsId,changeField,oldValue,newValue) VALUES (@AID,'Size',OLD.size,NEW.size);
    END IF;
    IF NEW.hair <> OLD.hair THEN
        IF @AID = 0 THEN
            INSERT INTO audits(auditTable,auditPK,`dateTime`) VALUES ('dogs', NEW.id, NOW());
            SELECT LAST_INSERT_ID() INTO @AID;
        END IF;
        INSERT INTO auditFields(auditsId,changeField,oldValue,newValue) VALUES (@AID,'Hair',OLD.hair,NEW.hair);
    END IF;
    IF NEW.age <> OLD.age THEN
        IF @AID = 0 THEN
            INSERT INTO audits(auditTable,auditPK,`dateTime`) VALUES ('dogs', NEW.id, NOW());
            SELECT LAST_INSERT_ID() INTO @AID;
        END IF;
        INSERT INTO auditFields(auditsId,changeField,oldValue,newValue) VALUES (@AID,'Age',OLD.age,NEW.age);
    END IF;
END$$

DELIMITER ;

更新:使用临时表的更简洁的版本

DELIMITER $$
CREATE TRIGGER tg_dogs_upd AFTER UPDATE ON dogs
FOR EACH ROW 
BEGIN
    DROP TEMPORARY TABLE IF EXISTS tempAuditFields;
    CREATE TEMPORARY TABLE tempAuditFields(changeField VARCHAR(256),oldValue VARCHAR(256),newValue VARCHAR(256));
    IF NEW.name <> OLD.name THEN
        INSERT INTO tempAuditFields VALUES ('Name',OLD.`name`,NEW.`name`);
    END IF;
    IF NEW.color <> OLD.color THEN
        INSERT INTO tempAuditFields VALUES ('Color',OLD.color,NEW.color);
    END IF;
    IF NEW.size <> OLD.size THEN
        INSERT INTO tempAuditFields VALUES ('Size',OLD.size,NEW.size);
    END IF;
    IF NEW.hair <> OLD.hair THEN
        INSERT INTO tempAuditFields VALUES ('Hair',OLD.hair,NEW.hair);
    END IF;
    IF NEW.age <> OLD.age THEN
        INSERT INTO tempAuditFields VALUES ('Age',OLD.age,NEW.age);
    END IF;

    IF (SELECT COUNT(*) FROM tempAuditFields) > 0 THEN
        INSERT INTO audits(auditTable,auditPK,`dateTime`) VALUES ('dogs', NEW.id, NOW());
        SELECT LAST_INSERT_ID() INTO @AID;
        INSERT INTO auditFields(auditsId,changeField,oldValue,newValue)
        SELECT @AID,changeField,oldValue,newValue FROM tempAuditFields;
        DROP TEMPORARY TABLE tempAuditFields;
    END IF;
END$$

DELIMITER ;