<?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; books online</title>
	<atom:link href="http://sqlfool.com/tag/books-online/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>Wed, 02 Nov 2011 20:39: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>
		<item>
		<title>BOL 2008 Update Released</title>
		<link>http://sqlfool.com/2009/05/bol-2008update-released/</link>
		<comments>http://sqlfool.com/2009/05/bol-2008update-released/#comments</comments>
		<pubDate>Fri, 22 May 2009 21:39:22 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[2008]]></category>
		<category><![CDATA[BOL]]></category>
		<category><![CDATA[books online]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=975</guid>
		<description><![CDATA[If you haven't heard, Microsoft released an update to Books Online for SQL Server 2008 yesterday. You can find the download here: http://www.microsoft.com/downloads/details.aspx?FamilyID=765433f7-0983-4d7a-b628-0a98145bcb97&#038;displaylang=en]]></description>
			<content:encoded><![CDATA[<p>If you haven't heard, Microsoft released an update to Books Online for SQL Server 2008 yesterday.  You can find the download here:</p>
<p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=765433f7-0983-4d7a-b628-0a98145bcb97&#038;displaylang=en" target="_blank">http://www.microsoft.com/downloads/details.aspx?FamilyID=765433f7-0983-4d7a-b628-0a98145bcb97&#038;displaylang=en</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2009/05/bol-2008update-released/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

