Unix Technical Forum

Disabling or Getting Rid of Foreign Keys So I Can Drop My Table

This is a discussion on Disabling or Getting Rid of Foreign Keys So I Can Drop My Table within the SQL Server forums, part of the Microsoft SQL Server category; --> Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:54 PM
apax999@gmail.com
 
Posts: n/a
Default Disabling or Getting Rid of Foreign Keys So I Can Drop My Table

Kinda new to SQL, using SQL Server 2005.

I have some foreign keys in a couple of tables. I need to drop these
tables, but can't since I'll get the error:

Msg 3726,

Level 16, State 1, Line 1
Could not drop object 'Client' because it is referenced by a FOREIGN
KEY constraint.

I'm not sure how to disable or get rid of these foreign keys so that I
can drop my tables. I tried:

ALTER TABLE Client NOCHECK CONSTRAINT ALL

Alter Table Client Drop Column Foreign Key Boss_ID;

I went into the Object Explorer and deleted the FK lines from each
table, but still the same error.

What am I doing wrong?

Thanks for your help.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:54 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Disabling or Getting Rid of Foreign Keys So I Can Drop My Table

apax999@gmail.com (apax999@gmail.com) writes:
> Kinda new to SQL, using SQL Server 2005.
>
> I have some foreign keys in a couple of tables. I need to drop these
> tables, but can't since I'll get the error:
>
> Msg 3726,
>
> Level 16, State 1, Line 1
> Could not drop object 'Client' because it is referenced by a FOREIGN
> KEY constraint.
>
> I'm not sure how to disable or get rid of these foreign keys so that I
> can drop my tables. I tried:
>
> ALTER TABLE Client NOCHECK CONSTRAINT ALL
>
> Alter Table Client Drop Column Foreign Key Boss_ID;
>
> I went into the Object Explorer and deleted the FK lines from each
> table, but still the same error.


You need to drop the foreign keys from the tables that references
Client, not the FKs of Client itself. Say there is an Orders table
with a column ClientID. Then you need to drop that FK constraint.
Then again, this would mean that the client ids in Orders would
be hanging in thin air. Of course, if you create the table, and
repopulate, it may be OK. But don't forget to reapply the foreign
keys you dropped.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!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
Forum Jump


All times are GMT. The time now is 06:34 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com