多重删除不起作用

时间:2011-11-22 19:15:04

标签: mysql

我有一个像这样设置的9表数据库。

表A ACCT_ID

表B tabB_id tabA_id_fk

表C tabC_id tabB_id_fk

提交 tabD_id tabB_id_fk

TableE tabE_id tabB_id_fk

我相信你现在看到的模式。第二个表引用第一个表,所有其他表引用第二个表。第二个表(TableB)有七个表,通过外键引用它。其中一个表是TableB的一对多,比方说TableD。我想用一个查询删除所有表中的相应行。我搜索了一段时间,但找不到答案。这是我正在使用的SQL语句。

DELETE FROM photos, location, contacts, messages, stats, viewed_by, ethnicity,  profile, members
USING photos INNER JOIN location 
INNER JOIN contacts 
INNER JOIN messages 
INNER JOIN stats 
INNER JOIN viewed_by 
INNER JOIN ethnicity 
INNER JOIN profile 
INNER JOIN members 
WHERE photos.profile_id_fk = profile.profile_id 
AND location.profile_id_fk = profile.profile_id 
AND stats.profile_id_fk = profile.profile_id 
AND viewed_by.profile_id_fk = profile.profile_id 
AND ethnicity.profile_id_fk = profile.profile_id 
AND profile.member_id_fk = members.member_id 
AND members.member_id=?

查询不会返回任何错误,但也不会删除任何行。可能是什么问题?

我应该只使用九个单独的删除查询,每个表一个吗?

谢谢大家。

2 个答案:

答案 0 :(得分:0)

您应该使用on delete cascade创建外键,这使得引用外键的所有表都会删除它的值。

阅读foreign keys constraints特别是此部分

  

InnoDB拒绝任何尝试创建的INSERT或UPDATE操作   如果没有匹配项,则子表中的外键值   父表中的候选键值。当UPDATE或DELETE时   operation会影响父表中具有匹配项的键值   子表中的行,结果取决于引用操作   使用FOREIGN KEY的ON UPDATE和ON DELETE子条款指定   条款。 InnoDB支持关于要采取的行动的五个选项。   如果未指定ON DELETE或ON UPDATE,则默认操作为   RESTRICT。

CASCADE: Delete or update the row from the parent table, and automatically delete or update the matching rows in the child table.
     

支持ON DELETE CASCADE和ON UPDATE CASCADE。之间   两个表,没有定义几个行动的ON UPDATE CASCADE子句   在父表或子表中的同一列上。       注意

Currently, cascaded foreign key actions do not activate triggers.

答案 1 :(得分:0)

我的第一个回答是假设InnoDB。这个答案是为MyISAM做的。 delete in mysql

> desc parent; desc child_1; desc child_2;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   | PRI | NULL    |       |
| name  | varchar(100) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(11)      | NO   | PRI | NULL    |       |
| parent_id_fk | int(11)      | YES  |     | NULL    |       |
| name         | varchar(100) | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(11)      | NO   | PRI | NULL    |       |
| parent_id_fk | int(11)      | YES  |     | NULL    |       |
| name         | varchar(100) | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

> select * from parent; select * from child_1; select * from child_2;
+----+------------+
| id | name       |
+----+------------+
|  1 | Andreas    |
|  2 | Wederbrand |
+----+------------+
2 rows in set (0.00 sec)

+----+--------------+-----------+
| id | parent_id_fk | name      |
+----+--------------+-----------+
|  1 |            1 | Child 1 1 |
|  2 |            1 | Child 1 2 |
|  3 |            2 | Child 2 1 |
|  4 |            2 | Child 2 2 |
+----+--------------+-----------+
4 rows in set (0.00 sec)

+----+--------------+-----------+
| id | parent_id_fk | name      |
+----+--------------+-----------+
|  1 |            1 | Child 1 1 |
|  2 |            1 | Child 1 2 |
|  3 |            2 | Child 2 1 |
|  4 |            2 | Child 2 2 |
+----+--------------+-----------+
4 rows in set (0.00 sec)

> delete p, c1, c2 from parent p, child_1 c1, child_2 c2 where p.id = c1.parent_id_fk and p.id = c2.parent_id_fk and p.id = 2;
Query OK, 5 rows affected (0.33 sec)

你的查询可能是正确的,我没有你的表,但是用

进行测试
DELETE photos, location, contacts, messages, stats, viewed_by, ethnicity,  profile, members
FROM photos 
INNER JOIN location 
INNER JOIN contacts 
INNER JOIN messages 
INNER JOIN stats 
INNER JOIN viewed_by 
INNER JOIN ethnicity 
INNER JOIN profile 
INNER JOIN members 
WHERE photos.profile_id_fk = profile.profile_id 
AND location.profile_id_fk = profile.profile_id 
AND stats.profile_id_fk = profile.profile_id 
AND viewed_by.profile_id_fk = profile.profile_id 
AND ethnicity.profile_id_fk = profile.profile_id 
AND profile.member_id_fk = members.member_id 
AND members.member_id=?
相关问题