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.
| 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_companyid | name1 | company012 | company02tb_contactid | id_company | name_contact1 | 2 | Craig2 | 1 | Franktb_contact_phoneid | id_contact | phone1 | 1 | 55555552 | 2 | 6666666I'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 AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-06 : 11:04:23
|
| [code]delete xfrom tb_contact_phone xinner join tb_contact a on a.id = x.id_contactwhere a.id_company = @CompanyIDdelete tb_contact where id_company = @CompanyIDdelete tb_companywhere id = @CompanyID[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
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 AthalyeIndia."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. |
 |
|
|
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. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-07 : 01:29:01
|
| Another who doesn't read all the answers.Peter LarssonHelsingborg, Sweden |
 |
|
|
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 LarssonHelsingborg, Sweden
Hi Peso,sorry for not give you some feedback.It worked fine. Thanks for the answer.Regards,Vinicius Oliveira |
 |
|
|
|
|
|