我试图在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);
我哪里错了?
答案 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;