IT Community - Software Programming, Web Development and Technical Support

How to delete a row from parent table that has relationship with the child tables?

This is a discussion on How to delete a row from parent table that has relationship with the child tables? within the Database Support forums, part of the Web Development category; There are 3 tables.. 1. Parent 2. Child1 3. Child2 The tables child1 and child2 have foreign keys linked to ...


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 08-14-2007, 01:31 AM
$enthil $enthil is offline
D-Web Sr.Programmer
 
Join Date: Apr 2007
Posts: 162
$enthil is on a distinguished road
Default How to delete a row from parent table that has relationship with the child tables?

There are 3 tables..

1. Parent
2. Child1
3. Child2

The tables child1 and child2 have foreign keys linked to the parent table. Suppose if i want to delete a row from parent table but it has some other rows in child1 and child2 with foreign key relationship.

Can anyone help me how to delete the row from parent table which is having some relation with child1 and child2? and if i delete that row from parent table, the related rows in child1 and child2 will also be deleted..

Plz help me...
__________________
$enthil
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-14-2007, 02:46 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Smile Re: How to delete a row from parent table that has relationship with the child tables

Hi,
First,delete record from child1,child2 then delete record from parent table.In this way we achieve that one..........


Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-14-2007, 03:09 AM
smani smani is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 56
smani is on a distinguished road
Default Re: How to delete a row from parent table that has relationship with the child tables

hi,
in oracle Database it is available:
while you are creating foreign key, v have one option to give 'onDelete Cascade'

if you set this option then it automatically delete the child also




CREATE TABLE table_name
(column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);

refer this url:
Oracle/PLSQL: Foreign Keys with cascade delete

Regards
Manivannan.S
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-14-2007, 03:33 AM
write2ashokkumar write2ashokkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 85
write2ashokkumar is on a distinguished road
Default Re: How to delete a row from parent table that has relationship with the child tables

hi...

In mysql also we can use the "ON DELETE CASCADE" option while creating the foreign key.

Ex:


CREATE TABLE table_name
(column1 datatype,
column2 datatype ,
. . . . . . . . . . . . . .

FOREIGN KEY (column2,.....column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);


If we use this option, we can delete the parent table data without consider the child table.

Regards,
S.Ashokkumar.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-14-2007, 03:43 AM
$enthil $enthil is offline
D-Web Sr.Programmer
 
Join Date: Apr 2007
Posts: 162
$enthil is on a distinguished road
Default Re: How to delete a row from parent table that has relationship with the child tables

hi smani

I worked with ur query and it works fine.. Thanks for ur help..

I have one more doubt, am using SQL Server project in Visual studio 2005. If i deployed the project, an assembly is created and is stored in SQL Server DB. But i want to use an external assembly in my SQL Server project. Is there any way to add an external assembly in SQL Server Database?
__________________
$enthil
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-14-2007, 09:04 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: How to delete a row from parent table that has relationship with the child tables

Quote:
Originally Posted by write2ashokkumar View Post
hi...

In mysql also we can use the "ON DELETE CASCADE" option while creating the foreign key.

Ex:


CREATE TABLE table_name
(column1 datatype,
column2 datatype ,
. . . . . . . . . . . . . .

FOREIGN KEY (column2,.....column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);


If we use this option, we can delete the parent table data without consider the child table.

Regards,
S.Ashokkumar.
Hi Ashok,

Code:
CREATE TABLE parent_table
(
 TABLE_ID	    NUMBER PRIMARY KEY,
 NAMES			VARCHAR(100) 
);


CREATE TABLE child_table
(
 TABLE_ID	    NUMBER PRIMARY KEY,
 PARENT_ID		NUMBER,
 NAMES			VARCHAR(100),
 FOREIGN KEY(PARENT_ID)REFERENCES PARENT_TABLE(TABLE_ID) ON DELETE CASCADE 
);


INSERT INTO PARENT_TABLE VALUES (1,'TEST');
INSERT INTO CHILD_TABLE VALUES(1,1,'TEST');

SELECT * FROM CHILD_TABLE;
SELECT * FROM PARENT_TABLE;


DELETE FROM PARENT_TABLE WHERE TABLE_ID = 1;

SELECT * FROM CHILD_TABLE;
SELECT * FROM PARENT_TABLE;
try out that code and find that ON DELETE CASCADE option deletes the parent_table even the child record exists and also deletes including the child records.
__________________
-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
What is the difference between DELETE TABLE and TRUNCATE TABLE commands in SQL Server oxygen Database Support 6 11-23-2007 06:17 AM
Need to close parent window after closing child window. S.Vinothkumar ASP and ASP.NET Programming 6 11-05-2007 06:08 AM
Is it possible to close the parent window (main web browser window) from a child win gp_logesh HTML, CSS and Javascript Coding Techniques 1 10-29-2007 04:53 AM
How to pass the value from the child window to parent window in javascript? kingmaker HTML, CSS and Javascript Coding Techniques 1 09-04-2007 03:18 AM
How can communicate between child and parent windows in VC++ win32 application? kingmaker C and C++ Programming 1 07-30-2007 12:51 AM


All times are GMT -7. The time now is 03:48 AM.


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

SEO by vBSEO 3.0.0