<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: SQL SERVER - Explanation about Usage of Unique Index and Unique Constraint</title>
	<atom:link href="http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<pubDate>Wed, 07 Jan 2009 16:56:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: safia</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-44371</link>
		<dc:creator>safia</dc:creator>
		<pubDate>Mon, 24 Nov 2008 16:08:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-44371</guid>
		<description>Nice topic and discussion. Thanks to all. I have a question. I have few join tables in database such as 

user_role
user_role_ID  PK
user_id           FK
role_id            FK

where a combination of user_id and role_id is unique. Is it a good idea to build a composite unique index on those kind of join tables or it is not necessary.

Thanks.</description>
		<content:encoded><![CDATA[<p>Nice topic and discussion. Thanks to all. I have a question. I have few join tables in database such as </p>
<p>user_role<br />
user_role_ID  PK<br />
user_id           FK<br />
role_id            FK</p>
<p>where a combination of user_id and role_id is unique. Is it a good idea to build a composite unique index on those kind of join tables or it is not necessary.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Tobey</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-44290</link>
		<dc:creator>Tom Tobey</dc:creator>
		<pubDate>Wed, 19 Nov 2008 21:48:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-44290</guid>
		<description>I'm running into this exact problem. Thanks for the discussion. We have thousands of suppliers and not all of them use DUNS number, but when they do we keep track of it in our vendor database at Lincoln Financial Group. We have a different internalsupplierid which is unique and is the primary key. I can handle unique checking on DUNS in the application (ugh, if I have to), but it sure would be nice if on the database SQL server made it easier to add a "no duplicates unless null" index option.</description>
		<content:encoded><![CDATA[<p>I&#8217;m running into this exact problem. Thanks for the discussion. We have thousands of suppliers and not all of them use DUNS number, but when they do we keep track of it in our vendor database at Lincoln Financial Group. We have a different internalsupplierid which is unique and is the primary key. I can handle unique checking on DUNS in the application (ugh, if I have to), but it sure would be nice if on the database SQL server made it easier to add a &#8220;no duplicates unless null&#8221; index option.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christoph Ingenhaag</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42903</link>
		<dc:creator>Christoph Ingenhaag</dc:creator>
		<pubDate>Wed, 17 Sep 2008 17:41:12 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42903</guid>
		<description>I would prefer an indexed view to enforce uniqueness on nullable columns (not in SQL Server 2008):


create view xy
with schemabinding
as
select
    uniquevalue
from dbo.table
where
    uniquevalue is not NULL

create unique clustered index cui on dbo.table(uniquevalue)

IMHO the only reasonable solution

Regards
Christop Ingenhaag</description>
		<content:encoded><![CDATA[<p>I would prefer an indexed view to enforce uniqueness on nullable columns (not in SQL Server 2008):</p>
<p>create view xy<br />
with schemabinding<br />
as<br />
select<br />
    uniquevalue<br />
from dbo.table<br />
where<br />
    uniquevalue is not NULL</p>
<p>create unique clustered index cui on dbo.table(uniquevalue)</p>
<p>IMHO the only reasonable solution</p>
<p>Regards<br />
Christop Ingenhaag</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yaakov Ellis</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42638</link>
		<dc:creator>Yaakov Ellis</dc:creator>
		<pubDate>Wed, 10 Sep 2008 08:43:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42638</guid>
		<description>Nagaraj - this however has the same problem as originally mentioned by Bengan: "&lt;em&gt;The problem of course is to find a set of deterministic data that is guaranteed to be unique for each row and at the same time guaranteed not to be present in the original column.&lt;/em&gt;".  

I think that the solution to this would be to use GUIDs (Type: UniqueIdentifier) as the datatype for the Primary Key row. This would ensure that no PK value would ever equal the unique column value. You would still have to put logic into your application to determine whether or not a real value or GUID-in-place-of-null is being used.</description>
		<content:encoded><![CDATA[<p>Nagaraj - this however has the same problem as originally mentioned by Bengan: &#8220;<em>The problem of course is to find a set of deterministic data that is guaranteed to be unique for each row and at the same time guaranteed not to be present in the original column.</em>&#8220;.  </p>
<p>I think that the solution to this would be to use GUIDs (Type: UniqueIdentifier) as the datatype for the Primary Key row. This would ensure that no PK value would ever equal the unique column value. You would still have to put logic into your application to determine whether or not a real value or GUID-in-place-of-null is being used.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nagaraj</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42616</link>
		<dc:creator>Nagaraj</dc:creator>
		<pubDate>Wed, 10 Sep 2008 02:11:32 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42616</guid>
		<description>I find Bengan solution to be perfect. 

1. create a computed column.
2. Pass the 'unique column'  value when it is not null
3. When it is null copy the Primary key's value into the computed column.
4. Set the unique constraint on computed column</description>
		<content:encoded><![CDATA[<p>I find Bengan solution to be perfect. </p>
<p>1. create a computed column.<br />
2. Pass the &#8216;unique column&#8217;  value when it is not null<br />
3. When it is null copy the Primary key&#8217;s value into the computed column.<br />
4. Set the unique constraint on computed column</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: HeatherK</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42609</link>
		<dc:creator>HeatherK</dc:creator>
		<pubDate>Tue, 09 Sep 2008 21:19:34 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42609</guid>
		<description>SQL Server 2008 supports filtered indexes (WHERE clause for indexes).  Filtered indexes allow you to enforce unique constraints on nullable columns.</description>
		<content:encoded><![CDATA[<p>SQL Server 2008 supports filtered indexes (WHERE clause for indexes).  Filtered indexes allow you to enforce unique constraints on nullable columns.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yaakov Ellis</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42586</link>
		<dc:creator>Yaakov Ellis</dc:creator>
		<pubDate>Tue, 09 Sep 2008 08:54:39 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42586</guid>
		<description>Sorry Imran, but I still don't see how your suggested solution enables the original poster to do what they want: have a unique constraint on one column that allows multiple null values, but ensures that all non-null values are unique.</description>
		<content:encoded><![CDATA[<p>Sorry Imran, but I still don&#8217;t see how your suggested solution enables the original poster to do what they want: have a unique constraint on one column that allows multiple null values, but ensures that all non-null values are unique.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pinaldave</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42575</link>
		<dc:creator>pinaldave</dc:creator>
		<pubDate>Tue, 09 Sep 2008 02:02:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42575</guid>
		<description>@Yaakov Ellis,  @Bengan and @Imran Mohammed,

I wish all of blog reader read this particular thread. This is very interesting and in depth discussion of the subject.  I really like the way all of you are participating to help community.

@Imran,

I really like your additional explanation and politeness towards other experts.

Regards,
Pinal Dave ( http://www.SQLAuthority.com )</description>
		<content:encoded><![CDATA[<p>@Yaakov Ellis,  @Bengan and @Imran Mohammed,</p>
<p>I wish all of blog reader read this particular thread. This is very interesting and in depth discussion of the subject.  I really like the way all of you are participating to help community.</p>
<p>@Imran,</p>
<p>I really like your additional explanation and politeness towards other experts.</p>
<p>Regards,<br />
Pinal Dave ( <a href="http://www.SQLAuthority.com" rel="nofollow">http://www.SQLAuthority.com</a> )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42569</link>
		<dc:creator>Imran Mohammed</dc:creator>
		<pubDate>Tue, 09 Sep 2008 00:46:49 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42569</guid>
		<description>Hello all,

My first statement says... "I am sure there must be another way of doing this, I would do something like this,"

The question was: There is a Unique Key and this field “alow null”, but when i am going to create a Unique Key the SQLSERVER saw that there were values duplicated and the values are “nulls”.

What I understood from this question was, the column we are talking about itself is unique except the fact that it has more than one null values. Because of this we cannot create a unique constraint on that column and I believe there is no way to create a unique constraint on a column which has more than one null unless you make a composite key, to do this I suggested two ways,

1. Combine this with any other unique column. Here when I say these, I am talking for only null records, not for non null records, that wasn;t even discussed in the question. The only concern of that person was how to solve null values and still make this column unique. no where he said that he want to make this column who unique.... his concern was only for null records.

2. I suggested that he create an identity column, in case if he dont have a uniwue column. now create a composite unique constraint on previous column and newly created identity column, again the concern here was only for null values. 

I understand your concern, you are asking if I make a unique constraint, on cola and colb, colb is identity, then I can have duplicate records in cola, but the combination of cola and colb will still be unique. You are correct, but again like I said, I answered this question thinking that the problem is only with null records and column by default have only unique non null records.

I am always ready to correct myself. Thanks for bringing up this fact.

Thanks,
Imran.</description>
		<content:encoded><![CDATA[<p>Hello all,</p>
<p>My first statement says&#8230; &#8220;I am sure there must be another way of doing this, I would do something like this,&#8221;</p>
<p>The question was: There is a Unique Key and this field “alow null”, but when i am going to create a Unique Key the SQLSERVER saw that there were values duplicated and the values are “nulls”.</p>
<p>What I understood from this question was, the column we are talking about itself is unique except the fact that it has more than one null values. Because of this we cannot create a unique constraint on that column and I believe there is no way to create a unique constraint on a column which has more than one null unless you make a composite key, to do this I suggested two ways,</p>
<p>1. Combine this with any other unique column. Here when I say these, I am talking for only null records, not for non null records, that wasn;t even discussed in the question. The only concern of that person was how to solve null values and still make this column unique. no where he said that he want to make this column who unique&#8230;. his concern was only for null records.</p>
<p>2. I suggested that he create an identity column, in case if he dont have a uniwue column. now create a composite unique constraint on previous column and newly created identity column, again the concern here was only for null values. </p>
<p>I understand your concern, you are asking if I make a unique constraint, on cola and colb, colb is identity, then I can have duplicate records in cola, but the combination of cola and colb will still be unique. You are correct, but again like I said, I answered this question thinking that the problem is only with null records and column by default have only unique non null records.</p>
<p>I am always ready to correct myself. Thanks for bringing up this fact.</p>
<p>Thanks,<br />
Imran.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bengan</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42540</link>
		<dc:creator>Bengan</dc:creator>
		<pubDate>Mon, 08 Sep 2008 11:02:00 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42540</guid>
		<description>Just like mr. Ellis, I fail to see how any of the two proposed solutions actually solve the original problem.

If you truly, desperately must have a column of unique non-null values but still allow many null values, one cheesy way would be to add a computed column, defined to contain the same non-null data, and some other, unique, data when the original column is null. E.g. like this:

ALTER TABLE SomeTable
 ADD DummyColumn AS CASE
   WHEN UniqueColumn IS NULL THEN SomeReallyUniqueColumnOrCombinationsThereofThatAreGuaranteedNeverToOccurInTheOtherUniqueColumn
   ELSE UniqueColumn
END PERSISTED

The problem of course is to find a set of deterministic data that is guaranteed to be unique for each row and at the same time guaranteed not to be present in the original column.</description>
		<content:encoded><![CDATA[<p>Just like mr. Ellis, I fail to see how any of the two proposed solutions actually solve the original problem.</p>
<p>If you truly, desperately must have a column of unique non-null values but still allow many null values, one cheesy way would be to add a computed column, defined to contain the same non-null data, and some other, unique, data when the original column is null. E.g. like this:</p>
<p>ALTER TABLE SomeTable<br />
 ADD DummyColumn AS CASE<br />
   WHEN UniqueColumn IS NULL THEN SomeReallyUniqueColumnOrCombinationsThereofThatAreGuaranteedNeverToOccurInTheOtherUniqueColumn<br />
   ELSE UniqueColumn<br />
END PERSISTED</p>
<p>The problem of course is to find a set of deterministic data that is guaranteed to be unique for each row and at the same time guaranteed not to be present in the original column.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yaakov Ellis</title>
		<link>http://blog.sqlauthority.com/2008/09/07/sql-server-explanation-about-usage-of-unique-index-and-unique-constraint/#comment-42522</link>
		<dc:creator>Yaakov Ellis</dc:creator>
		<pubDate>Mon, 08 Sep 2008 05:28:18 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1039#comment-42522</guid>
		<description>I don't see how this answers the original users' question. He wants to have ColA where the DB will enforce a constraint on the column so that it can have unlimited null values, but all non-null values will be unique. The suggested solution is to join ColA with ColB in a composite unique key, where ColB itself is a unique column. 

However, I don't think that this will solve the problem. True, it will allow multiple nulls in ColA (since any combo of null + ColB will be unique). But it will also allow multiple non-unique, non-null values in ColA (since ColB is a unique column itself, all of the rows for ColA could equal the same value while still observing the unique constraint). Am I missing something here?</description>
		<content:encoded><![CDATA[<p>I don&#8217;t see how this answers the original users&#8217; question. He wants to have ColA where the DB will enforce a constraint on the column so that it can have unlimited null values, but all non-null values will be unique. The suggested solution is to join ColA with ColB in a composite unique key, where ColB itself is a unique column. </p>
<p>However, I don&#8217;t think that this will solve the problem. True, it will allow multiple nulls in ColA (since any combo of null + ColB will be unique). But it will also allow multiple non-unique, non-null values in ColA (since ColB is a unique column itself, all of the rows for ColA could equal the same value while still observing the unique constraint). Am I missing something here?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
