问题描述
|
我有一个像这样的数据库模型,
一个母表叫它table_mother,几个子表。
table_mother和childs之间的关系是这样的:
所有表子代都有一个外键名称,作为母表的ID(id_table_mother)(关系是1-> n,因为id_table_mother是uniq,而tbale child可以为id_table_mother获得多个条目)
我想删除childs表中的所有记录,这些记录不再与母表相关,现在我尝试这样
DELETE FROM tb_child_1,tb_child_2,tb_child_3
WHERE
tb_child_1.id_table_mother
AND tb_child_2.id_table_mother
AND tb_child_3.id_table_mother
NOT IN (SELECT id_table_mother FROM tb_table_mother);
谢谢
编辑:这就是我现在结束的方式
delete from tb_child_1 where id_mother not in (select id_mother from tb_mother_table);
delete from tb_child_2 where id_mother not in (select id_mother from tb_mother_table);
任何“全局”解决方案?
而且我的数据库不是innodb,所以我不能使用foreigh键和东西
解决方法
您必须构建要删除或更新时执行的FOREIGN KEY约束,才能了解有关FOREIGN KEY约束的更多信息,请访问http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
,像这样写3个查询-
DELETE
tb_child_1
FROM
tb_table_mother
LEFT JOIN
tb_child_1
ON tb_table_mother.id_table_mother = tb_child_1.id_table_mother
WHERE
tb_child_1.id_table_mother IS NULL;