我想创建一个TRIGGER AFTER UPDATE,它将TABLE_1中刚刚更新的行复制到TABLE_2。
此代码在更新多行后引发错误:SQL错误(1062):键'PRIMARY'重复输入'1'
我知道这段代码不对,但我不知道如何操作已更新的行。
CREATE DEFINER=`companyERP`@`%` TRIGGER `trigger` AFTER INSERT ON `Table_1`
FOR EACH ROW BEGIN
INSERT INTO Table_2 SELECT *
FROM Table_1;
END
提前感谢您的帮助。
答案 0 :(得分:0)
CREATE DEFINER=`companyERP`@`%` TRIGGER `trigger` AFTER INSERT ON `Table_1`
FOR EACH ROW BEGIN
INSERT INTO Table_2 SELECT new.Field1, newField2 ...
FROM Table_1
END
答案 1 :(得分:0)
尝试:
/*
CREATE DEFINER=`companyERP`@`%` TRIGGER `trigger` AFTER INSERT ON `Table_1`
FOR EACH ROW BEGIN
INSERT INTO Table_2 SELECT *
FROM Table_1;
END
*/
DELIMITER //
CREATE TRIGGER `trigger` AFTER UPDATE ON `Table_1`
FOR EACH ROW
BEGIN
INSERT INTO `Table_2`
(`id_table_1`, `old_value`, `created_at`)
VALUES
(OLD.`id`, OLD.`value`, NOW());
END//
DELIMITER ;