2016-02-26 131 views
0

此代碼與我的問題非常相似。我不確定爲什麼表A沒有更新。 I SELECT * FROM A結束,A保持空白。我一直在這個小時,我不明白爲什麼我的觸發器不工作。謝謝MySQL觸發器未更新表

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; 

回答

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;