IT Community - Software Programming, Web Development and Technical Support

Delete records from multiple table at a time

This is a discussion on Delete records from multiple table at a time within the Database Support forums, part of the Web Development category; Hi Buddies We can delete records from multiple table in a single query? Are is there any function We used ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-28-2008, 05:07 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Delete records from multiple table at a time

Hi Buddies

We can delete records from multiple table in a single query? Are is there any function We used to delete record from multiple table..

Thanks
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2008, 09:06 PM
kumaresan kumaresan is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 12
kumaresan is on a distinguished road
Send a message via AIM to kumaresan Send a message via Yahoo to kumaresan
Default Re: Delete records from multiple table at a time

Hi Falcon ,

In Mysql have a multiple delete is available using this we can delete records from multiple table.

consider the table 'employee' and 'dept'. we need to delete department 20 from both tables use the following code to delete.

Code:
mysql> SELECT * FROM  employee;
+----+---------+--------+---------+---------+---------------------+
| id | name    | salary | address | dept_id | join_date           |
+----+---------+--------+---------+---------+---------------------+
|  1 | kumar   |  10000 | chennai |      10 | 2007-01-14 00:00:00 |
|  2 | ramu    |  10000 | chennai |      20 | 2007-04-24 00:00:00 |
|  3 | arun    |  20000 | chennai |      10 | 2007-02-14 00:00:00 |
|  4 | karthik |  30000 | chennai |      40 | 2007-07-19 00:00:00 |
|  5 | raja    |  20000 | chennai |      10 | 2007-06-29 00:00:00 |
|  6 | kamal   |  40000 | chennai |      30 | 2007-08-14 00:00:00 |
|  7 | mani    |  50000 | chennai |      20 | 0000-00-00 00:00:00 |
|  8 | sunder  |  60000 | chennai |      30 | 0000-00-00 00:00:00 |
+----+---------+--------+---------+--------+---------+------------+
8 rows in set (0.00 sec)


mysql> SELECT * FROM  dept;
+---------+---------+
| dept_id | name    |
+---------+---------+
|      10 | DB      |
|      20 | php     |
|      30 | flash   |
|      40 | testing |
|      50 | admin   |
+---------+---------+
5 rows in set (0.00 sec)


mysql> DELETE FROM dept,employee USING dept,employee WHERE  dept.dept_id=20 AND employee.dept_id=dept.dept_id;

Query OK, 3 rows affected (0.00 sec)


mysql> SELECT * FROM  employee;
+----+---------+--------+---------+---------+---------------------+
| id | name    | salary | address | dept_id | join_date           |
+----+---------+--------+---------+---------+---------------------+
|  1 | kumar   |  10000 | chennai |      10 | 2007-01-14 00:00:00 |
|  3 | arun    |  20000 | chennai |      10 | 2007-02-14 00:00:00 |
|  4 | karthik |  30000 | chennai |      40 | 2007-07-19 00:00:00 |
|  5 | raja    |  20000 | chennai |      10 | 2007-06-29 00:00:00 |
|  6 | kamal   |  40000 | chennai |      30 | 2007-08-14 00:00:00 |
|  8 | sunder  |  60000 | chennai |      30 | 0000-00-00 00:00:00 |
+----+---------+--------+---------+--------+---------+------------+
6 rows in set (0.00 sec)



mysql> SELECT * FROM  dept;


+---------+---------+
| dept_id | name    |
+---------+---------+
|      10 | DB      |
|      30 | flash   |
|      40 | testing |
|      50 | admin   |
+---------+---------+
4 rows in set (0.00 sec)
In this delete statement FROM Class table only will delete, USING class table for support.
__________________
Thanks,
v.kumarasan@gmail.com

Last edited by kumaresan : 04-01-2008 at 10:12 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 09:19 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Delete records from multiple table at a time

hi,
This really works for me too.
Can v have any other method to delete multiple records from multiple tables at a time?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-28-2008, 11:20 PM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Re: Delete records from multiple table at a time

Hi Jey

I have another on format to delete records from multiple tables at the same time

Code:
mysql> DELETE dep.*,emp.* FROM dept as dep,employee as emo
WHERE  dep.dept_id=20 AND emp.dept_id=dep.dept_id;
Regards
Falcon

Last edited by Falcon : 03-28-2008 at 11:41 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-02-2008, 05:27 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Delete records from multiple table at a time

Hi,
Thanks for the reply.
can we use truncate instead of Delete from the above query?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-02-2008, 05:36 AM
kumaresan kumaresan is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 12
kumaresan is on a distinguished road
Send a message via AIM to kumaresan Send a message via Yahoo to kumaresan
Default Re: Delete records from multiple table at a time

hi jey,

There is no way to use truncate instead of delete from above query because truncate is only can truncate all rows.
__________________
Thanks,
v.kumarasan@gmail.com
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-03-2008, 03:26 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Delete records from multiple table at a time

hi,
is it possible to delete single row using truncate?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-07-2008, 12:12 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Delete records from multiple table at a time

Please Understand the Purpose of TRUNCATE.
TRUNCATE will not work on row by row,it applies for all the records in the specified table.
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Multiple records using single Insert Statement vadivelanshanmugam Database Support 0 03-04-2008 09:26 PM
What is the difference between DELETE TABLE and TRUNCATE TABLE commands in SQL Server oxygen Database Support 6 11-23-2007 05:17 AM
How to delete a row from parent table that has relationship with the child tables? $enthil Database Support 5 08-14-2007 08:04 AM
How can we delete master table record vadivelanvaidyanathan Database Support 2 08-08-2007 02:29 AM
How to remove duplicate records from a table? with out using distinct key? KiruthikaSambandam Database Support 6 08-06-2007 09:16 PM


All times are GMT -7. The time now is 06:16 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0