How to Add a Primary Key in SQL Server

Galaxy Glossary

How do I add a primary key to an existing SQL Server table?

ADD PRIMARY KEY enforces row uniqueness in a SQL Server table by creating a clustered or non-clustered index on one or more columns.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Why add a primary key after table creation?

Late modeling, data import, or legacy systems may leave tables without a primary key. ALTER TABLE … ADD CONSTRAINT lets you enforce uniqueness without recreating the table.

What is the basic ALTER TABLE syntax?

The command attaches a PRIMARY KEY constraint to one or multiple columns. Optionally specify CLUSTERED | NONCLUSTERED.

How do I add a single-column primary key?

Use one numeric, unique, NOT NULL column—typically an identity column like id.

ALTER TABLE Customers
ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (id);

Can I add a composite primary key?

Yes—combine columns that together guarantee uniqueness.

ALTER TABLE OrderItems
ADD CONSTRAINT PK_OrderItems PRIMARY KEY (
order_id, product_id
);

Example: Fix missing keys in an ecommerce schema

The following statements enforce primary keys on previously imported CSV data.

-- Customers
ALTER TABLE Customers
ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (id);

-- Orders
ALTER TABLE Orders
ADD CONSTRAINT PK_Orders PRIMARY KEY (id);

-- Products
ALTER TABLE Products
ADD CONSTRAINT PK_Products PRIMARY KEY (id);

How do I choose CLUSTERED vs NONCLUSTERED?

CLUSTERED reorders data pages by the key—great for range scans on id. NONCLUSTERED leaves the table order intact, useful when a table already has a clustered index.

What happens to existing duplicates?

SQL Server refuses to add the constraint if duplicate or NULL values exist. Clean or deduplicate data first:

WITH dups AS (
SELECT id, COUNT(*) AS c FROM Customers GROUP BY id HAVING COUNT(*)>1
)
DELETE c
FROM Customers c
JOIN dups d ON d.id = c.id;

Best practices for adding primary keys

1. Use integer surrogate keys for performance.
2. Name constraints with the PK_TableName pattern.
3. Create keys in maintenance windows when tables are large.
4. Combine the operation with foreign key creation for referential integrity.

Why How to Add a Primary Key in SQL Server is important

How to Add a Primary Key in SQL Server Example Usage


-- Add a primary key to Orders after bulk load
ALTER TABLE Orders
ADD CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED (id);

-- Verify
EXEC sp_helpindex 'Orders';

How to Add a Primary Key in SQL Server Syntax


ALTER TABLE table_name
ADD CONSTRAINT constraint_name
    PRIMARY KEY [CLUSTERED | NONCLUSTERED]
    (column1 [, column2 …]);

Example with ecommerce tables:

-- Single-column PK
ALTER TABLE Customers
ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (id);

-- Composite PK
ALTER TABLE OrderItems
ADD CONSTRAINT PK_OrderItems PRIMARY KEY (order_id, product_id);

Common Mistakes

Frequently Asked Questions (FAQs)

Does adding a primary key lock the table?

Yes, SQL Server acquires schema modification locks. On large tables, schedule downtime or use ONLINE = ON in Enterprise Edition.

Can I drop and recreate a primary key?

Yes. Use ALTER TABLE … DROP CONSTRAINT PK_name, then re-add it. Be aware that foreign keys referencing the PK must be dropped first.

Is an identity column required?

No. Any NOT NULL unique column or column combination can serve as the primary key; identity just simplifies value generation.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.