一共有三个表:
table1:id,name
table2:id,name,table1_id table3
:
id,name,table2_id
更大的嵌套问题是不值得的,最多 3 个表是 100%。我试过这样,但它不能正常工作,它会破坏整个 table3
DELETE table1
, table2
, table3
FROM table1
INNER JOIN table2
ON table2.table1_id = table1.id INNER JOIN table3
ON table3.table2_id = table2.id WHERE table1.id = 1
选项 1(推荐):为外键
table1_id
和table2_id
.选项2:一个一个地删除记录,首先从
table3
,然后table2
最后从table1
你可以这样做:
添加约束
ALTER TABLE table2 ADD CONSTRAINT FK_table1_table2 FOREIGN KEY (table1_id) REFERENCES table1(id) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE table3 ADD CONSTRAINT FK_table2_table3 FOREIGN KEY (table2_id) REFERENCES table2(id) ON DELETE CASCADE ON UPDATE CASCADE;
我们进行拆除
从表 1 t1 中删除,其中 t1.id = 5;