Wednesday, January 20, 2010

Difference Between UNIQUE and PRIMARY KEY columns


What is the difference between UNIQUE and PRIMARY KEY columns?

With the primary key column we can identify rows of a table in a unique manner. This is very important when it comes to database programming. On the other hand, a unique key column,
  • forbids having identical values on the field. For example, If you have column for storing names you can not have two same names. Data should be unique.
  • You can have more than one unique key columns
  • is allowed to accept NULL values. This is the differentiating factor between primary key and unique key. Primary key does not allow NULL values.
See the below example.




ID
Name
City
1
Rose
Chicago
2
Mike
San Antonio
3
Null
Chicago
4
Lian
Chicago


Here ID column is primary key and "Name" column is a unique key.

0 comments:

Post a Comment