How to add Primary Key Constraint in SQL Server
A primary key is used to uniquely identify each record in a table. A table can have only one primary key. A primary key can consist of one or more fields/columns. A primary key with more than one column is called as composite key.
A combination of NOT NULL and UNIQUE Constraints is also called primary key.
For example, in a student’s table, each and every student have a unique id/roll number. We cannot have the same id/roll number for two students in a table.
Primary Key on Create Table Using T-SQL
Primary Key can be created at the time of table creation using T-SQL. We can create Primary Key just by writing PRIMARY KEY keyword after writing column name, and it’s data type. In the below example ‘EmpID INT PRIMARY KEY NOT NULL’ is the first column where EmpID is the PRIMARY KEY column.
CREATE TABLE tblEmployee ( EmpID INT PRIMARY KEY NOT NULL, EmpFirstName NVARCHAR(20) NULL, EmpLastName NVARCHAR(20) NULL, EmpEmail NVARCHAR(50) NULL, EmpPhone NVARCHAR(15) NULL, EmpDeptID INT NULL, EmpSalary INT NULL, EmpJoiningDate DATETIME NULL ) GO
Primary Key on Alter Table Using T-SQL
ALTER TABLE tblEmployee
ADD PRIMARY KEY (EmpID)
A Primary Key Constraint can also be created on alter table.
Adding Primary Key When Creating Table Using GUI(Graphical User Interface)
- Expand Databases, then expand database(HR for this demo) where you want to create table, and right click on Tables, go to New and then click on Table as shown in below image.

- In the Text Editor window, fill the table with required fields.

- To make the EmpID column as Primary Key, right-click on the left side arrow of EmpID column and click on Set Primary Key.

- Click on save button or right click on dbo. Table_1 tab and click on Save Table_1.

- Fill the pop window with table name and click on OK button.

- A table with the Primary Key Constraint will be created. You can check it in the Object Explorer by expanding the database where you created the table, then expand the Tables node and expand Keys node, primary key will be there.

Recommended Readings
- Advanced SQL Queries For Practice With Solution
- SQL Queries For Practice With Solution
- SQL Interview Questions and Answers
- STORED PROCEDURE in SQL Server
- How To Join Tables Data in SQL Server
- How to use Transaction in SQL Stored Procedure
- Difference Between IN and NOT IN Operators in SQL
- How To Modify Date in SQL Using DATEADD
- How To Get Year From Date in SQL Server
- How To Get Month From Date in SQL Server
- How To Get Day From Date in SQL Server
- How To Use ROW_NUMBER Function in SQL
- Date and Time Functions in SQL Server
- How To Find Nth Highest Salary in SQL Server
- How to Backup Table Using SELECT INTO Statement
- How To Use HAVING Clause in SQL Server
- Aggregate Functions in SQL Server
- How To Group Data Using Group By in SQL Server
- How To Truncate Table in SQL Server
- How To Delete Data From Table in SQL Server
- How To Update Table Data in SQL Server
- How To Sort Data Using Order By Clause in SQL
- How To Select Distinct Records in SQL Server
- How to Filter Data From Table in SQL Server
- Round Off Values Using Ceiling and Floor in SQL
- How To Find Square Root Of A Number in SQL Server
- How To Select Data From Table in SQL Server
- How To Insert Data in SQL Server Table
- How To Add NOT NULL Constraint in SQL Server
- How To Add Check Constraint on SQL Server Table
- How To Add Default Constraint on SQL Server Table
- Unique Key Constraint in SQL Server
- How to add Foreign Key Constraint in SQL Server
- How To Add Identity To SQL Server Table Column
- How to add Primary Key Constraint in SQL Server
- How To Create Alter and Drop Table in SQL Server
- How To Create Alter and Drop Database in SQL