473,503 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DELETE FROM multiple tables

code green
1,726 Recognized Expert Top Contributor
I have a parent table `products` that contains all company products.
The product details are divided amongst 4-5 product details child tables
such as `paper`, `pens`, `books`, `sets`, `pens_paper`.
The 'product_ids` may be in one or more of these child tables but always in `products`.
Is a single DELETE query possible that will delete all occurences of a `product_id` in the tables where the `product_id` occurs.
Mar 20 '07 #1
3 9891
Motoma
3,237 Recognized Expert Specialist
I have a parent table `products` that contains all company products.
The product details are divided amongst 4-5 product details child tables
such as `paper`, `pens`, `books`, `sets`, `pens_paper`.
The 'product_ids` may be in one or more of these child tables but always in `products`.
Is a single DELETE query possible that will delete all occurences of a `product_id` in the tables where the `product_id` occurs.
One way to do this would be to use foreign keys between the tables and have cascading deletes in place.
Mar 20 '07 #2
code green
1,726 Recognized Expert Top Contributor
Thanks for the reply but I asked for a QUERY that performs a multiple delete based on the conditions.
Mar 28 '07 #3
code green
1,726 Recognized Expert Top Contributor
I seem to have solved it without any help.
Will post my solution
Expand|Select|Wrap|Line Numbers
  1. --delete id 100 wherever it appears across the database--
  2. DELETE FROM `table1`,`table2`,`table3` USING `table1` 
  3. LEFT JOIN `table2` ON `table2`.`id` = 100  AND any_other_condition_for_table2
  4. LEFT JOIN `table3` ON `table3`.`id` = 100 AND any_other_condition_for_table3
  5. WHERE `table1`.`id` = 100
The left join solves the problem of the possibility of the id not appearing in a certain table
Mar 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
849
by: Ray | last post by:
Hello all, I am attempting to delete multiple rows from multiple tables as follows: DELETE FROM attachments,responses,response_lines WHERE attachments.id IN(2,7,11) AND...
5
2834
by: Mike | last post by:
I am not a sql person and could use some help with a delete...here is what I want: I have the following tables/fields (only including necessary fields) answers result_id results result_id
2
1510
by: Eric Haskins | last post by:
Ok I upgraded to 4.1.12 Max so I could start using InnoDb and be able to do multiple table deletes among others. I have an app system that I need to delete info from a few tables when we delete...
4
2016
by: John Baker | last post by:
HI again: I have a query which selects records on tblPOmods on the basis of information on two other tables. I wish to delete the selected transactions, and am having a problem doing it. It...
14
8063
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I...
3
2533
by: Jeff | last post by:
In a subform I have a simple SQL statement that links an order details table to a product table. The form is used to enter order details. Most fields are obviously from the details table, with only...
1
4477
by: jpr | last post by:
Hello, My database has 5 tables. WHen I add data to one table, it runs an append query that copies three records to other 4 tables. The main table is MASTER. The data I copy are: ID, SSN and...
5
4131
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I...
5
9651
by: MRounds | last post by:
Hi Have looked through many forums but cannot find a solution for this. I have a table where i store certain dates and i want to use this table in several queries to delete records from multiple...
6
4168
by: clloyd | last post by:
I have a database with over 20 tables. I need to delete one to many record(s) in all tables by using a loop code. Some tables with have one record, multiple records or no records for the criteria...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.