简体   繁体   English

触发MySql插入或更新

[英]Trigger MySql Insert Or Update

I need a mysql trigger after insert OR Update a table i want to add to table notifications (and in future send email notifications) 插入或更新要添加到表通知中的表后(以及以后发送电子邮件通知)后,我需要一个mysql触发器

trigger insert work fine Trigger update have a problem, if I edit "word" he insert correctly, but if I edit again... i receive a error because e try to insert in table notifications the same id 触发器插入工作正常触发器更新有问题,如果我正确编辑“单词”,他会正确插入,但是如果我再次编辑...我收到错误消息,因为尝试在表通知中插入相同的ID

    table_real
    -------------
    | id | word | news |
    -------------
    | 1  | this | this |
    -------------
    | 2  | that | this |
    -------------
    | 3  | this | this |
    -------------

    tablenotifications
    ---------------------
    | id | word | news |
    ---------------------
    | 1  | this |   2   |
    ---------------------
    | 2  | that |   1   |


DELIMITER $$
CREATE
    TRIGGER `trigger_insert` AFTER INSERT
    ON `table_real` 
    FOR EACH ROW BEGIN
        INSERT INTO tablenotifications(id, word, news) VALUES (new.id, new.word, new.news);

    END$$

DELIMITER ;


DELIMITER $$
CREATE
    TRIGGER `trigger_update` AFTER UPDATE 
    ON `table_real` 
    FOR EACH ROW BEGIN

        INSERT INTO tablenotifications (id, word, news) VALUES (new.id, new.word, new.news);

    END$$

DELIMITER ;

Solution: Edit table, change my primary key 解决方案:编辑表,更改我的主键

when eciting a table, instead of inserting, update the row that corresponds to the edited id. 当引用一个表时,而不是插入,而是更新与编辑的ID对应的行。

DELIMITER $$
CREATE
TRIGGER `trigger_update` AFTER UPDATE 
ON `table_real` 
FOR EACH ROW BEGIN

    UPDATE  tablenotifications SET id=new.id,  word=new.word, news=new.news;

END$$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM