Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL Drop Primary Key

Hi

I am trying to drop a primary key in my SQL table [Machines]. The following isn't working

ALTER TABLE Machines DROP CONSTRAINT pk_Machine_Number

This is strange because with SELECT column_name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
I can see "Machine Number" so I am not sure what I am doing wrong
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

ALTER TABLE Machines DROP CONSTRAINT pk_Machine_Number
that should work...

try this, try to right click the table, and then select Script Table as > CREATE To > New Query Editor Window, see what's the CONSTRAINT name there?
Use this query to verify if that constraint really exists:
SELECT name  
FROM sys.key_constraints  
WHERE type = 'PK' AND OBJECT_NAME(parent_object_id) = N'Machines';  

Open in new window

What does "not work" means? What error message do you get?

btw, you cannot drop a PRIMARY KEY or UNIQUE constraint, when it's part of a FOREIGN KEY relation or when the table has a XML or spatial index.
Avatar of Murray Brown

ASKER

Hi Vitor. When I run your SQL I get PK__Machines__0687CEB6222177F3
Would that last bit have been added on automatically
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so this worked ALTER TABLE Machines DROP CONSTRAINT PK__Machines__0687CEB6222177F3. I used VB.net code to create the primary key so I made a mistake then
Great. Thanks once again! Excellent answer