Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropping Primary Key

Status
Not open for further replies.

jfokas

Programmer
Apr 28, 2002
42
0
0
I need to drop on the primary key on a table within a script but keep getting an error. The syntax should be straight-forward:

ALTER TABLE accession_2 DROP PRIMARY KEY

Yet, I get this error:

Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'PRIMARY'

This is a SQL Server 2000 database. Can anyone tell me what I'm missing. I'm sure the answer is rediculously simply but I'm not seeing it.
 
You have to drop it as a constraint.

ALTER TABLE Accession_2 DROP CONSTRAINT <Primarykeyname>

Go to the design of the table and find out what your primary key name is and insert it appropriately.

You might also want to add this code to your script to prevent an attempt to drop it if it doesn't exist (or already has been dropped):

Code:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[<primarykeyname>]') 
and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
Alter table ...



Catadmin - MCDBA, MCSA
Beware the error of pre-emptive poultry inventory!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top