Please enable Javascript for better experience...
How to remove primary key in Sql Server
By Big | Jan 5, 2015 | In Tips | Total Views [ 1506 ]
Taged In
(1 Like)
Rate

I have seen in most of the forums putting my sql command instead of sql command to remove primary key constraint. Please don't get confused between MySql and Sql commands as both are different from each other. Let's see how to remove primary key constraint from the table.

Introduction

Every table must have a primary key definition. This can be single or group of two or more, I mean composite primary key. For more details on composite primary key please click on the link. Let's see how to delete a primary key from a table.

Code

We use alter and drop command to delete primary key constraint. Let's create one first then we will delete that. I am creating a table and adding primary key to it. For more details on how to add a primary key please click on this link.

Creating a table

Let's create a table and add primary key constraint and name it PK_ID.

CREATE TABLE MyTable(ID int not null, FirstName varchar(50), LastName varchar(50), CONSTRAINT PK_ID PRIMARY KEY (ID))

Remove primary key

Now let's try to delete it. For this you can use alter command.

ALTER TABLE MyTable DROP CONSTRAINT PK_ID

If you have not specified primary key name while creating your table, you can check its name in key section also. Please see the image. You can find your here.

Hope it helps you. Use it in your code and enjoy.

Share this

About the Author

Big
Rahul Jha (Big)
Founder, Developer dotnet-concept.com
Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment