Home > Database, SQL Server > Difference between UNIQUE constraint and PRIMARY key

Difference between UNIQUE constraint and PRIMARY key

A UNIQUE constraint is similar to PRIMARY key, but you can have more than one UNIQUE constraint per table.

When you declare a UNIQUE constraint, SQL Server creates a UNIQUE index to speed up the process of searching for duplicates. In this case the index defaults to NONCLUSTERED index, because you can have only one CLUSTERED index per table.

* The number of UNIQUE constraints per table is limited by the number of indexes on the table i.e 249 NONCLUSTERED index and one possible CLUSTERED index.

Contrary to PRIMARY key UNIQUE constraints can accept NULL but just once. If the constraint is defined in a combination of fields, then every field can accept NULL and can have some values on them, as long as the combination values is unique.

Categories: Database, SQL Server
  1. Rubinaalexmary
    March 1, 2007 at 10:15 am

    The basic differences between Primary Key and Unique key are as follows.

    1) By default Primary Key will generate Clustured Index
    whereas Unique Key will Generate Non-Clustured Index.

    2) Primary Key is a combination of Unique and NOT NULL Constraints so it can’t
    have duplicate values or any NUll
    Whereas for Oracle UNIQUE Key can have any number of NULL whereas for SQL
    Server It can have only one NULL

    3) A table can have only one PK but It can have any number of UNIQUE Key.

    • Hyma
      February 1, 2012 at 6:09 pm

      thank you ,very helpful

    • MANISH PANDEY
      March 21, 2013 at 7:23 am

      I THINK IT IS RIGHT

    • pooja maheshwari
      July 25, 2013 at 5:17 pm

      thank you
      a comparison to not null key would also be helpful

  2. April 17, 2007 at 7:12 am

    It is good try to collect more points

  3. Sumana
    August 6, 2007 at 11:27 am

    Hi,

    Good explanation

  4. Truong Trung Trang
    August 31, 2007 at 12:57 am

    Very clear and helpful explanation! Thanks a lot

  5. jayashree
    December 19, 2007 at 6:56 am

    good

  6. Deepthi
    January 5, 2008 at 10:51 am

    If possible,pls include a detailed explanation…

  7. chaitanya
    February 12, 2008 at 7:03 am

    Primary Key does not allow null values where as unique constraint allow ‘single’ null value.

    A table can have only single Primary Key where as it can have multiple unique constraints ( max 16)

    • vits
      March 5, 2013 at 3:18 pm

      thanks a lot. i was looking for this info….

  8. patar
    February 22, 2008 at 1:36 am

    Very good. Many thanks

  9. Sunil Yadav
    February 29, 2008 at 11:42 pm

    Very good explanation difference between primary key and unique key

  10. Aravind
    May 13, 2008 at 6:59 am

    its very good article..
    thks a lot

  11. Aravind
    May 13, 2008 at 7:01 am

    thks a lot to Rubinaalexmary

    regards
    achiever.

  12. Kanhaiya
    July 2, 2008 at 2:13 pm

    Simly Great….

  13. perumal
    July 8, 2008 at 2:15 pm

    excellent, thanks a lot….

  14. AIDSTER
    July 17, 2008 at 5:29 am

    ty so much

  15. Sunil Bhati
    July 31, 2008 at 9:24 am

    Nice arctical ……

  16. August 20, 2008 at 5:39 am

    its good, but pls try to give the detailed description about the differences

  17. nirmala
    September 15, 2008 at 3:56 pm

    Good explanation..
    txs
    nirmala

  18. September 16, 2008 at 11:28 am

    good explanation

  19. September 16, 2008 at 11:28 am

    Are govind chuudaraa

  20. September 16, 2008 at 11:29 am

    jaheer gaadiki kuuda cheppu

  21. Prashant
    September 26, 2008 at 8:52 am

    Helped a lot..

  22. Ali
    October 29, 2008 at 5:12 am

    nice, useful for getting knowledge from the scratch.

  23. sourav
    February 9, 2009 at 9:00 am

    Please give an example.

  24. debashish
    April 13, 2009 at 7:14 am

    thanks, very good explanation

  25. Asim
    August 28, 2009 at 3:58 am

    Very Well Explanation.
    Thanks for your help

  26. Sivakumar
    October 6, 2009 at 4:42 am

    Very nice explanation

  27. Asghar
    October 18, 2009 at 5:37 pm

    Its very good and very clear explanation. Thank you so much

  28. raj dhakad
    January 2, 2010 at 11:46 am

    thnx
    nice explaination…..

  29. January 2, 2010 at 11:48 am

    thnx
    nic explaination…..

  30. neenu
    May 18, 2010 at 5:34 pm

    nice xplanation!!!

  31. swarnima
    July 11, 2010 at 1:35 pm

    good!! but….. need more explanation.

  32. Parthiv
    July 26, 2010 at 4:09 am

    Thanx a lot….

  33. krishna
    September 1, 2010 at 12:29 pm

    Unique key constrain nothing but when we apply unique key column on any particular table or column, that column does not accept duplicate value, it can accept Null values if we cross the condition on column it will an error message,

    but we can have more than one UNIQUE constraint per table.

    Primary key nothing but not null constraint and unique column when we apply primary key column on any particular table or column, that column can not accept null values and it can not accept duplicate values mean time it arranges the data in ascending order,
    but we can not have more than one primary key constraint per table

  34. sanket berde
    April 20, 2011 at 8:20 pm

    thank you….

  35. abhishek
    May 10, 2011 at 6:47 am

    thanks krishna..!

  36. Lokesh
    June 6, 2011 at 9:27 pm

    very Good Explanation helps a lot…
    thank u

  37. Vijitha Epa
    September 9, 2011 at 10:13 am

    A good explaination. not messy with so many explanations and examples. as the first level of explanation. Good and summarised. Thanks

  38. Kuldeep Bansal
    December 24, 2011 at 3:58 pm

    This is one of the best answer so far, I have read online. Just useful information. Very well presented. I had found another good collection of sql keys over internet.
    please check out this link…

    http://mindstick.com/Articles/d837d16c-d483-4948-ad18-41b405c011bd/?Important%20SQL%20Keys

  39. mallikarjuna
    December 26, 2011 at 6:52 am

    good explanation thaq.

  40. kalai
    March 17, 2012 at 10:59 am

    can any one help me to find out how to delete a duplicat values in a table

  41. maya
    August 15, 2012 at 6:00 am

    what is cluster index and non cluster index?

  42. Jay
    August 16, 2012 at 7:23 am

    Very well Explained…Thank You !

  43. Swathika
    August 18, 2012 at 6:44 am

    Hi Guys,
    Please let me know,which one is the faster while retrieving the data.

  44. rajath
    October 24, 2012 at 11:37 am

    thanks was helpfull

  45. kalyan
    November 1, 2012 at 6:59 am

    Thanks it was helpful

  46. Anil Kumar
    August 3, 2013 at 3:53 pm

    nice

  47. mohammed
    April 21, 2014 at 10:18 am

    thanks was helpfull

  48. shivani pandit
    December 7, 2014 at 4:16 am

    thanx its helpful

  1. No trackbacks yet.

Leave a reply to Kanhaiya Cancel reply