Primary Key T-SQL

Add Constraint Primary Key Example

To create a primary key in a table, use the command alter table with add constraint.

Departments table

USE tempdb;
GO
CREATE TABLE dbo.departments(
id int NOT NULL,
name varchar(250) );
GO


Create Constraint Primary Key

USE tempdb;
GO
ALTER TABLE dbo.departments
ADD CONSTRAINT PK_DEP_ID PRIMARY KEY CLUSTERED (ID);
GO