MySQL触发器不更新表

时间:2016-02-26 10:04:57

标签: mysql sql database triggers

此代码与我的问题非常相似。我不确定为什么表A根本没有更新。我SELECT * FROM A在最后,A仍然是空的。我已经在这几个小时了,我无法弄清楚为什么我的扳机不起作用。感谢

CREATE TABLE A
(
aVariable int
);

CREATE TABLE B
(
bVariable int
);

delimiter //

CREATE TRIGGER AB
AFTER INSERT ON B
FOR EACH ROW
BEGIN
    IF (NEW.bVariable < 10) THEN //I also tried bVariable < 10
       INSERT INTO A VALUES (11)
    END IF;
END;

delimiter ;

INSERT INTO B VALUES (1);

SELECT * //A is still empty
FROM A;

SELECT *
FROM B;

1 个答案:

答案 0 :(得分:0)

使用正确的注释语法。请参阅9.6 Comment Syntax

DROP TABLE IF EXISTS B;
DROP TABLE IF EXISTS A;

CREATE TABLE A (
  aVariable int
);

CREATE TABLE B (
  bVariable int
);

DELIMITER //

CREATE TRIGGER AB AFTER INSERT ON B
FOR EACH ROW
BEGIN
 -- IF (NEW.bVariable < 10) THEN //I also tried bVariable < 10
    IF (NEW.bVariable < 10) THEN -- //I also tried bVariable < 10
       -- INSERT INTO A VALUES (11)
       INSERT INTO A VALUES (11);
    END IF;
END//

DELIMITER ;

INSERT INTO B VALUES (1);

--  SELECT * //A is still empty
SELECT * -- //A is still empty
FROM A;

SELECT *
FROM B;