我想在TABLE1上插入新行之后创建触发器,并在更新table1时创建更新触发器。
我有2张桌子。
在插入table1信息之后,我想触发将一些数据复制到table2的新行。 (在这种情况下,F和G为null,而id2为自动递增。)
id1,A,B,C,D,E =是table1列。 id2,id1,C,E,F,G =是table2列。 id1和id2是每个表上的主键
TABLE1(id1,A,B,C,D,E)
TABLE2(id2,id1,C,F,G)
CREATE TABLE Table1(id1 INT, A VARCHAR(10), B VARCHAR(10), C date), D VARCHAR(10), E VARCHAR(10));
CREATE TABLE Table2(id2 INT, C2 date, F VARCHAR(10), G VARCHAR(10));
GO
CREATE TRIGGER Table1_AfterInsert
AFTER INSERT ON Table1
FOR EACH ROW BEGIN
UPDATE Table2
SET C2 = NEW.C
WHERE id1 = NEW.id2;
END;
GO
INSERT INTO Table2(id2, F,G)VALUES(1,'111','red');
INSERT INTO Table2(id2, F,G)VALUES(2,'222','blue');
INSERT INTO Table2(id2, F,G)VALUES(3,'333','red');
GO
INSERT INTO Table1(id1, C)VALUES(1,'AAA'),(2,'BBB');
GO
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
GO
CREATE TRIGGER Table1_AfterUpdate
AFTER UPDATE ON Table1
FOR EACH ROW BEGIN
IF (OLD.C != NEW.C2)
THEN
UPDATE Table2
SET C2 = NEW.C
WHERE id2 = NEW.id1;
END IF;
END;
GO
UPDATE TableA
目前,如果我删除了每一行,则将所有行复制到table2中。 当我尝试为每一行放置内容时,会出现错误。
这与我的真实表有关,我在第7行附近出现错误#1064 SINTAX
CREATE TABLE man_value(id (PK), nome_documento VARCHAR(10), descricao VARCHAR(10), data_fim date, data_inicio date, color VARCHAR(10), quem_fim VARCHAR(10), quem_inicio VARCHAR(10));
CREATE TABLE calendar(id (PK),id_man INT, end_date date, tiltle VARCHAR(10), description VARCHAR(10), color VARCHAR(10), created_by VARCHAR(10), create_at (datetime));
------------------------------------------------------------------------------------------- THIS ONE IS WORKING OK
DELIMITER $$
CREATE TRIGGER man_value_calendar_AfterInsert
AFTER INSERT ON man_value
FOR EACH ROW BEGIN
INSERT INTO calendar (id_man, title, end_date, description, color,create_by)
VALUES (NEW.id, NEW.nome_documento, NEW.data_fim,NEW.descricao, '#FFD700',NEW.quem_fim);
END$$
DELIMITER ;
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- NO ERROR BUT DOES NOTHING
DELIMITER $$
CREATE TRIGGER man_value_calendar_AfterUpdate
AFTER UPDATE ON man_value
FOR EACH ROW BEGIN
IF (OLD.id = NEW.id)
THEN
UPDATE calendar
SET id_man=NEW.id, title = NEW.nome_documento, end_date=NEW.data_fim, description=NEW.descricao, color='#FFD700', create_by=NEW.quem_fim
WHERE id_man = NEW.id;
END IF;
END$$
DELIMITER ;
答案 0 :(得分:0)
已解决
创建表man_value(id(PK),nome_documento VARCHAR(10),descricao VARCHAR(10),data_fim日期,data_inicio日期,颜色VARCHAR(10),quem_fim VARCHAR(10),quem_inicio VARCHAR(10));
CREATE TABLE calendar(id (PK),id_man INT, end_date date, tiltle VARCHAR(10), description VARCHAR(10), color VARCHAR(10), created_by VARCHAR(10), create_at (datetime));
---------------------------------
DELIMITER $$
CREATE TRIGGER man_value_calendar_AfterInsert
AFTER INSERT ON man_value
FOR EACH ROW BEGIN
INSERT INTO calendar (id_man, title, end_date, description, color,create_by)
VALUES (NEW.id, NEW.nome_documento, NEW.data_fim,NEW.descricao, '#FFD700',NEW.quem_fim);
END$$
DELIMITER ;
-- -- -- -- -- -- -- -- --
DELIMITER $$
CREATE TRIGGER man_value_calendar_AfterUpdate
AFTER UPDATE ON man_value
FOR EACH ROW BEGIN
IF (OLD.id = NEW.id)
THEN
UPDATE calendar
SET id_man=NEW.id, title = NEW.nome_documento, end_date=NEW.data_fim, description=NEW.descricao, color='#FFD700', create_by=NEW.quem_fim
WHERE id_man = NEW.id;
END IF;
END$$
DELIMITER ;