---
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: Create &#038; Add to an Existing Table
image: https://www.guru99.com/images/sql-server-primary-key-1.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

A primary key in SQL Server is a column or set of columns whose values uniquely identify every row in a table. Each table holds one primary key, and its columns must never be NULL.

* 🔑 **What is a primary key:** A primary key is one or more columns whose values are unique and identify each row in a table.
* 🚫 **Core rules:** A table allows only one primary key, every value must be unique, and no key column may contain NULL.
* 🖱️ **Two creation methods:** SQL Server Management Studio Table Designer and the T-SQL CREATE TABLE statement both define a primary key.
* 🧱 **CONSTRAINT clause:** Inside CREATE TABLE, the CONSTRAINT … PRIMARY KEY clause names the key and lists its column or columns.
* ➕ **Add to an existing table:** ALTER TABLE … ADD CONSTRAINT adds a primary key to the NOT NULL columns of an existing table.
* 🧩 **Composite key:** A primary key can combine up to 16 columns, forming a composite key that is unique across the whole set.

[ Read More ](javascript:void%280%29;) 

![SQL Server PRIMARY KEY: How to Create and Add to an Existing Table](https://www.guru99.com/images/sql-server-primary-key-1.png)

## What is Primary Key in SQL?

A primary key in [SQL Server](https://www.guru99.com/ms-sql-server-tutorial.html) is a field, or a combination of fields, that identifies a record uniquely. It is a column or a set of columns in which every value is unique. In other words, no two rows can share the same primary key value.

### Rules for Primary Key

* Each table can have only one primary key.
* All the values are unique, and the primary key value can uniquely identify each row.
* The system will not allow inserting a row whose primary key value already exists in the table.
* A primary key cannot be NULL.

The diagram below summarizes all the points above about the SQL primary key.

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

## How to Create Primary Key in SQL Server

You can create a primary key in two ways:

* [SQL Server Management Studio](https://www.guru99.com/sql-server-management-studio.html)
* T-SQL: create the primary key while creating a new table

Let us study each method in detail.

### SQL Server Management Studio

Step 1) Right-click on the table name and click **Design**.

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

Step 2) Right-click on the column name and click **Set Primary Key**.

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

Result: Course\_Id is now a primary key, shown by a key icon beside the column.

[](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 a table](https://www.guru99.com/sql-server-table-create-alter-drop.html) with a primary key from T-SQL.

**Syntax:**

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

Here, CONSTRAINT names the key, PRIMARY KEY marks the column that must stay unique, and every column also needs a [data type](https://www.guru99.com/sql-server-datatype.html).

Let us 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 **Execute**.

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

Result: Course\_Id is now a primary key on the COURSE\_TSQL\_PK table.

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

Example: Let us see whether it allows entering multiple records with the same Course ID.

Step 1) Insert four rows with different Course\_ID values.

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 that all data was inserted successfully by running the SELECT query.

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

Note: You can insert duplicate values in a non-primary-key column.

Step 3) Now let us try inserting a new record with an existing Course\_ID, which is the primary key.

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

Result: The system does not allow inserting the new value, because 4 already exists in the Course\_ID column, which is a primary key.

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

### RELATED ARTICLES

* [Oracle vs SQL Server – Difference Between Them ](https://www.guru99.com/oracle-vs-sql-server.html "Oracle vs SQL Server – Difference Between Them")
* [SSAS Tutorial: What is SSAS Cube, Architecture & Types ](https://www.guru99.com/ssas-tutorial.html "SSAS Tutorial: What is SSAS Cube, Architecture & Types")
* [CASE Statement & Nested Case in SQL Server: T-SQL Example ](https://www.guru99.com/sql-server-case.html "CASE Statement & Nested Case in SQL Server: T-SQL Example")
* [SQL Server Tutorial PDF for Beginners (Free Download) ](https://www.guru99.com/sql-server-tutorial-pdf.html "SQL Server Tutorial PDF for Beginners (Free Download)")

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

Now we will see how to add a primary key to an existing table in SQL.

You can use the ALTER statement to create a primary key. However, a primary key can be created only on columns that are defined as NOT NULL. You cannot create a primary key on a column that allows NULLs; if a column currently allows NULLs, you must first alter it to NOT NULL, or 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!

* A primary key can be a combination of multiple columns. This combination is known as the composite primary key.
* A primary key can have a maximum of 16 columns.

## FAQs

🔑 What is the difference between a primary key and a unique key?

Both enforce uniqueness. A table allows only one primary key, and its columns cannot be NULL. A table may hold several unique keys, and a unique column can accept one NULL value. Primary keys usually define the main row identifier.

🗂️ Does a primary key build an index automatically?

Yes. SQL Server automatically builds a unique index to enforce the primary key. By default this index is clustered, which physically orders the rows. You can specify NONCLUSTERED instead when another column already carries the clustered index.

🧩 What is a composite primary key, and what is its column limit?

A composite primary key combines two or more columns so their values are unique together, even when each column repeats individually. SQL Server allows a primary key to span up to 16 columns and a total key length of 900 bytes.

🔗 How is a primary key different from a foreign key?

A primary key uniquely identifies each row within its own table and never allows NULLs. A [foreign key](https://www.guru99.com/sql-server-foreign-key.html) stores values that must match a primary or unique key in another table, creating a relationship between the two tables.

🗑️ How do you delete an existing primary key constraint?

Run ALTER TABLE table\_name DROP CONSTRAINT constraint\_name. You must know the constraint name, which you can find in sys.key\_constraints. Dropping the primary key also removes its supporting unique index and any foreign key that references it.

🆔 Should you use an IDENTITY column as the primary key?

An IDENTITY column that auto-generates numbers is a popular surrogate primary key because it stays narrow, stable, and never NULL. Choose a natural key instead only when an existing column, such as a product code, is already guaranteed unique.

🤖 Can GitHub Copilot generate primary key constraints and CREATE TABLE scripts?

Yes. [GitHub Copilot](https://github.com/features/copilot) can write CREATE TABLE statements and PRIMARY KEY constraints from natural-language prompts and suggest suitable key columns. Always review the generated keys, data types, and nullability, then test the script before running it in production.

🧠 How does AI help choose and design primary keys?

AI and machine learning tools analyze sample data and query patterns to recommend which column or columns should be the primary key, flag candidate keys that contain duplicates or NULLs, and suggest surrogate keys. The developer confirms each recommendation before applying it.

#### 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-1.png","url":"https://www.guru99.com/images/sql-server-primary-key-1.png","width":"700","height":"250","caption":"SQL Server PRIMARY KEY","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: Create &#038; Add to an 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: Create &#038; Add to an Existing Table","dateModified":"2026-07-25T11:18:37+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/sql-server-primary-key-1.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"}},{"articleSection":"SQL Server","headline":"SQL PRIMARY KEY: Create &#038; Add to an 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","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/fiona","name":"Fiona Brown"},"dateModified":"2026-07-25T11:18:37+05:30","image":{"@id":"https://www.guru99.com/images/sql-server-primary-key-1.png"},"copyrightYear":"2026","name":"SQL PRIMARY KEY: Create &#038; Add to an Existing Table","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"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is the difference between a primary key and a unique key?","acceptedAnswer":{"@type":"Answer","text":"Both enforce uniqueness. A table allows only one primary key, and its columns cannot be NULL. A table may hold several unique keys, and a unique column can accept one NULL value. Primary keys usually define the main row identifier."}},{"@type":"Question","name":"Does a primary key build an index automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. SQL Server automatically builds a unique index to enforce the primary key. By default this index is clustered, which physically orders the rows. You can specify NONCLUSTERED instead when another column already carries the clustered index."}},{"@type":"Question","name":"What is a composite primary key, and what is its column limit?","acceptedAnswer":{"@type":"Answer","text":"A composite primary key combines two or more columns so their values are unique together, even when each column repeats individually. SQL Server allows a primary key to span up to 16 columns and a total key length of 900 bytes."}},{"@type":"Question","name":"How is a primary key different from a foreign key?","acceptedAnswer":{"@type":"Answer","text":"A primary key uniquely identifies each row within its own table and never allows NULLs. A foreign key stores values that must match a primary or unique key in another table, creating a relationship between the two tables."}},{"@type":"Question","name":"How do you delete an existing primary key constraint?","acceptedAnswer":{"@type":"Answer","text":"Run ALTER TABLE table_name DROP CONSTRAINT constraint_name. You must know the constraint name, which you can find in sys.key_constraints. Dropping the primary key also removes its supporting unique index and any foreign key that references it."}},{"@type":"Question","name":"Should you use an IDENTITY column as the primary key?","acceptedAnswer":{"@type":"Answer","text":"An IDENTITY column that auto-generates numbers is a popular surrogate primary key because it stays narrow, stable, and never NULL. Choose a natural key instead only when an existing column, such as a product code, is already guaranteed unique."}},{"@type":"Question","name":"Can GitHub Copilot generate primary key constraints and CREATE TABLE scripts?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot can write CREATE TABLE statements and PRIMARY KEY constraints from natural-language prompts and suggest suitable key columns. Always review the generated keys, data types, and nullability, then test the script before running it in production."}},{"@type":"Question","name":"How does AI help choose and design primary keys?","acceptedAnswer":{"@type":"Answer","text":"AI and machine learning tools analyze sample data and query patterns to recommend which column or columns should be the primary key, flag candidate keys that contain duplicates or NULLs, and suggest surrogate keys. The developer confirms each recommendation before applying it."}}]}],"@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"}}]}
```
