Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何为嵌套表创建触发器_Mysql_Sql_Sql Server_Database - Fatal编程技术网

Mysql 如何为嵌套表创建触发器

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 ; 根据您的表

我正在尝试为表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 ;

根据您的表设计,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 ;