更新后的MySQL触发器创建

时间:2015-11-26 20:45:32

标签: mysql database triggers

我试图在MySQL中创建一个触发器,以便从另一个表帐户自动生成表操作的条目。

这是我的表格:

CREATE TABLE Accounts (
number INT CHECK (number >= 0),
balance REAL CHECK (balance >= 0),
PRIMARY KEY (number)
);

CREATE TABLE Operations (
date DATE,
number INT REFERENCES Accounts(number),
amount REAL,
PRIMARY KEY (date, number)
);

在下面,触发我试图做的工作,产生ERROR 1064(42000):

CREATE TRIGGER OperationTrig
AFTER UPDATE OF balance ON Accounts
REFERENCING OLD ROW AS ooo NEW ROW AS nnn
FOR EACH ROW
WHEN(nnn.balance <> ooo.balance)
INSERT INTO Operations
VALUES(CURDATE(), nnn.number, nnn.balance - ooo.balance);

我哪里错了?

1 个答案:

答案 0 :(得分:0)

认为WHEN条款;我不认为它是有效的MySQL语法。同上REFERENCING子句。 (但两者都是有效的Oracle / PostgreSQL。)

试试这个:

CREATE TRIGGER OperationTrig
  AFTER UPDATE ON Accounts
  FOR EACH ROW
BEGIN
  IF (NEW.balance <> OLD.balance) THEN
    INSERT INTO Operations
      VALUES(CURDATE(), NEW.number, NEW.balance - OLD.balance);
  END IF;
END;