<?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: Primary Key vs Unique Constraint</title>
	<atom:link href="http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/</link>
	<description>Self-Professed SQL Scripting Junkie!</description>
	<lastBuildDate>Fri, 04 May 2012 08:30:44 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>By: ivanrdgz</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4982</link>
		<dc:creator>ivanrdgz</dc:creator>
		<pubDate>Sun, 18 Oct 2009 03:12:37 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4982</guid>
		<description>Michelle,

In order to replicate a table using transactional replication the table must have a primary key. The replication uses the primary key for tracking all changes on the published database and the subscriber. In fact, if you modify the primary key on the subscriber (I am talking about simple transactional replication) and afterward you modify the same record on the publisher the replication will fail, since it will not be able to find that record on the subscriber. In merge replication a rowguid column is added to each published table for that purpose.</description>
		<content:encoded><![CDATA[<p>Michelle,</p>
<p>In order to replicate a table using transactional replication the table must have a primary key. The replication uses the primary key for tracking all changes on the published database and the subscriber. In fact, if you modify the primary key on the subscriber (I am talking about simple transactional replication) and afterward you modify the same record on the publisher the replication will fail, since it will not be able to find that record on the subscriber. In merge replication a rowguid column is added to each published table for that purpose.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Machanic</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4571</link>
		<dc:creator>Adam Machanic</dc:creator>
		<pubDate>Mon, 22 Jun 2009 21:18:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4571</guid>
		<description>Clustered unique constraint on non-nullable columns will behave exactly the same as clustered PK with regard to the various components of the query engine.

XML indexes require PKs because under the covers a hidden table is created (called the &quot;nodes table&quot;) which has a 1:m relationship with the original table. The nodes table includes the PK from the original table so that if a query can perform best by doing a seek into the nodes table first, then finding related attributes in the base table, that can be done as efficiently as possible. The requirement for PK vs unique is probably just one of ease, so they don&#039;t have to figure out which unique constraint/index to use if there are multiple...</description>
		<content:encoded><![CDATA[<p>Clustered unique constraint on non-nullable columns will behave exactly the same as clustered PK with regard to the various components of the query engine.</p>
<p>XML indexes require PKs because under the covers a hidden table is created (called the &#8220;nodes table&#8221;) which has a 1:m relationship with the original table. The nodes table includes the PK from the original table so that if a query can perform best by doing a seek into the nodes table first, then finding related attributes in the base table, that can be done as efficiently as possible. The requirement for PK vs unique is probably just one of ease, so they don&#8217;t have to figure out which unique constraint/index to use if there are multiple&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michelle Ufford</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4570</link>
		<dc:creator>Michelle Ufford</dc:creator>
		<pubDate>Mon, 22 Jun 2009 15:59:19 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4570</guid>
		<description>I&#039;m just now getting a chance to catch up to comments...

@Jason - how does that affect transactional replication?  I&#039;m a replication novice.  :)

@Damon - I don&#039;t care about diagrams.  :)

@SQLDenis - good point about the nullability!

@Adam - why do XML indexes require PK&#039;s?  I&#039;m not familiar with XML indexes.

@Carolyn - I&#039;m not sure why the DB engine would treat a clustered unique index any different than a clustered primary key.  Could you provide an example or link to more information?

Thanks everyone for your comments and input!  :)</description>
		<content:encoded><![CDATA[<p>I&#8217;m just now getting a chance to catch up to comments&#8230;</p>
<p>@Jason &#8211; how does that affect transactional replication?  I&#8217;m a replication novice.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@Damon &#8211; I don&#8217;t care about diagrams.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@SQLDenis &#8211; good point about the nullability!</p>
<p>@Adam &#8211; why do XML indexes require PK&#8217;s?  I&#8217;m not familiar with XML indexes.</p>
<p>@Carolyn &#8211; I&#8217;m not sure why the DB engine would treat a clustered unique index any different than a clustered primary key.  Could you provide an example or link to more information?</p>
<p>Thanks everyone for your comments and input!  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Carolyn Richardson</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4569</link>
		<dc:creator>Carolyn Richardson</dc:creator>
		<pubDate>Mon, 22 Jun 2009 15:42:14 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4569</guid>
		<description>I believe that a unique index rather than a Clustered Primary Key will probably lead to bookmark lookups rather than Index seeks, and can radically increases the disk I/O. Where there is no primary key but a unique clustered index these should be altered to be a Clustered Primary Key.</description>
		<content:encoded><![CDATA[<p>I believe that a unique index rather than a Clustered Primary Key will probably lead to bookmark lookups rather than Index seeks, and can radically increases the disk I/O. Where there is no primary key but a unique clustered index these should be altered to be a Clustered Primary Key.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Cheung</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4567</link>
		<dc:creator>Peter Cheung</dc:creator>
		<pubDate>Mon, 22 Jun 2009 08:22:15 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4567</guid>
		<description>Change Tracking in SQL 2008 requires primary key.</description>
		<content:encoded><![CDATA[<p>Change Tracking in SQL 2008 requires primary key.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Machanic</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4562</link>
		<dc:creator>Adam Machanic</dc:creator>
		<pubDate>Sun, 21 Jun 2009 14:23:58 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4562</guid>
		<description>I thought of two more things that need &quot;actual&quot; PKs: XML indexes and FTS.</description>
		<content:encoded><![CDATA[<p>I thought of two more things that need &#8220;actual&#8221; PKs: XML indexes and FTS.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLDenis</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4551</link>
		<dc:creator>SQLDenis</dc:creator>
		<pubDate>Fri, 19 Jun 2009 18:07:03 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4551</guid>
		<description>&gt;&gt;is there any reason to actually use a primary key when a unique index serves the same purpose and offers greater flexibility?

A primary key does not allow nulls while a unique index allows 1 null, this might be problematic if you are trying to create a foreign key
Also by default a PK is clustered while a unique constraint is not

People just have to be aware of the differences...hey for all you know someone puts 2 unique constraints on the same column on sorted desc the other asc  (I have seen this!!!)

create table bla(id int)

alter table bla add  constraint  uq_con unique  (id asc) 
alter table bla add  constraint  uq_con2 unique  (id desc)</description>
		<content:encoded><![CDATA[<p>&gt;&gt;is there any reason to actually use a primary key when a unique index serves the same purpose and offers greater flexibility?</p>
<p>A primary key does not allow nulls while a unique index allows 1 null, this might be problematic if you are trying to create a foreign key<br />
Also by default a PK is clustered while a unique constraint is not</p>
<p>People just have to be aware of the differences&#8230;hey for all you know someone puts 2 unique constraints on the same column on sorted desc the other asc  (I have seen this!!!)</p>
<p>create table bla(id int)</p>
<p>alter table bla add  constraint  uq_con unique  (id asc)<br />
alter table bla add  constraint  uq_con2 unique  (id desc)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Damon Clark</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4550</link>
		<dc:creator>Damon Clark</dc:creator>
		<pubDate>Fri, 19 Jun 2009 17:15:52 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4550</guid>
		<description>Assuming you are not allowing nulls in those columns, there is not a lot of difference.  As Adam said, for diagramming, etc it won&#039;t be as clear because most tools (SSMS, SQL Diagrams, virtually any modeling tool) are designed to show PKs and FKs in a special manner.</description>
		<content:encoded><![CDATA[<p>Assuming you are not allowing nulls in those columns, there is not a lot of difference.  As Adam said, for diagramming, etc it won&#8217;t be as clear because most tools (SSMS, SQL Diagrams, virtually any modeling tool) are designed to show PKs and FKs in a special manner.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4549</link>
		<dc:creator>Jason</dc:creator>
		<pubDate>Fri, 19 Jun 2009 17:09:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4549</guid>
		<description>Transactional replication :)</description>
		<content:encoded><![CDATA[<p>Transactional replication <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Machanic</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/comment-page-1/#comment-4548</link>
		<dc:creator>Adam Machanic</dc:creator>
		<pubDate>Fri, 19 Jun 2009 16:54:03 +0000</pubDate>
		<guid isPermaLink="false">http://sqlfool.com/?p=1068#comment-4548</guid>
		<description>The PK shows up in the UI with nice little key icons, whereas the UNIQUE index doesn&#039;t. And that&#039;s about the only benefit I can think of. You can also do other things with UNIQUE indexes, e.g. create them non-clustered and use INCLUDE, if you want to use a non-clustered key and want to avoid bookmark lookups at the same time...</description>
		<content:encoded><![CDATA[<p>The PK shows up in the UI with nice little key icons, whereas the UNIQUE index doesn&#8217;t. And that&#8217;s about the only benefit I can think of. You can also do other things with UNIQUE indexes, e.g. create them non-clustered and use INCLUDE, if you want to use a non-clustered key and want to avoid bookmark lookups at the same time&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

