Wednesday, June 9, 2010

What's the difference between a primary key and a unique key constraints?

Both Primary Key and Unique Key enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default.

Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL.

Primary key:
  1. Primary key used to uniqly identify each row in Table.
  2. Primary key does not allow duplicate values and Null values.
  3. Primary key is default Clustered Indexes
  4. One table can have only one Primary key.

 Unique Key:

  1. Unique Key is used to uniqly identify each row in Table.
  2. Unique Key does not allow duplicate values but allowes only one Null value.
  3. Unique Key is default Non- Clustered Indexe
  4. One table can have more than one Unique Keys.

No comments:

Post a Comment