MySQL:当另一个表插入一行时触发在一个表中插入新行

时间:2015-03-16 18:29:12

标签: mysql triggers insert

当行被插入到另一个表中时,我想将一个新行插入到现有表中。有很多例子,但是我无法找到一个带触发器的例子。这是我的代码。我究竟做错了什么?触发器根本不会触发:

USE `MTFDFormdB`;
DELIMITER $$
CREATE TRIGGER `StationTrouble_temp` 
AFTER INSERT ON `StationTrouble_temp` 
FOR EACH ROW INSERT INTO `StationTrouble`
(`_rowid_`,
`fullname`,
`username`,
`email5`,
`controlnumber`,
`station`,
`problemdescription`,
`appaerentcause`,
`observed`,
`reportstatus`,
`_submitted_`,
`_fromaddress_`,
`_flags_`,
`_transactid_`,
`submittername`,
`other submitter`) SELECT rowid_,
fullname,
username,
email5,
controlnumber,
station,
problemdescription,
appaerentcause,
observed,
reportstatus,
_submitted_,
_fromaddress_,
_flags_,
_transactid_,
submittername,othersubmitter
FROM StationTrouble_temp;

1 个答案:

答案 0 :(得分:0)

您可以使用new关键字从执行触发器的表中获取新插入的记录,因此它应该是

DELIMITER $$
CREATE TRIGGER `StationTrouble_temp` AFTER INSERT ON `StationTrouble_temp` 
FOR EACH ROW
 BEGIN
        INSERT INTO `StationTrouble`
        (
                `_rowid_`,
                `fullname`,
                `username`,
                `email5`,
                `controlnumber`,
                `station`,
                `problemdescription`,
                `appaerentcause`,
                `observed`,
                `reportstatus`,
                `_submitted_`,
                `_fromaddress_`,
                `_flags_`,
                `_transactid_`,
                `submittername`,
                `othersubmitter`
        )
        values
        (
            new.rowid_,
            new.fullname,
            new.username,
            new.email5,
            new.controlnumber,
            new.station,
            new.problemdescription,
            new.appaerentcause,
            new.observed,
            new.reportstatus,
            new._submitted_,
            new._fromaddress_,
            new._flags_,
            new._transactid_,
            new.submittername,
            new.othersubmitter
        );
end ; $$