Reputation: 335
i have 3 tables and i dont want define any foreign key in my tables. my tables structure are like below: tables diagram
i have written this query :
delete relativedata, crawls, stored
from relativedata inner join
crawls
on relativedata.crawl_id = crawls.id and
relativedata.id = ? inner join
stored
on stored.crawl_id = crawls.id
this query works for me unless one of tables has no records. now how can i do this delete in 3 tables in 1 query?
Upvotes: 4
Views: 8965
Reputation: 2162
If it works if all tables have records, try using LEFT JOIN instread of INNER JOIN. Also, You had some mess with Your joins ON conditions. Try it like this:
delete
relativedata, crawls, stored
from
relativedata
LEFT join crawls on relativedata.crawl_id = crawls.id
LEFT join stored on relativedata.crawl_id = stored.crawl_id
WHERE
relativedata.id = ?
Also, foregin keys are good thing, and not using them is generally bad idea. Yes, they seems to be annoying at first, but try to focus on WHEN they annoy You. Most of the times they do it when You are meddling with data in a way You should not, and without them You wloud cause data incostincency in Your DB.
But, it is just my opinion.
Upvotes: 4