<?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>UNIQUE CONSTRAINT &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/unique-constraint/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 28 Feb 2018 02:55:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Server: Allow Multiple NULL Values in UNIQUE Constraint</title>
		<link>https://www.dbrnd.com/2018/03/sql-server-allow-multiple-null-values-in-unique-constraint/</link>
					<comments>https://www.dbrnd.com/2018/03/sql-server-allow-multiple-null-values-in-unique-constraint/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 04 Mar 2018 16:16:42 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[UNIQUE CONSTRAINT]]></category>
		<category><![CDATA[UNIQUE NON NONCLUSTERED INDEX]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7186</guid>

					<description><![CDATA[<div><img width="679" height="382" src="https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-679x382.jpg" class="attachment-post-image size-post-image wp-post-image" alt="UNIQUE Constraint" decoding="async" fetchpriority="high" srcset="https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-679x382.jpg 679w, https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-1030x579.jpg 1030w, https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-850x478.jpg 850w, https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint.jpg 1280w" sizes="(max-width: 679px) 100vw, 679px" /></div>In this post, I am sharing a demonstration on how to allow multiple NULLs for UNIQUE Constraint in SQL Server. As per the ANSI, UNIQUE constraint allows multiple NULLs. But in the SQL Server, it allows only one NULL value. With the UNIQUE constraint, you cannot insert multiple NULLs. But you can create UNIQUE NONCLUSTERED [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="679" height="382" src="https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-679x382.jpg" class="attachment-post-image size-post-image wp-post-image" alt="UNIQUE Constraint" decoding="async" srcset="https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-679x382.jpg 679w, https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-1030x579.jpg 1030w, https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint-850x478.jpg 850w, https://www.dbrnd.com/wp-content/uploads/2018/02/UNIQUE-Constraint.jpg 1280w" sizes="(max-width: 679px) 100vw, 679px" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&amp;linkname=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&amp;linkname=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&amp;linkname=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&amp;linkname=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&amp;linkname=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&amp;linkname=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F03%2Fsql-server-allow-multiple-null-values-in-unique-constraint%2F&#038;title=SQL%20Server%3A%20Allow%20Multiple%20NULL%20Values%20in%20UNIQUE%20Constraint" data-a2a-url="https://www.dbrnd.com/2018/03/sql-server-allow-multiple-null-values-in-unique-constraint/" data-a2a-title="SQL Server: Allow Multiple NULL Values in UNIQUE Constraint"></a></p><p>In this post, I am sharing a demonstration on how to allow multiple NULLs for UNIQUE Constraint in SQL Server. </p>
<blockquote><p>As per the ANSI, UNIQUE constraint allows multiple NULLs. But in the SQL Server, it allows only one NULL value.<br />
With the UNIQUE constraint, you cannot insert multiple NULLs. </p></blockquote>
<p>But you can create UNIQUE NONCLUSTERED INDEX with the NOT NULL filter and can insert multiple NULLs.</p>
<p>Please check the below demonstration:</p>
<p><strong>Create a sample table with UNIQUE Constraint:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestUnique (ID INT UNIQUE)
GO</pre> </p>
<p><strong>Try to insert multiple NULLs:</strong></p><pre class="crayon-plain-tag">INSERT INTO tbl_TestUnique 
VALUES (1),(NULL),(NULL),(2),(NULL)
GO


Msg 2627, Level 14, State 1, Line 4
Violation of UNIQUE KEY constraint 'UQ__tbl_Test__3214EC26DBD9CD71'. Cannot insert duplicate key in object 'dbo.tbl_TestUnique'. The duplicate key value is (<NULL>).</pre><p> </p>
<p><strong>Now, try to insert with single NULL:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_TestUnique 
VALUES (1),(2),(NULL)
GO</pre> </p>
<p><strong>Drop the table:</strong></p>
<pre class="crayon-plain-tag">DROP TABLE tbl_TestUnique
GO</pre> </p>
<p><strong>Create the same table with UNIQUE NONCLUSTERED INDEX:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestUnique (ID INT)
GO

CREATE UNIQUE NONCLUSTERED INDEX idx_tbl_TestUnique_ID
ON tbl_TestUnique(ID)
WHERE ID IS NOT NULL
GO</pre> </p>
<p><strong>Now, INSERT Multiple Nulls:</strong><br />
As we defined UNIQUE on ID NOT NULL, so we can insert multiple NULLs.</p>
<pre class="crayon-plain-tag">INSERT INTO tbl_TestUnique 
VALUES (1),(2),(NULL),(NULL)
GO

INSERT INTO tbl_TestUnique 
VALUES (NULL),(NULL),(NULL)
GO</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/03/sql-server-allow-multiple-null-values-in-unique-constraint/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Allow single NULL for UNIQUE Constraint Column</title>
		<link>https://www.dbrnd.com/2017/12/postgresql-allow-single-null-for-unique-constraint-column/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 17 Dec 2017 16:16:49 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[IS NULL]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[UNIQUE]]></category>
		<category><![CDATA[UNIQUE CONSTRAINT]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6820</guid>

					<description><![CDATA[In this post, I am sharing a solution on how to allow only single NULL record in the UNIQUE Constraint Column of PostgreSQL. PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values Once you define the UNIQUE constraint, you can insert N number of NULL values for that column which is the principal rule of [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&amp;linkname=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&amp;linkname=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&amp;linkname=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&amp;linkname=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&amp;linkname=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&amp;linkname=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F12%2Fpostgresql-allow-single-null-for-unique-constraint-column%2F&#038;title=PostgreSQL%3A%20Allow%20single%20NULL%20for%20UNIQUE%20Constraint%20Column" data-a2a-url="https://www.dbrnd.com/2017/12/postgresql-allow-single-null-for-unique-constraint-column/" data-a2a-title="PostgreSQL: Allow single NULL for UNIQUE Constraint Column"></a></p><p>In this post, I am sharing a solution on how to allow only single NULL record in the UNIQUE Constraint Column of PostgreSQL. </p>
<blockquote class="wp-embedded-content" data-secret="xUrjuAI5aI"><p><a href="https://www.dbrnd.com/2016/01/postgresql-composite-unique-constraint-does-not-consider-null-values/">PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/postgresql-composite-unique-constraint-does-not-consider-null-values/embed/#?secret=xUrjuAI5aI" data-secret="xUrjuAI5aI" width="600" height="338" title="&#8220;PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>Once you define the UNIQUE constraint, you can insert N number of NULL values for that column which is the principal rule of UNIQUE Constraint. </p>
<p>What if I need an only single NULL record for UNIQUE Column, we should check the below demonstration. </p>
<p><strong>Create a sample table with UNIQUE Constraint:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_testunique (ID INTEGER UNIQUE);</pre> </p>
<p><strong>Insert sample multiple NULLs:</strong><br />
You can insert all NULLs.</p>
<pre class="crayon-plain-tag">INSERT INTO tbl_testunique 
VALUES (1),(NULL),(NULL),(NULL);</pre> </p>
<p><strong>Now, Truncate the table:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_testunique 
VALUES (1),(NULL),(NULL),(NULL);</pre> </p>
<p><strong>Create a UNIQUE INDEX with IS NULL:</strong></p>
<pre class="crayon-plain-tag">CREATE UNIQUE INDEX idx_id ON tbl_testunique ((ID IS NULL)) 
WHERE ID IS NULL;</pre> </p>
<p><strong>Now, Try to execute same INSERT statement:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_testunique 
VALUES (1),(NULL),(NULL),(NULL);</pre> </p>
<p><strong>Will get ERROR in the result:</strong></p>
<pre class="crayon-plain-tag">********** Error **********
ERROR: duplicate key value violates unique constraint "idx_id"
SQL state: 23505
Detail: Key ((id IS NULL))=(t) already exists.</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Interview: What is the difference between Unique Constraint and Unique Index?</title>
		<link>https://www.dbrnd.com/2017/07/sql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index/</link>
					<comments>https://www.dbrnd.com/2017/07/sql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 11 Jul 2017 16:16:14 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[UNIQUE CONSTRAINT]]></category>
		<category><![CDATA[Unique Index]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5650</guid>

					<description><![CDATA[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 &#8220;No difference &#8211; Both are same&#8221;. This is a wrong answer. Which one is the [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&amp;linkname=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F07%2Fsql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index%2F&#038;title=SQL%20Server%20Interview%3A%20What%20is%20the%20difference%20between%20Unique%20Constraint%20and%20Unique%20Index%3F" data-a2a-url="https://www.dbrnd.com/2017/07/sql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index/" data-a2a-title="SQL Server Interview: What is the difference between Unique Constraint and Unique Index?"></a></p><p>I am sharing this interview question for Database Developer who has around 1 or 2 years of experience. </p>
<p>Many times, I have asked the difference between Unique Constraint and Unique Index but most of the time people answers were like &#8220;No difference &#8211; Both are same&#8221;. <strong>This is a wrong answer</strong>.</p>
<p><strong>Which one is the better:</strong></p>
<blockquote><p>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.<br />
There is no any performance difference between these two, so you can use any one of this.<br />
But still, there are few minor differences between Unique Constraint and Unique Index. </p></blockquote>
<p><strong>Disable:</strong></p>
<blockquote><p><strong>Unique Constraint:</strong> You cannot disable the Unique Constraint, you will get below error:<br />
Msg 4916, Level 16, State 0, Line 1<br />
Could not enable or disable the constraint. See previous errors.</p>
<p><strong>Unique Index:</strong> You can disable it</p>
<pre class="crayon-plain-tag">ALTER INDEX Index_name ON Table_name DISABLE</pre>
 </p></blockquote>
<p><strong>Errors:</strong> The error messages for both violations are also different</p>
<blockquote><p><strong>Unique Constraint violation error:</strong></p>
<pre class="crayon-plain-tag">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.</pre> </p>
<p><strong>Unique Index violation error:</strong></p>
<pre class="crayon-plain-tag">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.</pre>
</p></blockquote>
<p><strong>Filters:</strong></p>
<blockquote><p><strong>Unique Constraint:</strong> You cannot add filter in Unique Constraint.<br />
<strong>Unique Index:</strong> You can add filter in Unique Index:</p>
<pre class="crayon-plain-tag">CREATE UNIQUE NONCLUSTERED INDEX idx_name 
ON dbo.table_name(column_name) WHERE column_name ='M'</pre>
 </p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/07/sql-server-interview-what-is-the-difference-between-unique-constraint-and-unique-index/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Example of Partial UNIQUE Composite Index</title>
		<link>https://www.dbrnd.com/2016/08/postgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 22 Aug 2016 17:56:07 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Partial Index Constraint]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[UNIQUE CONSTRAINT]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3549</guid>

					<description><![CDATA[The Data validation is always a major concern of any Database System. We all know about the UNIQUE constraint which prevents the duplicate records. One of the Previous article, I have already shared one small note about the UNIQUE Index of PostgreSQL. PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values In this post, I [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&amp;linkname=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records%2F&#038;title=PostgreSQL%3A%20Example%20of%20Partial%20UNIQUE%20Composite%20Index" data-a2a-url="https://www.dbrnd.com/2016/08/postgresql-example-of-partial-unique-composite-index-to-avoid-duplicate-records/" data-a2a-title="PostgreSQL: Example of Partial UNIQUE Composite Index"></a></p><p>The Data validation is always a major concern of any Database System.<br />
We all know about the UNIQUE constraint which prevents the duplicate records. </p>
<p>One of the Previous article, I have already shared one small note about the UNIQUE Index of PostgreSQL. </p>
<blockquote class="wp-embedded-content" data-secret="mBxVzjd76w"><p><a href="https://www.dbrnd.com/2016/01/postgresql-composite-unique-constraint-does-not-consider-null-values/">PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/postgresql-composite-unique-constraint-does-not-consider-null-values/embed/#?secret=mBxVzjd76w" data-secret="mBxVzjd76w" width="600" height="338" title="&#8220;PostgreSQL: Composite UNIQUE Constraint does not consider NULL Values&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>In this post, I am sharing a similar post for Partial Unique Index of PostgreSQL. </p>
<p>For example, I want to stop duplicate Student Grages for particular Student Division only.</p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Students
(
	StudID INT 
	,StudName CHARACTER VARYING 
	,StudDivision CHARACTER VARYING
	,StudGrade CHAR(1)
);</pre> </p>
<p><strong>Create a Partial UNIQUE Index:</strong></p>
<pre class="crayon-plain-tag">CREATE UNIQUE INDEX tbl_Students_StudGrade ON tbl_Students (COALESCE(StudDivision,''),COALESCE(StudGrade,'')) 
WHERE StudDivision IN ('First','Second','Third');</pre> </p>
<p><strong>Insert few sample records:</strong><br />
As we have creted Partial index so we can insert only unique combination of StudDivision (First,Second,Third) and StudGrade.</p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Students
VALUES 
(1,'Anvesh','First','A')
,(2,'Jeeny','Second','B')
,(3,'Roy','Third','B')
,(4,'Mukesh','First','C')
,(5,'Martin','Second','C')
,(6,'Kavita','Third','A');</pre> </p>
<p><strong>Try to insert few Invalid records:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Students
VALUES 
(7,'Rajesh','First','A')
,(8,'Nile','Second','B');</pre> </p>
<p><strong>Try to insert few records which are not validated by Partition Index:</strong><br />
We can insert duplicate records StudGrade + StudDivision for other StudDivisions except Partial StudDivision.</p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Students
VALUES 
(7,'Dimpi','Fourth','C')
,(8,'Bony','Fourth','C');</pre> </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
