---
description: This tutorial covers SQL Primary Key definitions, rules, How to add a primary key to an existing table in SQL, interesting facts, and more with detailed examples.
title: SQL PRIMARY KEY: How to Create &#038; Add to Existing Table
image: https://www.guru99.com/images/sql-server-primary-key.png
---

 

[Skip to content](#main) 

## What is Primary Key in SQL?

 **Primary Key**  is a field or a combination of fields that identify a record uniquely. The Primary key is a column or set of columns that are unique. In other words, every value is unique for Primary Key.

### Rules for Primary Key

* Each table can have only one SQL Primary Key.
* All the values are unique and Primary key SQL value can uniquely identify each row.
* The system will not allow inserting a row with SQL Server Primary Key which already exists in the table.
* Primary Key cannot be NULL.

Below diagram summarizes all the above point for the SQL Primary Key.

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR1.png)

How Primary Key SQL Works

## How to Create Primary Key in SQL Server

We can Create a Primary Key in **2 ways:**

1\. [SQL Server Management Studio](https://www.guru99.com/sql-server-management-studio.html)

2\. T-SQL: Create Primary while creating a New Table

### SQL Server Management Studio

 **Step 1)** Right Click on the Table name. Click on **Design.**

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR2.png)

**Step 2)** Right-click on the Column name. Click on ‘**Set Primary Key’**

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR3.png)

**Result:** Course\_Id is now a **Primary Key.**

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR4.png)

### T-SQL: Create a Primary key while creating a New Table.

Below is the syntax to create table with Primary Key from T-SQL

**Syntax:**

CREATE TABLE <Table_Name>
(
Column1 datatype,
Column2 datatype,CONSTRAINT <Name> PRIMARY KEY (Column name)
.
);

Let’s create a Table with One Column as a SQL Primary Key.

**Query:**

CREATE TABLE COURSE_TSQL_PK 
(Course_ID Int not Null,
 Course_name Varchar(20)
 CONSTRAINT PK PRIMARY KEY (Course_ID)	
)

**Step 1)** Run the Query by clicking on **‘Execute.’**

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR5.png)

**Result:** Course\_Id is now a **Primary Key.** 

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR6.png)

**Example:** Let’s see if it allows entering Multiple Records with Same Course ID.

**Step 1)** Insert 4 rows with **different Course\_ID** 

Insert into COURSE_TSQL_PK values (1,'SQL');
Insert into COURSE_TSQL_PK values (2,'Python');
Insert into COURSE_TSQL_PK values (3,'SQL');
Insert into COURSE_TSQL_PK values (4,'C');

**Step 2)** Verify all Data Inserted successfully by running the Select query.

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR7.png)

**Note**: We can insert duplicate values in the Non-Primary key.

**Step 3)** Now let’s try Inserting New records with an existing Course\_ID which is Primary Key.

Insert into COURSE_TSQL_PK values (4,'JAVA');

**Result:** System does not allow inserting new value as 4 is there Course\_ID column which is a Primary Key.

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR8.png)

### How to Add Primary Key to Existing Table in SQL?

Now we will see how to add Primary Key to existing table in SQL:

You can use the ALTER statement to create a primary key. However, the primary key can only be created on columns that are defined as NOT NULL. You cannot create a primary key on a column that allows NULLs. If you need to do, you have to drop and recreate the table.

**Here is the syntax:**

ALTER TABLE tableName
ADD CONSTRAINT constraintName PRIMARY KEY (column_1, column_2, ... column_n);

**For example:**

ALTER TABLE students
ADD CONSTRAINT students_pk PRIMARY KEY (admission);

[](https://www.guru99.com/images/1/030819%5F0835%5FSQLServerPR9.png)

We have added a primary key constraint to an already existing table. The constraint has been added on the admission column and assigned the name students\_pk.

## Interesting Facts!

* The Primary key can be a combination of multiple columns. This combination is known as the **Composite primary key**.
* The Primary key can have a maximum of 16 columns.

## Summary

* Each Table can have only one Primary Key
* All the values are unique which can **uniquely identify each row.**

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/sql-server-primary-key.png","url":"https://www.guru99.com/images/sql-server-primary-key.png","width":"468","height":"330","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/sql-server-primary-key.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/sql-server","name":"SQL Server"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/sql-server-primary-key.html","name":"SQL PRIMARY KEY: How to Create &#038; Add to Existing Table"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/sql-server-primary-key.html#webpage","url":"https://www.guru99.com/sql-server-primary-key.html","name":"SQL PRIMARY KEY: How to Create &#038; Add to Existing Table","dateModified":"2024-06-28T15:48:35+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/sql-server-primary-key.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/sql-server-primary-key.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown","description":"I'm Fiona brown, a Full Stack Developer with over a decade of experience, sharing practical guides on robust and scalable application development.","url":"https://www.guru99.com/author/fiona","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/fiona-brown-author.png","url":"https://www.guru99.com/images/fiona-brown-author.png","caption":"Fiona Brown","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"image":{"@id":"https://www.guru99.com/images/sql-server-primary-key.png"},"headline":"SQL PRIMARY KEY: How to Create &#038; Add to Existing Table","description":"This tutorial covers SQL Primary Key definitions, rules, How to add a primary key to an existing table in SQL, interesting facts, and more with detailed examples.","keywords":"sqlserver, sql","@type":"Article","author":{"@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown"},"dateModified":"2024-06-28T15:48:35+05:30","copyrightYear":"2024","name":"SQL PRIMARY KEY: How to Create &#038; Add to Existing Table","articleSection":"SQL Server","subjectOf":[{"@type":"HowTo","name":"How to Create Primary Key in SQL Server","description":"We can Create a Primary Key in 2 ways. Using SQL Server Management Studio and T-SQL: Create Primary while creating a New Table.","step":[{"@type":"HowToStep","name":"Step 1) Click on Design","text":"Create Primary Key Using SQL Server Management Studio. In first step Right Click on the Table name. Click on Design.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/1/030819_0835_SQLServerPR2.png"},"url":"https://www.guru99.com/sql-server-primary-key.html#step1"},{"@type":"HowToStep","name":"Step 2)  Set Primary Key","text":"Now Right-click on the Column name. Click on Set Primary Key and in Result Course_Id is now a Primary Key.","image":{"@type":"ImageObject","url":"https://cdn.guru99.com/images/1/030819_0835_SQLServerPR3.png"},"url":"https://www.guru99.com/sql-server-primary-key.html#step2"}]}],"@id":"https://www.guru99.com/sql-server-primary-key.html#schema-24058","isPartOf":{"@id":"https://www.guru99.com/sql-server-primary-key.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/sql-server-primary-key.html#webpage"}}]}
```
