【问题标题】:my trigger error in mysql, hen help to fix我在 mysql 中的触发错误,请帮助修复
【发布时间】:2021-09-29 05:44:42
【问题描述】:

我不明白为什么我的触发器不起作用。

第一个表:

CREATE TABLE `Payment` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `idPaymentMethod` int(20) DEFAULT NULL,
  `createAt` timestamp NOT NULL DEFAULT current_timestamp(),
  `updateAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
  `actived` tinyint(1) NOT NULL DEFAULT 1,
  `numberInvoices` int(11) DEFAULT NULL,
  `preferentialPaymentDay` int(11) DEFAULT NULL,
  `invoicesFrequency` bigint(20) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

第二张桌子:

CREATE TABLE `PaymentHistory` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `idPaymentMethod` int(20) DEFAULT NULL,
  `idPayment` int(20) DEFAULT NULL,
  `createAt` timestamp NOT NULL DEFAULT current_timestamp(),
  `updateAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
  `actived` tinyint(1) NOT NULL DEFAULT 1,
  `numberInvoices` int(11) DEFAULT NULL,
  `preferentialPaymentDay` int(11) DEFAULT NULL,
  `invoicesFrequency` bigint(20) NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

我的触发器:

DELIMITER $$
CREATE
TRIGGER `update_history` AFTER
UPDATE
    ON
    `Payment` 
FOR EACH ROW BEGIN

    UPDATE
    `PaymentHistory`
SET
    id = OLD.id,
    idPaymentMethod = old.idPaymentMethod,
    createAt = old.createAt,
    updateAt = old.updateAt,
    actived = old.actived,
    numberInvoices = old.numberInvoices

END$$

当我尝试修复某些东西时,我遇到了很多错误。

我得到的错误:

SQL 错误 [1064] [42000]:您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“END$$”附近使用的正确语法

SQL 错误 [1054] [42S22]:“字段列表”中的未知列“OLD.id”

我真的迷路了。

我需要将“Payment”表中的所有更新数据插入或更新到“PaymentHistory”表中

请问,我需要帮助。

【问题讨论】:

    标签: mysql sql triggers


    【解决方案1】:

    您需要用分号结束每个命令

    DELIMITER $$
    CREATE
    TRIGGER `update_history` AFTER
    UPDATE
        ON
        `Payment` 
    FOR EACH ROW BEGIN
    
        UPDATE
        `PaymentHistory`
    SET
        id = OLD.id,
        idPaymentMethod = old.idPaymentMethod,
        createAt = old.createAt,
        updateAt = old.updateAt,
        actived = old.actived,
        numberInvoices = old.numberInvoices;
    
    END$$
    
    DELIMITER ;
    

    example

    【讨论】:

      【解决方案2】:

      首先,感谢@nbk

      我在你的帮助下解决了

      我将de“之后”改为“之前”并添加“;”

      看;

      TRIGGER `update_history` BEFORE
      UPDATE
          ON
          `Payment` 
      FOR EACH ROW BEGIN
      
          UPDATE
          `PaymentHistory`
      SET
           idPayment = old.id
          , idPaymentMethod = old.idPaymentMethod
          , createAt = old.createAt
          , updateAt = old.updateAt
          , actived = old.actived
          , numberInvoices = old.numberInvoices;
      
      END
      

      【讨论】:

      • 不带 WHERE 子句的更新将更新 每一 行,因此您应该解决这个问题,无论您是在之前还是之后进行更新,结果都是一样
      猜你喜欢
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      相关资源
      最近更新 更多