Avatar of kadin
Flag for United States of America asked on

deleting from multiple tables.

I am trying to delete 1 whole row from each of 13 tables. There may or may not be a row to delete, in some of the tables. My sql below does not work. Do I have to inner join all 13 tables? The example below just has six tables. Thanks.

FROM one on
ON on.id = tw.id
INNER JOIN three th
ON tw.id = th.id
INNER JOIN four fo
ON th.id = fo.id
INNER JOIN five fi
ON fo.id = fi.id
ON fi.id = si.id
WHERE id = ?";

Avatar of undefined
Last Comment

8/22/2022 - Mon
Sharath S

You cannot delete from multiple tables at the same time by joining like you mentioned.
You need to write separate DELETE statements for each table.

Thanks for your response.

The google search I did seem to suggest that I could, unless I misunderstood.


Do you mean it is impossible in MySQL?
That will mean I have to make 13 separate calls to the database.
Neil Russell

Yes you are correct you can.

BUT ONLY when there is a full relationship that is satisfied between all of the tables in your FROM clause.  I dont think you can when there is a table that does not meet the WHERE condition and I am not sure you use JOINS like that for it either.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Neil Russell

You could of course write an AFTER DELETE Trigger that tidies up for you.

Thanks for your help.
What do you mean by full relationship? All my tables have an id column. For example, I want to delete all the rows with the id= 73. There will be no more than one row from each table with that id number and some tables may not have that id number.

Are you saying, for this to work in one sql statement, all tables must have at least one row with that id in the where clause?
Neil Russell

Yes because you can not use inner joins, outer joins etc.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

I tried some tests. It looks like your right, I can delete from multiple tables with one statement but only if the table has a row with that (id) to delete.

It seems odd SQL has this limitation. I am a little new at this, does anyone know how I might get this done in one statement? Would stored procedures be able to handle something like this?
Neil Russell

A Delete trigger would maybe be more efficient?

How would that work? Is it to complex to explain?
Your help has saved me hundreds of hours of internet surfing.
Neil Russell

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

How do I get the id number in there? For example, do I just use the word old as you did or am I supposed to replace old with something? By the way thanks for your help.
Neil Russell

old is the internal name used by MySql to denote the record that was just deleted.

Thanks. I will try to put something together and test it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.