<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Server with Mr. Denny &#187; Unique Index</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/sql-server/tag/unique-index/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/sql-server</link>
	<description></description>
	<lastBuildDate>Wed, 19 Jun 2013 19:39:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>SQL Server gets an ANSI compliant unique index &#8230; sort of</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/sql-server-gets-an-ansi-compliant-unique-index-sort-of/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/sql-server-gets-an-ansi-compliant-unique-index-sort-of/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 11:00:48 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Index]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[T/SQL]]></category>
		<category><![CDATA[Unique Index]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/?p=718</guid>
		<description><![CDATA[If you work with any of the other big database platforms you&#8217;ve probably noticed that SQL Server&#8217;s implementation of a unique index is &#8220;different&#8221; than the others.  Until now there hasn&#8217;t been a way to fix that without using a trigger.  Until now&#8230; But first, a little background information. When you create a unique index [...]]]></description>
				<content:encoded><![CDATA[<p>If you work with any of the other big database platforms you&#8217;ve probably noticed that SQL Server&#8217;s implementation of a unique index is &#8220;different&#8221; than the others.  Until now there hasn&#8217;t been a way to fix that without using a trigger.  Until now&#8230;<span id="more-718"></span></p>
<p>But first, a little background information.</p>
<p>When you create a unique index in SQL Server (assuming a single column index here), you can have a single row with each value, and a single null value.</p>
<p>When you create a unique index in Oracle, or any other database which uses the ANSI compliant unique index (again assuming a single column index), you can hand a single row with each value, and as many null values as you want.</p>
<p>The logic of allowing multiple NULL values (which I agree with) is that NULL isn&#8217;t a value, its unknown, so who&#8217;s to say that two unknown values aren&#8217;t different.</p>
<p>With SQL Server if you wanted to enforce this you&#8217;d have to write some logic into a trigger to enforce this.</p>
<p>With SQL Server 2008 and up, you no longer need to do this.  A standard unique index can now be used to give you an ANSI complaint unique index.  The trick, is to use a filtered index.</p>
<p>When you create a unique filtered index the uniqueness is only applied to the values which are stored in the index.  Values which aren&#8217;t stored in the index aren&#8217;t required to be unique.  So if we create a unique index on a column where the column is not null, we have an ANSI complaint unique index.</p>
<p>We can test this with a little sample code.</p>
<pre class="brush: sql; title: ; notranslate">create table test
(column1 int)
go
create unique index ux_test_c1 on dbo.test
(column1)
where column1 is not null
go
insert into test
select null
go
insert into test
select null
go
insert into test
select 1
go
insert into test
select 2
go
select *
from test</pre>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/sql-server-gets-an-ansi-compliant-unique-index-sort-of/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
