?
Solved

How to delete a primary key in a table in SQL Server?

Posted on 2008-10-21
2
Medium Priority
?
1,255 Views
Last Modified: 2012-05-05
I have a table: Company and I want to delete its primary ID, but below statement is not working:
#1
ALTER TABLE Company DROP CONSTRAINT pk_Company

When I checked the table properties in SQL Server and use below statement:
#2
ALTER TABLE Company DROP CONSTRAINT pk_Company__725BF7F6
then it will work.

My question is: how to write a script to delete the primary key of a table, just as simple as #1? Since if I want to write a script to be deployed to production server to delete a primary key, I couldn't know the exact name: pk_Company__725BF7F6 before viewing production.

I hope my question is clear and thanks for any help.
0
Comment
Question by:heyday2004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Expert Comment

by:Muhammad Kashif
ID: 22765639
You can get the name of primary key constraint

SELECT Constraint_Name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = 'TableName'

0
 
LVL 11

Accepted Solution

by:
Louis01 earned 2000 total points
ID: 22765673

DECLARE @table nvarchar(255);
DECLARE @sql nvarchar(1024);
 
SET @table = 'Company';
 
SET @sql = (select 'ALTER TABLE [' + @table + '] DROP CONSTRAINT [' + [name] + ']'
			  from sys.objects 
			 where parent_object_id in (select object_id 
										  from sys.objects 
										 where [name] = @table)
			   and type='PK');
 
EXEC sp_executesql @sql;

Open in new window

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question