0% found this document useful (0 votes)
190 views2 pages

Difference Between Primary Key and Unique Key

A primary key uniquely identifies each record in a database table and cannot contain NULL values or duplicate values. A table can only have one primary key. In contrast, a unique key also uniquely identifies records but can contain NULL values and a table can have multiple unique keys. The document provides an example table definition that specifies an integer primary key for the employee ID and a unique key for the mobile number, which is allowed to contain NULL values.

Uploaded by

Sangameshpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views2 pages

Difference Between Primary Key and Unique Key

A primary key uniquely identifies each record in a database table and cannot contain NULL values or duplicate values. A table can only have one primary key. In contrast, a unique key also uniquely identifies records but can contain NULL values and a table can have multiple unique keys. The document provides an example table definition that specifies an integer primary key for the employee ID and a unique key for the mobile number, which is allowed to contain NULL values.

Uploaded by

Sangameshpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Difference between Primary Key & Unique Key

Primary Key:
Can be only one in a table

It never allows null values

Primary Key is unique key identifier and can not be null and must
be unique.

Unique Key:
Can be more than one unique key in one table.

Unique key can have null values

It cant be candidate key

Unique key can be null and may not be unique.

CREATE TABLE Employee


(
EmpID int PRIMARY KEY, --define primary key
Name varchar (50) NOT NULL,
MobileNo int UNIQUE, --define unique key
Salary int NULL
)

----------------------------------------------------------------------------------------------------------Difference between Primary Key & Unique Key


A UNIQUE constraint and PRIMARY key both are similar and it provide
unique enforce uniqueness of the column on which they are defined.
Some are basic differences between Primary Key and Unique key are as
follows.
Primary key

Primary key cannot have a NULL value.

Each table can have only single primary key.

Primary key is implemented as indexes on the table. By default this


index is clustered index.
Primary key can be related with another table's as a Foreign Key.

We can generate ID automatically with the help of Auto Increment


field. Primary key supports Auto Increment value.

Unique Constraint

Unique Constraint may have a NULL value.

Each table can have more than one Unique Constraint.

Unique Constraint is also implemented as indexes on the table.


By default this index is Non-clustered index.
Unique Constraint cannot be related with another table's as a
Foreign Key.
Unique Constraint doesn't support Auto Increment value.

Define Primary and Unique Key


Create table Student
(
StuId int primary key, ---- Define Primary Key
StuName varchar(50) Not Null,
ContactNo int Unique --- Define Unique Key
)
Insert some data in table

Now you will see above fig. StuId can not have NULL value but
ContactNo can have NULL value.

You might also like