2

我有两个表,称为帐户和交易。帐户插入数据时,我需要自动将数据插入事务。我在 MySQL 中创建了这个触发器。它给出错误 1064(语法错误)。问题是什么?

CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
  INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
    account.deposit,account.accNo,
    "Teller","Cash","Deposit");
END
4

3 回答 3

1

MySQL 与 BEGIN END 块内的分号混淆,因此您必须使用 DELIMITER 临时强制 MySQL 使用不同的分隔符。
文档:http ://dev.mysql.com/doc/refman/5.5/en/create-trigger.html

DELIMITER //
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
  INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");
END//
DELIMITER ;

您可能需要更换:

      INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),account.deposit,account.accNo,"Teller","Cash","Deposit");

和:

      INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),NEW.deposit,NEW.accNo,"Teller","Cash","Deposit");
于 2012-06-06T18:53:37.313 回答
1

这是你原来的触发器

CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
  INSERT INTO transaction VALUES (2,account.openDate,CURTIME(),
    account.deposit,account.accNo,
    "Teller","Cash","Deposit");
END

替换account.NEW.(可选:替换"'

DELIMITER $$
CREATE TRIGGER upTransaction AFTER INSERT ON account FOR EACH ROW
BEGIN
  INSERT INTO transaction VALUES (2,NEW.openDate,CURTIME(),
    NEW.deposit,NEW.accNo,
    'Teller','Cash','Deposit');
END $$
DELIMITER ;
于 2012-06-06T19:06:04.567 回答
0
CREATE
TRIGGER `event_name` AFTER INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
    -- trigger body
    -- this code is applied to every
    -- inserted/updated/deleted row
END;

好像您在“帐户”之后缺少表名。(编辑:或帐户之前的数据库名称,我不确定帐户属于什么)

于 2012-06-06T18:58:14.107 回答