Mysql 如何为嵌套表创建触发器
我正在尝试为表3创建触发器以插入新行每个我都有关于触发器的基本信息这是我尝试做的,但是我的错误执行被中断Mysql 如何为嵌套表创建触发器,mysql,sql,sql-server,database,Mysql,Sql,Sql Server,Database,我正在尝试为表3创建触发器以插入新行每个我都有关于触发器的基本信息这是我尝试做的,但是我的错误执行被中断 delimiter # create trigger TABLE3_INSERT_TRIGGER after insert on table2 for each row begin insert into table3 (tableZ_ID, table2_ID) values (new.tableZ_ID, new.table2_ID); end# delimiter ; 根据您的表
delimiter #
create trigger TABLE3_INSERT_TRIGGER after insert on table2
for each row
begin
insert into table3 (tableZ_ID, table2_ID) values (new.tableZ_ID, new.table2_ID);
end#
delimiter ;
根据您的表设计,tableZero中必须有可用的数据,否则它将给出与引用键相关的错误 表1->表0
如果需要更多信息,请告诉我,根据您的表设计,tableZero中必须有可用数据,否则它将给出与引用键相关的错误 表1->表0
如果您要在表2的触发器中获取更多信息,请告诉我您正在引用的
new.tableZ_ID
,但是table2
中没有tableZ_ID
列
为了获得tableZ_ID
,您必须将table2
值连接回包含tableZ_ID
列的表,该列是基于您的模式的tableZero
。考虑到您的FK关系,为了从table2
到tableZero
,您必须通过table1
。触发器的外观如下所示:
delimiter #
create trigger TABLE3_INSERT_TRIGGER after insert on table2
for each row
begin
INSERT INTO table3 (tableZ_ID, table2_ID)
SELECT t0.tableZ_ID, new.table2_ID
FROM table1 t1
INNER JOIN tableZero t0 on t1.table1_ID = t0.table1_ID
WHERE t1.table1_ID = new.table1_ID;
end#
delimiter ;
在表2的触发器中,您引用的是
new.tableZ_ID
,但table2
中没有tableZ_ID
列
为了获得tableZ_ID
,您必须将table2
值连接回包含tableZ_ID
列的表,该列是基于您的模式的tableZero
。考虑到您的FK关系,为了从table2
到tableZero
,您必须通过table1
。触发器的外观如下所示:
delimiter #
create trigger TABLE3_INSERT_TRIGGER after insert on table2
for each row
begin
INSERT INTO table3 (tableZ_ID, table2_ID)
SELECT t0.tableZ_ID, new.table2_ID
FROM table1 t1
INNER JOIN tableZero t0 on t1.table1_ID = t0.table1_ID
WHERE t1.table1_ID = new.table1_ID;
end#
delimiter ;