<?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 Fool &#187; primary key</title>
	<atom:link href="http://sqlfool.com/tag/primary-key/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Adventures in SQL Tuning - a blog for the rest of us</description>
	<lastBuildDate>Fri, 16 Jul 2010 14:34:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Primary Key vs Unique Constraint</title>
		<link>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/</link>
		<comments>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 16:35:29 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[BOL]]></category>
		<category><![CDATA[books online]]></category>
		<category><![CDATA[constraints]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[primary key]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1068</guid>
		<description><![CDATA[Recently, I encountered a table that needed to have the definition of a clustered index altered. It just so happens that the clustered index and the primary key were one and the same, a pretty common occurrence. However, when we went to modify the index, it failed. The following entry in Books Online for CREATE [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I encountered a table that needed to have the definition of a clustered index altered.  It just so happens that the clustered index and the primary key were one and the same, a pretty common occurrence.  However, when we went to modify the index, it failed.  </p>
<p>The following entry in Books Online for <a href="http://msdn.microsoft.com/en-us/library/ms188783.aspx" target="_blank">CREATE INDEX</a> explains why:</p>
<blockquote><p>If the index enforces a PRIMARY KEY or UNIQUE constraint and the index definition is not altered in any way, the index is dropped and re-created preserving the existing constraint. However, if the index definition is altered the statement fails. To change the definition of a PRIMARY KEY or UNIQUE constraint, drop the constraint and add a constraint with the new definition.</p></blockquote>
<p>Let's test this, shall we?</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Create a table with a clustered primary key */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">myTable</span>
<span style="color: #808080;">&#40;</span>
      myID      <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>   Not Null
    , myDate    <span style="color: #0000FF;">SMALLDATETIME</span>       Not Null
    , myNumber  <span style="color: #0000FF;">INT</span>                 Not Null
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> CIX_myTable 
        <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #808080;">&#40;</span>myDate, myID<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Insert some data */</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> myTable
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2009-01-01'</span>, <span style="color: #000;">100</span> <span style="color: #0000FF;">UNION</span> All
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2009-02-01'</span>, <span style="color: #000;">200</span> <span style="color: #0000FF;">UNION</span> All
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'2009-01-05'</span>, <span style="color: #000;">300</span>;
&nbsp;
<span style="color: #008080;">/* Try to alter the index - FAIL */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">INDEX</span> CIX_myTable
    <span style="color: #0000FF;">ON</span> myTable<span style="color: #808080;">&#40;</span>myID, myDate, myNumber<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span>Drop_Existing <span style="color: #808080;">=</span> <span style="color: #0000FF;">ON</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Drop the clustered primary key */</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">myTable</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">CONSTRAINT</span> CIX_myTable;
&nbsp;
<span style="color: #008080;">/* Add a unique clustered index */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">UNIQUE</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">INDEX</span> CIX_myTable 
    <span style="color: #0000FF;">ON</span> myTable<span style="color: #808080;">&#40;</span>myDate, myID<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Add a unique constraint */</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> myTable
    <span style="color: #0000FF;">ADD</span> <span style="color: #0000FF;">CONSTRAINT</span> Unique_myTable
        <span style="color: #0000FF;">UNIQUE</span> <span style="color: #808080;">&#40;</span>myDate<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Try to alter the index - SUCCESS */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">INDEX</span> CIX_myTable
    <span style="color: #0000FF;">ON</span> myTable<span style="color: #808080;">&#40;</span>myID, myDate, myNumber<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span>Drop_Existing <span style="color: #808080;">=</span> <span style="color: #0000FF;">ON</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Add a primary key constraint */</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> myTable
    <span style="color: #0000FF;">ADD</span> <span style="color: #0000FF;">CONSTRAINT</span> PK_myTable
        <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">&#40;</span>myID, myDate<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Try to alter the index - SUCCESS */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">CLUSTERED</span> <span style="color: #0000FF;">INDEX</span> CIX_myTable
    <span style="color: #0000FF;">ON</span> myTable<span style="color: #808080;">&#40;</span>myID, myDate<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span>Drop_Existing <span style="color: #808080;">=</span> <span style="color: #0000FF;">ON</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Clean-Up */</span>
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> myTable;</pre></div></div>

<p>The only instance that actually fails is the PRIMARY KEY constraint.  The unique clustered index is able to be modified successfully, even when a unique constraint is applied to the table.  So either I'm misunderstanding BOL, or BOL is mistaken.  Either way, I'm then left with the following question: is there any reason to actually use a primary key when a unique index serves the same purpose and offers greater flexibility?  </p>
<p>Questions, comments, and explanations are welcome.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2009/06/primary-key-vs-unique-constraint/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
