snl snl - 1 month ago 6
SQL Question

how to drop a primary key in sql server?

how to drop a primary key with t-sql(i mean script)
like alter table tablename modify primary key..!
how to write a script to drop a primary key?
thanks in advance!!!!

Answer

To Drop any constraint this is the method

ALTER TABLE Yourtable -- Table Name
DROP CONSTRAINT PK_primarykey_name -- Primary Key name 

If you don't know the primary key constraint name then run the below script to know

SELECT CONSTRAINT_NAME  
FROM   INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE  TABLE_NAME = 'yourtable'  -- Table Name
       AND TABLE_SCHEMA = 'dbo'  -- change it if table is in some other schema 
       AND CONSTRAINT_TYPE = 'PRIMARY KEY'