<?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>youdidwhatwithtsql.com &#187; database design</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/database-design/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Primary Keys do not always have to be &#8216;Id&#8217;!</title>
		<link>http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499</link>
		<comments>http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499#comments</comments>
		<pubDate>Sun, 03 Jan 2010 15:41:50 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Data]]></category>
		<category><![CDATA[DBA]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database design]]></category>
		<category><![CDATA[primary keys]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499</guid>
		<description><![CDATA[We&#8217;ve all more than likely spotted tables in databases with no primary keys. But does a primary key always have to be defined something like&#8230; ?View Code TSQLALTER TABLE MyTable ADD Id INTEGER NOT NULL IDENTITY&#40;1,1&#41; PRIMARY KEY CLUSTERED; I think we have ORM to blame for this widespread practice of using auto-incrementing integers as [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499">Primary Keys do not always have to be &#8216;Id&#8217;!</a></p>
]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all more than likely spotted tables in databases with no <a href="http://databases.about.com/cs/administration/g/primarykey.htm" target="_blank">primary keys</a>. But does a primary key always have to be defined something like&#8230;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p499code2'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4992"><td class="code" id="p499code2"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> MyTable <span style="color: #0000FF;">ADD</span> Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</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> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>;</pre></td></tr></table></div>

<p>I think we have <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORM</a> to blame for this widespread practice of using auto-incrementing integers as PK&#8217;s.</p>
<blockquote>
<p>In the ORM, these additional restrictions are placed on primary keys:</p>
<ul>
<li>Primary keys should be anonymous integer or numeric identifiers. </li>
</ul>
<p>source <a href="http://en.wikipedia.org/wiki/Unique_key">http://en.wikipedia.org/wiki/Unique_key</a></p>
<p>  <font style="background-color: #ffffff"></font></p></blockquote>
<p>I&#8217;m not saying this isn&#8217;t generally sensible. Probably most of the primary keys I have ever defined have been of this type. This is the best choice if the key will be used regularly in table joins. But this need not always be the case. Lets take an example. The table below contains UK Postcodes and location information.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postcode_Table_with_Id.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Postcode Table with Id" border="0" alt="Postcode Table with Id thumb Primary Keys do not always have to be Id!" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postcode_Table_with_Id_thumb.png" width="644" height="309" /></a> </p>
</p>
<p>What&#8217;s my beef with this? Well, lets assume that this table is solely used for for location lookups by postcode. What is the purpose of Id? Will it ever be used in any joins to other tables? Very unlikely in my opinion. Why not change the <strong>Postcode</strong> column to be the primary key?</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postcode_Table.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Postcode Table" border="0" alt="Postcode Table thumb Primary Keys do not always have to be Id!" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/01/Postcode_Table_thumb.png" width="644" height="236" /></a> </p>
<p>I saw this exact situation in a live system a few years ago. The <strong>Id </strong>column was redundant but the <strong>Postcode </strong>column wasn&#8217;t even indexed. Not good in a table with 2.4+ Million records. Careful selection of your Primary Keys not only result in fewer redundant columns but will also help with your indexing strategy.</p>
<p>Primary Keys, when selected like this, also protect us from screwing up our data. The <strong>Postcode </strong>table above contained over 300K duplicated postcodes. I&#8217;ve seen this cause developers to <a href="http://www.databasedev.co.uk/eliminate_duplicates.html" target="_blank">eliminate duplicates with SQL SELECT DISTINCT</a>. Lets fix our data, not our compose our queries around it causing performance hits!</p>
<p>For further reading see this article about <a href="http://www.pseudotheos.com/view_object.php?object_id=1273" target="_blank">the proper selection of a Primary Key</a>.</p>
<p><map name='google_ad_map_499_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/499?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_499_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=499&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fprimary-keys-do-not-always-have-to-be-id%2F499' title="Primary Keys do not always have to be Id!" alt=" Primary Keys do not always have to be Id!" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499">Primary Keys do not always have to be &#8216;Id&#8217;!</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/primary-keys-do-not-always-have-to-be-id/499/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

