---
title: "Primary key"  
description: "In this blog I will explain you that how to work with primary key in sql server. I will show some examples in whic you see different mechanism to crea"  
author: "Anonymous User"  
published: 2011-08-01  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/214/primary-key  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# Primary key

[Primary key](https://www.mindstick.com/blog/474/remove-primary-key-from-access-table) in SqlServer

\

**In this blog I will explain some examples** that how to work with [primary keys](https://www.mindstick.com/forum/156008/how-do-you-set-two-primary-keys-in-access) in [sql server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server). Here in following examples I will show you that how to create primary key, how to create primary key using alter statement and how to drop primary key using alter statement.

--This is a demonstration of primary key in Sql server.

--Firstly we needs to create a table in which we can apply this demonstration.

--Creating a primary key on column label.

createtable **Employee**

(

**EmpId** varchar(5)notnull[constraint](https://www.mindstick.com/articles/434/constraint-in-sql-server) **pk1** primarykey,

**EmpRefNo** varchar(10)notnull,

**EmpName** varchar(50)notnull,

**EmpUId** varchar(15)notnull,

**EmpPanNo** varchar(20)null

)

--Creating primary key on table label.

createtable **Employee**

(

**EmpId** varchar(5)notnull,

**EmpRefNo** varchar(10)notnull,

**EmpName** varchar(50)notnull,

**EmpUId** varchar(15)notnull,

**EmpPanNo** varchar(20)null,

constraint **pk1** primarykey(EmpId)

)

--Creating primary key on [multiple columns](https://www.mindstick.com/forum/157767/how-to-apply-group-on-multiple-columns-in-sql).

--When we create primary key on more than one column then

--This key is also [known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) composite key.

createtable **Employee**

(

**EmpId** varchar(5)notnull,

**EmpRefNo** varchar(10)notnull,

**EmpName** varchar(50)notnull,

**EmpUId** varchar(15)notnull,

**EmpPanNo** varchar(20)null,

constraint **pk1** primarykey(EmpId,EmpUId)

)

--Creating primary key using alter staement.

--When we create primary key using alter statement

--Then we have to make sure that column have "[not null](https://www.mindstick.com/interview/1933/what-is-not-null-constraint)"

--[attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) and if it don't have "not null"

--attributes then we needs to assign "not null"

--[attribute](https://www.mindstick.com/blog/221/attributes-reflection) first using alter statement then we

--create primary key on that.

--Statements to create Employee table.

createtable **Employee**

(

**EmpId** varchar(5)notnull,

**EmpRefNo** varchar(10)notnull,

**EmpName** varchar(50)notnull,

**EmpUId** varchar(15)notnull,

**EmpPanNo** varchar(20)null,

)

--Statements to create primary key using alter statement.

altertable **Employee**

addconstraint **pk1** primarykey(EmpId)

--Statements to drop primary key using alter statement.

altertable **Employee**

dropconstraint **pk1**

**Thanks.**

---

Original Source: https://www.mindstick.com/blog/214/primary-key

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
