Introduction
A Primary Key is one of the essential components of a relational database theory. Primary keys enforce entity integrity by uniquely identifying entity instances. In this article, we will learn how to use the SQL Server PRIMARY KEY constraint to create a Primary Key for a table.
SQL Server Primary Key
The key points about Primary Key are as follows.
- The PRIMARY KEY constraint uniquely identifies each record in a table.
- Primary keys must contain UNIQUE values and cannot have NULL values.
- A table can have only ONE primary key, and in the table, this primary key can consist of single or multiple columns (fields).
Note
We can create a Primary Key using SQL Server MANAGEMENT STUDIO or Transact-SQL (T-SQL). When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index. The primary key column cannot allow NULL values. Here, I will briefly explain what a clustered and nonclustered index is, as it is connected to many primary key concepts.
Clustered Index in SQL Server
A clustered index defines the order in which data is physically stored in a table.
--The following query retrieves the indexes created on the student table.
EXECUTE sp_helpindex student
The output of the above query is.

In the output, you can see only one index. This index was automatically created because of the primary key constraint on the "id" column.
Nonclustered Index in SQL Server
A nonclustered index doesn't sort the physical data inside the table. A nonclustered index is stored in one place, and table data is stored in another.
--CREATE A EMP TABLE
CREATE TABLE Emp (
EmpID INT
,EmpFname VARCHAR(50)
,EmpLname VARCHAR(50)
,DeptID INT
,IsActive BIT
)
--The below script creates a non-clustered index on the "EmpFname" column of the Emp table.
CREATE NONCLUSTERED INDEX IX_tblEmp_Name
ON Emp(EmpFname ASC)
You can view the Nonclustered index either using Transact-SQL Query or using Object Explorer:
exec sp_helpindex 'Emp'
In the output of the T-SQL Query, you can easily see index_name,index_description, and index_keys,

Nonclustered indexes using Object Explorer

Now, let's understand the usage of the Primary Key using an example.
Step 1. Applying Primary Key on a Single Column.
The following SQL creates a PRIMARY KEY on the "ID" column when the "Student" table is created:
create table Student
(
ID int NOT NULL identity(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
)
You can see Output on the design Page, how the Primary Key exactly looks.

We can see in column properties that in identity specification if we want to set identity, the specification is "No." It gives a Warning, as mentioned below, and removing the Primary key from the design section will also provide the same warning. There is no modification to the ID column till its Primary Key.









Sathish KumarPosted Jan 22, 2021, 6:08 AM
Good Explanation
Raju PaladiyaPosted Mar 5, 2020, 2:31 AM
Nicely explained.
Ravi PatelPosted Mar 4, 2020, 11:24 PM
Nice articles thanks, in Note section While we create "Primary Key", it will automatically create a Clustered and Non-Clustered Index. this statement little bit confusing. i think primary key create only cluster index let me correct if i am wrong.