Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 July SQL Server Interview: What is the difference between Unique Constraint and Unique Index?

SQL Server Interview: What is the difference between Unique Constraint and Unique Index?

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

I am sharing this interview question for Database Developer who has around 1 or 2 years of experience.

Many times, I have asked the difference between Unique Constraint and Unique Index but most of the time people answers were like “No difference – Both are same”. This is a wrong answer.

Which one is the better:

When you are creating a column using UNIQUE constraint or UNIQUE Index, in both the cases SQL Server by default creates a unique non-clustered index.
There is no any performance difference between these two, so you can use any one of this.
But still, there are few minor differences between Unique Constraint and Unique Index.

Disable:

Unique Constraint: You cannot disable the Unique Constraint, you will get below error:
Msg 4916, Level 16, State 0, Line 1
Could not enable or disable the constraint. See previous errors.

Unique Index: You can disable it

1
ALTER INDEX Index_name ON Table_name DISABLE

Errors: The error messages for both violations are also different

Unique Constraint violation error:

1
2
3
Msg 2627, Level 14, State 1, Line 1
Violation of UNIQUE KEY constraint 'uk_tb'. Cannot insert duplicate key in object 'dbo.mytab'. The duplicate key value is (1).
The statement has been terminated.

Unique Index violation error:

1
2
3
Msg 2601, Level 14, State 1, Line 1
Cannot insert duplicate key row in object 'dbo.col1' with unique index 'id_tb'. The duplicate key value is (1).
The statement has been terminated.

Filters:

Unique Constraint: You cannot add filter in Unique Constraint.
Unique Index: You can add filter in Unique Index:

1
2
CREATE UNIQUE NONCLUSTERED INDEX idx_name
ON dbo.table_name(column_name) WHERE column_name ='M'

Jul 11, 2017Anvesh Patel
PostgreSQL: ERROR - canceling statement due to statement timeoutPostgreSQL: Different options to check If Table Exists in Database
Comments: 2
  1. Suresh
    July 12, 2017 at 5:59 am

    Hi Anvesh,

    I have concern about difference between unique constraint and unique index.
    When will create unique constraint ,The nonclustered index would be created automatically based on unique constraint’ column.

    Please correct me if i did mistake.

    Thanks
    T.Suresh

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      July 12, 2017 at 5:59 pm

      Thank you Suresh – For this correction. Yes – You are right, in both cases SQL Server adds a Non-Clustered Index on a column. I also modified this same in the article.

      ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

July 11, 2017 2 Comments SQL Server, SQL Server InterviewAnvesh Patel, database, database research and development, dbrnd, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Interview, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL, UNIQUE CONSTRAINT, Unique Index
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....