Compare Primary key and Unique key / Comparison of Primary key and Unique key / Differentiate Primary and Unique key
Primary Key and Unique Key Comparison
Comparison Property
|
Primary Key
|
Unique Key
|
How to
create?
|
Using
the keyword ‘PRIMARY KEY’
Ex. (in SQL)
attribute name type(size) PRIMARY KEY
Emp_ID CHAR(10) PRIMARY KEY
|
Using
the keyword ‘UNIQUE’
Ex. (in SQL)
attribute name type(size) UNIQUE
Emp_ID CHAR(10) UNIQUE
|
Purpose
|
To
uniquely identify records
|
To
uniquely identify records
|
Null values
|
Cannot
have NULL values
|
Can
have at most one NULL value
|
Number of
keys
|
Can
have only one Primary key
|
Can
have many unique keys
|
Can we use
one or more attributes (columns) together to form the key?
(Composite
keys)
|
Yes.
One or more columns together can form Primary key
|
Yes.
One or more columns together can form Unique key.
|
Maximum
number of columns permitted in a composite key (in Oracle).
|
32
|
32
|
Can we Create,
Add, Drop, Enable and Disable?
|
Yes
|
Yes
|
Is it
compulsory to have these keys in a table?
|
No.
Optional
|
No.
Optional
|
Automatic creation
of index tables (in Oracle)
|
Primary
key declaration automatically creates index on the Primary key attribute.
|
Unique
key declaration automatically creates an index on the Unique attribute.
|