Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
See more:
I need Single SQL Query to delete records in two tables without using trigger option and without two delete commands.

Table look like:

Table 1: Category
cat_id
cat_name

Table 2: subcategory
subcat_id
cat_id
cat_name

Thanks in advance........
Posted
Updated 17-Sep-17 23:54pm
Comments
Archana K 21-Aug-12 6:33am    
Specify foreign key for the details tables which references to the primary key of master and set Delete rule = Cascade .

Now when u delete a record from the master table all other details table record based on the deleting rows primary key value, will be deleted automatically.

So in that case a single delete query of master table can delete master tables data as well as child tables data.

Try this:
SQL
DELETE c.*, sb.*
FROM Category c
LEFT JOIN subCategory sb ON c.cat_id = sb.cat_id
WHERE c.cat_id = 1


Also have look on similar answer: How to delete multiple rows from different tables in single query[^]
 
Share this answer
 
v2
Comments
[no name] 21-Aug-12 6:24am    
But it is not possible in sql 2008!!!
Prasad_Kulkarni 21-Aug-12 6:46am    
Why so?
Maciej Los 21-Aug-12 17:12pm    
Good answer, my 5!
Prasad_Kulkarni 22-Aug-12 0:01am    
Thank you Iosmac! Glad to see you.
vikasvanvi 7-Feb-14 0:18am    
not working in sql 2008
You can go for on delete cascade :-

SQL> create table test16 (id number primary key,name varchar2(10));

Table created.


SQL> create table test17 (id number,address varchar2(10), foreign key (id) references test16(id) on
delete cascade);

Table created.


------------------------------------------------------------------------
these are the rows of the tables

SQL> select * from test16;

ID NAME
--------- ----------
100 srini
200 ram
300 sam

SQL> select * from test17;

ID ADDRESS
--------- ----------
100 add1
200 add2
300 add3

-----------------------------------------------------------------------

deleting the row in one table , so that it will delete the foreign keys of the child table.

SQL> delete from test16 where id=300;

1 row deleted.

---------------------------------------------------------------------
And checking whether the rows are deleted or not

SQL> select * from test16;

ID NAME
--------- ----------
100 srini
200 ram

SQL> select * from test17;

ID ADDRESS
--------- ----------
100 add1
200 add2
 
Share this answer
 
Hi

You can have foriegn key relationship for subcategory with delete cascade option
if you have foriegn key you can alter it or can add it

SQL
ALTER TABLE subcategory
ADD CONSTRAINT fk_category
FOREIGN KEY (cat_id)
REFERENCES  Category(cat_id)
ON DELETE CASCADE;
 
Share this answer
 
v2
hi delete two table from asp .net code

create table state
sid,
sname

create table city
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900