日期:2014-05-16  浏览次数:20848 次

创建一个触发事件
SQL code

tableA:
aid name
1   tom
2   node

tableB:
bid weight aid date
1   1.5    1
当对tableA中的aid进行增、删、改时,tableB中的aid对应的进行增、删、改。

自己写了个,但是错的:
CREATE TRIGGER testref AFTER INSERT ON tableA
  FOR EACH ROW BEGIN
    INSERT INTO tableB AS w SET w.nid = NEW.nid, w.`date`= NOW();
  END
|
AFTER UPDATE ON tableA
FOR EACH ROW
BEGIN 
  UPDATE tableB SET w.nid = NEW.nid WHERE w.nid = OLD.nid;
END
|
AFTER DELETE ON ncnode
FOR EACH ROW 
BEGIN
  DELETE FROM ncweight WHERE w.nid=NEW.nid;
END;



------解决方案--------------------
写3个TRIGGER
------解决方案--------------------
分别写三个,一个一个写,三个 create trigger 不要混在一起。
------解决方案--------------------
CREATE TRIGGER testref1 AFTER INSERT ON tableA
FOR EACH ROW BEGIN
INSERT INTO tableB AS w SET w.nid = NEW.nid, w.`date`= NOW();
END
CREATE TRIGGER testref2
AFTER UPDATE ON tableA
FOR EACH ROW
BEGIN 
UPDATE tableB SET w.nid = NEW.nid WHERE w.nid = OLD.nid;
END
CREATE TRIGGER testref3
AFTER DELETE ON ncnode
FOR EACH ROW 
BEGIN
DELETE FROM ncweight WHERE w.nid=NEW.nid;
END;
------解决方案--------------------
探讨

写3个TRIGGER

------解决方案--------------------
NEW->OLD
------解决方案--------------------
delimiter $$
CREATE TRIGGER testref1 AFTER INSERT ON ncnode
FOR EACH ROW 
BEGIN
INSERT INTO ncweight SET ncweight.nid = NEW.nid, ncweight.`date`= NOW();
END$$
delimiter ;

其它的自己修改