Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Delete values that contains foreing keys

Author  Topic 

voliveira
Starting Member

5 Posts

Posted - 2006-12-06 : 10:48:38
Hi every one,

i'd like to know the best way to delete values from tables that use foreing keys. For example.

tb_company
id | name
1 | company01
2 | company02

tb_contact
id | id_company | name_contact
1 | 2 | Craig
2 | 1 | Frank

tb_contact_phone
id | id_contact | phone
1 | 1 | 5555555
2 | 2 | 6666666


I'd like to delete the company with id 1, but I use company PK as a FK on my table tb_contact and the PK of tb_contact as FK in my tb_contact_phone.

Thanks for helping.

Regards,

Vinicius Oliveira




harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-06 : 10:57:07
So what do you want? Cascade Delete? Or you want to delete only those records which don't have child records in Contact table?

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-06 : 11:04:23
[code]delete x
from tb_contact_phone x
inner join tb_contact a on a.id = x.id_contact
where a.id_company = @CompanyID

delete tb_contact
where id_company = @CompanyID

delete tb_company
where id = @CompanyID[/code]

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

voliveira
Starting Member

5 Posts

Posted - 2006-12-06 : 11:12:03
quote:
Originally posted by harsh_athalye

So what do you want? Cascade Delete? Or you want to delete only those records which don't have child records in Contact table?

Harsh Athalye
India.
"Nothing is Impossible"



Cascade Delete. I'd like to delete the company, but i have to delete, the contact and the phone contact as well, because of the FK.
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2006-12-06 : 11:54:36
1 option....move to SQL2005....support for "cascade delete" is inbuilt.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-07 : 01:29:01
Another who doesn't read all the answers.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

voliveira
Starting Member

5 Posts

Posted - 2006-12-07 : 06:05:18
quote:
Originally posted by Peso

Another who doesn't read all the answers.


Peter Larsson
Helsingborg, Sweden



Hi Peso,

sorry for not give you some feedback.

It worked fine. Thanks for the answer.

Regards,

Vinicius Oliveira
Go to Top of Page
   

- Advertisement -