<?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: Find missing foreign/primary keys in SQL Server</title>
	<atom:link href="http://richarddingwall.name/2008/12/21/find-missing-foreignprimary-keys-in-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://richarddingwall.name/2008/12/21/find-missing-foreignprimary-keys-in-sql-server/</link>
	<description>The adventures of a young kiwi software developer in London</description>
	<lastBuildDate>Tue, 07 Sep 2010 06:56:23 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Paul Fox</title>
		<link>http://richarddingwall.name/2008/12/21/find-missing-foreignprimary-keys-in-sql-server/comment-page-1/#comment-2046</link>
		<dc:creator>Paul Fox</dc:creator>
		<pubDate>Fri, 06 Feb 2009 06:00:08 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=752#comment-2046</guid>
		<description>Thats great, 

I made a couple of changes so that the FK column has to be the first column in the index. And it actually outputs a script to create the missing indexes.



SELECT
	&#039;create index IX_&#039; + t.name + &#039;_&#039; + c.name + &#039; on &#039; + t.name + &#039;(&#039; + c.name + &#039;)&#039;
FROM
	sys.tables t

	INNER JOIN sys.syscolumns c ON
		c.id = t.OBJECT_ID

	-- Join on foreign key columns
	inner JOIN sys.foreign_key_columns fkc ON
		(fkc.parent_object_id = t.OBJECT_ID
		AND c.colid = fkc.parent_column_id)
		OR (fkc.referenced_object_id = t.OBJECT_ID
		AND c.colid = fkc.referenced_column_id)
		and fkc.constraint_object_id IS not NULL

-- only get the FKs where the first column is not indexed
where not exists (
	select 1 
	from sys.index_columns ic 
	where
		ic.OBJECT_ID = t.OBJECT_ID		
		AND ic.column_id = c.colid
		--first column only
		and ic.key_ordinal = 1
	)
ORDER BY
	t.name,
	c.name</description>
		<content:encoded><![CDATA[<p>Thats great, </p>
<p>I made a couple of changes so that the FK column has to be the first column in the index. And it actually outputs a script to create the missing indexes.</p>
<p>SELECT<br />
	&#8216;create index IX_&#8217; + t.name + &#8216;_&#8217; + c.name + &#8216; on &#8216; + t.name + &#8216;(&#8216; + c.name + &#8216;)&#8217;<br />
FROM<br />
	sys.tables t</p>
<p>	INNER JOIN sys.syscolumns c ON<br />
		c.id = t.OBJECT_ID</p>
<p>	&#8211; Join on foreign key columns<br />
	inner JOIN sys.foreign_key_columns fkc ON<br />
		(fkc.parent_object_id = t.OBJECT_ID<br />
		AND c.colid = fkc.parent_column_id)<br />
		OR (fkc.referenced_object_id = t.OBJECT_ID<br />
		AND c.colid = fkc.referenced_column_id)<br />
		and fkc.constraint_object_id IS not NULL</p>
<p>&#8211; only get the FKs where the first column is not indexed<br />
where not exists (<br />
	select 1<br />
	from sys.index_columns ic<br />
	where<br />
		ic.OBJECT_ID = t.OBJECT_ID<br />
		AND ic.column_id = c.colid<br />
		&#8211;first column only<br />
		and ic.key_ordinal = 1<br />
	)<br />
ORDER BY<br />
	t.name,<br />
	c.name</p>
]]></content:encoded>
	</item>
</channel>
</rss>
