Unique index or unique key?

Viewed 30386

What is the diffrence between a unique index and a unique key?

7 Answers

Here are few key differences:

Purpose:

  • Unique Key: Ensures integrity of data at table level, so that no duplicates can be entered in the table. Is not used for query planning, does not contribute to query speed. (It's different purpose than Primary Key, primary key uniquely identifies each record for data operations such as update / delete etc. In complex tables, a unique key can be combinations of several columns and it will be inefficient to use unique key for identifying records for transactions. Hence primary key is quick way of identifying a particular record in the table, while unique key guarantees that no two records have same key attributes.)
  • Unique Index: Ensures uniqueness of data at index level, cannot guarantee uniqueness at the table level e.g. in case of filtered index. Is used for query planning and fetching data and thus speeds up queries depending on columns used / queried.

Filter Option:

  • Unique Key: Filter option is not available
  • Unique Index: Filter option is available

Storage Option:

  • Unique Key: Filegroup only
  • Unique Index: Filegroup or partition

Icon:

  • Unique Key: Icon is vertical key [ enter image description here ]
  • Unique Index: Icon is b-tree [ enter image description here ]

The functionalities are more or less same, it’s dependent on your use case.

Suppose you want to permit duplicate rows based on CUSTOMER_ID and TEAM_NAME.

In that case you can use both:

  • UNIQUE INDEX idx_customer_id_name (CUSTOMER_ID,TEAM_NAME)
  • UNIQUE KEY unique_key_customer_id_name (CUSTOMER_ID,TEAM_NAME)

But you should consider how often you fetch records based on CUSTOMER_ID AND TEAM_NAME. If it is more, then you should use unique index as it would help in faster retrieval of records otherwise you should go with unique key as it would prevent overheard of fetching based on index.

Related