2011-05-26 128 views
1

我有这样的数据库建模, 一个母表让我们称之为table_mother和几个子表。 关系beetween table_mother和孩子的是这样的:如何从同一个查询中的多个子表中删除记录

所有表孩子的有一个外键样的名字作为母表(id_table_mother)(关系为1-> n的id_table_mother是uniq的和tbale孩子能得到的ID几个条目id_table_mother)

我想与母亲表中删除在孩子的表至极的所有记录都涉及不多,现在我尝试这样的事情

  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); 

THX

编辑:这就是我现在

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的,所以我可以“T与FOREIGH键和东西

+1

你可以使用级联的触发这里是一个有用的linkhttp://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp话题=/COM? ibm.sqls.doc/sqls362.htm看看是否有帮助 – Devjosh 2011-05-26 09:20:53

+0

sry无法到达该页面(未找到) – krifur 2011-05-26 12:09:49

+0

http:// p ublib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ib m.sqls.doc/sqls362.htm复制并粘贴到您的浏览器地址栏 – Devjosh 2011-05-26 12:25:42

回答

0

写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; 
相关问题