Difference between Primary Key and Unique Key

So many people asked me this question; In many interviews also I faced this question.Let’s we have a look into this.

  • Primary Key and Unique key (both) uniquely identifies a row.
  • Unique Key Allows 1 null column, where are Primary key will not allow null values.
  • As per the DBA’s Point of view,  Primary key creates a clustered index,where unique key creates non-clustered index.

I hope every one like this article.Please make some comments .

print

6 thoughts on “Difference between Primary Key and Unique Key

  1. Author’s gravatar

    hiiii,
    I liked this article very much bcoz i too faced the same question in different interviews.Could u plz include the practical use of primary key and unique key with an example .
    and also want to know in which situation unique key is using instead of primary key.

    Thanks and Regards
    Preethy kamath

    1. Author’s gravatar

      Hi Preethy, This is a wonderful question and also an enthusiastic one…I will explain here with a simple example.Consider a table Employee details with columns EmpID,Name and InsuranceID.Case scenario, Each Employee has an EmployeeID and InsuranceID(both are different).Here in this case EmpID and InsuranceID should be unique.EmpID can be used as a Primary key, where as InsuranceID can be used as UniqueKey.We are not using unique key instead pf Primary key.Each table should have a Primary Key.

      Note : We can use only one Primary Key(ie one clustered index) in a table.

      Thanks For this comment..Please keep on visiting the site…..

      Reply
  2. Author’s gravatar

    Hai Varun,
    Will u give me a clear reference in the coding standards have to use in SQL.( In Storeprocedures, queries, Views, triggers as well as Cursors also)

    Yours Failthfully,

    Dipu Joseph
    Programmer
    Iware Solutions, Infopark

    1. Author’s gravatar

      Hi Dipu,

      Thanks 4 visiting my site.I will post the coding Standards soon…..

      thanking u,
      Varun R

      Reply
  3. Author’s gravatar

    Dear varun,
    will you please describe the types of keys using in sql

    Thanks and Regards,
    C.H.Jithin

  4. Author’s gravatar

    hi varun,

    IS Unique key more than one null columns

Leave a Reply

Your email address will not be published. Required fields are marked *


8 − three =