How to write query to Delete the records in child table
and corresponding records in parent table
Answers were Sorted based on User's Feedback
Answer / arijitnit
delete from child_table where values=...
on delete cascade..
U have to set the foreign key as the parent super key
| Is This Answer Correct ? | 11 Yes | 7 No |
Answer / dileep
Better you create a Trigger on child table like this one
CREATE TRIGGER trDelTrigger ON [dbo].[ChildTable]
FOR DELETE
AS
DELETE FROM ParentTable WHERE ParentTable.IdColumn=
(SELECT Deleted.IdColumn FROM Deleted)
| Is This Answer Correct ? | 9 Yes | 5 No |
Answer / krishna evsg
With help of temporary tables , we can do it made easy
create table #temp_ParentIDs
(
ID int
)
GO
Insert into #temp_ParentIDs
(ID)
select Parent_id from child_tab
GO
delete from Child_tab
GO
delete from parent_tab where parent_Id In(select * from
#temp_ParentIDs)
GO
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / krishnakumar
create table employee(id int references authors(au_id) ON
DELETE CASCADE,firstname(30))
this is chilld table .in this query use on first child table
constraints values delete next parent table constrains value
delete ... this is ON DELETE CASCADE CONCEPT
| Is This Answer Correct ? | 7 Yes | 5 No |
Answer / chaitanya
***********
Delete from P_table where P_Id=C_Id
***********
This is only aplicable if you have defined the foreign key
in the child table with the constraint on Delete Cascade.
Else you will get a Integration error.
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / satyanarayana
I think using ON DELETE CASCADE we can delete the records
from both child and parent table
| Is This Answer Correct ? | 4 Yes | 4 No |
Answer / tester
Hi vaishali have u executed the query it is not working
| Is This Answer Correct ? | 6 Yes | 9 No |
Answer / surya
use this:
DELETE FROM childTable WHERE EXISTS(SELECT id FROM
parentTable WHERE parentTable .id = childTable .id where id = 1)
| Is This Answer Correct ? | 4 Yes | 7 No |
Answer / sanjay
delete details from details inner join master on
details.keyid=master.keyid
| Is This Answer Correct ? | 0 Yes | 4 No |
Answer / zackziss
delete from c from child c inner join parent
on c.childid = parentid
| Is This Answer Correct ? | 2 Yes | 11 No |
How do I connect to sql server database?
How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?
what is bit data type? and what are the information that can be stored inside a bit column?
i want only duplicates rows from coloumn ex. emp_id(colomn name)1,1,2,3,3,4,5,5. so i want only duplicates no.
What languages bi uses to achieve the goal?
How to write a script for upate the data in prod , i have 50000 row are there
what is Data Transformation Services (DTS) ?can u explain me in detail?
Explain foreign key in sql server?
It is important form e to get the information from log files of applications executed by the task scheduler? Does sql studio save these log files? : sql server management studio
I have a table Events Events containing cardno,time,id,name--each id has a cardno my requirement is every day each employee swipe the card several times i want to calculate first and last time of each card the output should be name 1 2 6 7 in out in out holiday holiday xxx 09:30 06:30 09:40 06:45 where 1,2...... are dates for example january 1,2, etc. 6 and 7 are saturday and sunday how it is posssible
What are statistics?
What is the difference between resultset and resultsetmetadata?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)