What is PRIMARY KEY Constraint in SQL Server?
Introduction:
In this article i will explain what is PRIMARY KEY Constraint in SQL Server.
Description:
In previous articles i explained what is Constraint, how many Constraints available in SQL Server, and what is NOT NULL, CHECK, and UNIQUE Constraint in SQL Server. Now i will explain what is PRIMARY KEY Constraint in SQL Server.
PRIMARY KEY Constraint:
PRIMARY KEY constraints identify the column or set of columns that have values that uniquely identify a row in a table. No two rows in a table can have the same primary key value. You cannot enter NULL for any column in a primary key.
Note: A column or combination of columns that qualify as a primary key value is referred to as a candidate key.
We can create Primary Key constraint in two ways:
- Column Level
- Table Level
Column Level:
Now i'm creating Employee table where EmpNo column has Primary key constraint.
Create table Employee ( EmpNo int Primary key, -- Column Level EmpName varchar(50) )
Table Level:
Now i'm creating Employee table where EmpNo column has Primary key constraint.
Create table Employee ( EmpNo int, EmpName varchar(50), constraint PK_EmpNo Primary Key(EmpNo) -- Table Level )
Difference between UNIQUE and PRIMARY KEY:
- UNIQUE allows only one null value. But where as Primary Key will not allow a nul value.
- Table can have only one primary key and can have any number of UNIQUE constraints.
-
CreatedOct 24, 2013
-
UpdatedNov 03, 2020
-
Views1,898