<?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>Dave Rowe's Blog &#187; keys</title>
	<atom:link href="http://blog.roweware.com/tag/keys/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.roweware.com</link>
	<description>Ramblings about things I think I know...</description>
	<lastBuildDate>Wed, 14 Jul 2010 17:45:52 +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>Surrogates</title>
		<link>http://blog.roweware.com/2009/12/23/surrogates/</link>
		<comments>http://blog.roweware.com/2009/12/23/surrogates/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 20:14:44 +0000</pubDate>
		<dc:creator>Dave Rowe</dc:creator>
				<category><![CDATA[Database Design]]></category>
		<category><![CDATA[keys]]></category>
		<category><![CDATA[linking table]]></category>
		<category><![CDATA[many to many]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://blog.roweware.com/?p=150</guid>
		<description><![CDATA[In my previous post, I used a key style that is open to debate and has been for many years amongst DB folks. The idea of every table having a surrogate key, regardless of the purpose of the table. This says, that for any record in the table I have a single column that acts [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I used a key style that is open to debate and has been for many years amongst DB folks.  The idea of every table having a surrogate key, regardless of the purpose of the table.  This says, that for any record in the table I have a single column that acts as the primary key.  Given a many-to-many relationship, using a surrogate key on the linking table allows me to describe the relationship in terms of objects and how they&#8217;re represented.  As shown in the below diagram &#8211; each user may have many user_role instances, which are tied to a single role instance.  This makes the lives of ORMs much easier since you can create objects for the linking table, which has a simple key to reference.</p>
<p style="text-align: center;"><a href="http://blog.roweware.com/wp-content/uploads/2009/12/Surrogate.png"><img class="size-full wp-image-152 aligncenter" title="Surrogate" src="http://blog.roweware.com/wp-content/uploads/2009/12/Surrogate.png" alt="" width="414" height="124" /></a></p>
<p>The ORM then has a User, UserRole, and Role object to use in accessing these tables and adding / removing relationships with ease, since it only needs to worry about the single surrogate &#8216;id&#8217; key on each table.  In the linking table (as a design concern), one should place a Unique Index on the user_id/role_id column combination.</p>
<p>The other option is using a composite candidate key.  I may have the specific terminology wrong, but the idea is that instead of the single surrogate key to identify a unique record in the linking table, you use a design like the diagram below, which combines the columns that are foreign keys to their respective tables to create the primary key.  The combination of the columns creates a unique identifying key.  The difficulty emerges with ORMs attempting to create objects out this design, and attempting to correctly generate the SQL required to make updates / deletes, etc, using each member of the composite key.</p>
<p style="text-align: center;"><a href="http://blog.roweware.com/wp-content/uploads/2009/12/NoSurrogate.png"><img class="size-full wp-image-151 aligncenter" title="NoSurrogate" src="http://blog.roweware.com/wp-content/uploads/2009/12/NoSurrogate.png" alt="" width="399" height="108" /></a></p>
<p>Personally speaking, I&#8217;m a fan of the surrogate key approach, but I&#8217;ve worked with both.  I won&#8217;t discuss the performance impacts of either design, since I don&#8217;t have nearly the research base to accurately describe it.  But, using simple integer based keys, the difference should be low.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roweware.com/2009/12/23/surrogates/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
