Creating Primary Key in a SQL Server table

In this blog we shall learn how to create a Primary Key on a table using T-SQL.

A Primary Key on a table can be either created at the time of creating the table or after the table is created by altering the table and adding the Primary Key Constraint.  This blog covers the first method of creating the table and Primary Key at the same time. The important change in the create table script is to include a section inside the table schema definition by mentioning the keyword CONTRAINT followed by the Primary Key Name followed by the Key words PRIMARY KEY.   

CREATE TABLE EmployeeTable ( EmployeeID INT , FName VARCHAR (25)
, LName VARCHAR (25), Gender VARCHAR (1),
( EmployeeID ASC ) ) ON [PRIMARY]  GO

Some points to remember about Primary Keys

  • A table can consist of one and only one Primary Key.
  • By default a clustered Primary Key is created.
  • A Primary Key on a table can be based on one or more columns of that table.
  • A Primary Key ensure no duplicate values can exist in that column(s).
  • A Primary Key on one column can be used as a parent key on another table and helps in ensuring referential integrity between those tables.
  • A Primary Key is required when creating a Transactional Publication in Replication

Do you like this site? Like our FB page @\LearnSQLWithBru so that, you know when there is a new blog post.

–Bru Medishetty

One thought on “Creating Primary Key in a SQL Server table

  1. Pingback: Dropping a Column from a table using T-SQL – SQL Server

Leave a Reply