Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 10th, 2006, 04:05 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default Deleting Records Within Relational Tables

Hi All

I have quite a complex database (well I think it is) which consists of several related tables.

Now to date the only way I have performed a deletion is by individually removing the foreign key records first and then remove the primary key record.

Is their a more effective way of doing this?.. I was thinking of a stored procedure that checks all foreign key tables and if a record is referenced in the table perform a deletion... basically performing all of my individual deletions in one execution.

Your experience would be appreciated.

Rit
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
 
Old May 10th, 2006, 10:46 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Hi Rit,

I think I've had this problem before and I'm thinking it might be because I designed something wrong in my relationships. Usually, if I have a foreign key referenced like say I have a table for Users and then a separate table for phone numbers and I reference the phone numbers table with a foreign key. I will set the key to cascade on updates and deletes so when I delete a user all the entries in the foreign key table will also be delete. However, if I also reference that same data elsewhere I won't be able to delete the primary key table entry because that foreign key is also being referenced in another table. I would humbly suggest reviewing your relationships and see if they really make sense.

Thanks,
Richard

 
Old May 10th, 2006, 04:25 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a case where a triger on the master parent table may be usefull. Only on deletes. That way the code is in one place and you insure its executed with every/any delete method. Although I usually avoid triggers this is one exception for me.

 
Old May 10th, 2006, 06:35 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

I agree with rob as a last resort. There's also code-wise deletes, which are pretty simple and quick but sometimes hard to keep track of.

 
Old May 11th, 2006, 02:43 AM
Authorized User
 
Join Date: May 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to manudutt
Default

there is no need to add any sproc or any trigger for this situation , if you use constraints for your table .. If you use foreign key constraint with ON DELETE CASCADE , it will automatically delete all the referenced records in the referenced table ..

As you have said that you did it manually that shows how poorly your table is designed.

I will show it using two example table -
1. t1 has an 'id' and a 'name' column
2. t2 has 'id' and 'phone' column , t1.id is the foreign key for this table ..

CREATE TABLE t1
(
id int NOT NULL,
name varchar(30)
)
go

CREATE TABLE t2
(
id int,
phone varchar(12),
CONSTRAINT fk_1 FOREIGN KEY (id) REFERENCES t1(id) ON DELETE CASCADE)
go

Now if you try to delete data from t1 which is referenced to t2 , it will delete all such refernces.
Now say we have following data

t1
id name
----------
1 Som
2 Manu

t2
id phone
-----------
1 78787878
1 76777666
2 76776677

and then we have executed following
DELETE FROM t1 WHERE id=1
go

it will delete 1 record from t1 and two records from t2 (which are referenced)

There may be one more situation, where you dont want to delete data from t1 which is referenced. In such situation just dont use ON DELETE CASCADE , use only FOREIGN KEY constraint , it will give error if any such data is deleted which has references in other table.



 
Old May 11th, 2006, 03:11 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Thanks All for your input. First port of call is to look at the table design/ relationships.

Many thanks

Rit





Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting Duplicate Records BrianWren Pro VB Databases 1 February 22nd, 2008 01:30 PM
Deleting all the records in a table suthaharmca Beginning VB 6 4 January 2nd, 2008 06:14 PM
Deleting duplicate records ninel SQL Server 2000 3 June 14th, 2006 02:12 PM
deleting records in linked tables tico31pl Access 4 May 18th, 2006 04:10 PM
Post/Get from 2 Tables in Relational Database TSEROOGY Classic ASP Databases 1 November 16th, 2004 03:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.