I have a 9 table db set up like this.
TableA
acct_id
TableB
tabB_id
tabA_id_fk
TableC
tabC_id
tabB_id_fk
TableD
tabD_id
tabB_id_fk
TableE
tabE_id
tabB_id_fk
I’m sure you see the pattern by now. The second table reference the first and all others reference the second. The second table (TableB) has seven tables which reference it through foreign keys. One of the tables is one-to-many with TableB, let’s say TableD. I want to delete the corresponding rows in all the tables with one query. I’ve searched for awhile and can’t find an answer. Here is the SQL statement I’m using.
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=?
The query does not return any errors but it also doesn’t delete any rows. What could be the problem?
Should I just use nine seperate delete queries, one for each table?
Thanks everyone.
My first answer assumes InnoDB. This answer does this for MyISAM.
delete in mysql
Your query could be correct, I don’t have your tables, but test with