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.

28 Responses

  1. 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.

  2. It is good try to collect more points

  3. Hi,

    Good explanation

  4. Very clear and helpful explanation! Thanks a lot

  5. good

  6. If possible,pls include a detailed explanation…

  7. 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)

  8. Very good. Many thanks

  9. Very good explanation difference between primary key and unique key

  10. its very good article..
    thks a lot

  11. thks a lot to Rubinaalexmary

    regards
    achiever.

  12. Simly Great….

  13. excellent, thanks a lot….

  14. ty so much

  15. Nice arctical ……

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

  17. Good explanation..
    txs
    nirmala

  18. good explanation

  19. Are govind chuudaraa

  20. jaheer gaadiki kuuda cheppu

  21. Rubinaalexmary thank you very much for your precision made on March 1, 2007

  22. Helped a lot..

  23. nice, useful for getting knowledge from the scratch.

  24. Please give an example.

  25. thanks, very good explanation

  26. Very Well Explanation.
    Thanks for your help

  27. Very nice explanation

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

Leave a Reply