<?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 All Tables With No Primary Key</title>
	<atom:link href="http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/</link>
	<description>News and views from Pythian DBAs</description>
	<lastBuildDate>Sat, 20 Mar 2010 20:27:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Glanzer</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-413961</link>
		<dc:creator>Glanzer</dc:creator>
		<pubDate>Mon, 15 Mar 2010 16:48:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-413961</guid>
		<description>^^^ Ooops, that is for SQL Server.  Sorry I didn&#039;t catch that this was a MySQL thread!</description>
		<content:encoded><![CDATA[<p>^^^ Ooops, that is for SQL Server.  Sorry I didn&#8217;t catch that this was a MySQL thread!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Glanzer</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-413959</link>
		<dc:creator>Glanzer</dc:creator>
		<pubDate>Mon, 15 Mar 2010 16:46:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-413959</guid>
		<description>Way too complicated.  This is all you need:

SELECT name AS &quot;Table&quot; FROM sys.TABLES
WHERE OBJECTPROPERTY(object_id,&#039;TableHasPrimaryKey&#039;) = 0;

To do the same for all databases:

exec SP_MSForEachDB &#039;use [?]; select DB_NAME(), name as &quot;Table&quot; FROM sys.TABLES
WHERE OBJECTPROPERTY(object_id,&#039;&#039;TableHasPrimaryKey&#039;&#039;) = 0;&#039;</description>
		<content:encoded><![CDATA[<p>Way too complicated.  This is all you need:</p>
<p>SELECT name AS &#8220;Table&#8221; FROM sys.TABLES<br />
WHERE OBJECTPROPERTY(object_id,&#8217;TableHasPrimaryKey&#8217;) = 0;</p>
<p>To do the same for all databases:</p>
<p>exec SP_MSForEachDB &#8216;use [?]; select DB_NAME(), name as &#8220;Table&#8221; FROM sys.TABLES<br />
WHERE OBJECTPROPERTY(object_id,&#8221;TableHasPrimaryKey&#8221;) = 0;&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sathish</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-361453</link>
		<dc:creator>Sathish</dc:creator>
		<pubDate>Mon, 01 Jun 2009 10:40:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-361453</guid>
		<description>Hi,
  
   It&#039;s really helpful for me.Thanks a lot!!!!</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>   It&#8217;s really helpful for me.Thanks a lot!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sheeri Cabral</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-281414</link>
		<dc:creator>Sheeri Cabral</dc:creator>
		<pubDate>Wed, 24 Sep 2008 14:00:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-281414</guid>
		<description>Samit,

MySQL does not use PL/SQL so I&#039;m afraid I cannot help you.</description>
		<content:encoded><![CDATA[<p>Samit,</p>
<p>MySQL does not use PL/SQL so I&#8217;m afraid I cannot help you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samit Katiyar</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-281334</link>
		<dc:creator>Samit Katiyar</dc:creator>
		<pubDate>Wed, 24 Sep 2008 08:45:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-281334</guid>
		<description>I tried to execute the mentioned query to get All tables and their primary keys, if exist, BUT IT&quot;S GIVING ERROR:
&quot;Invalid Number of Argument&quot; at concat............

Can u plz explain how to get all tables having a common PK or FK......in PL/SQL Dev

Thanks</description>
		<content:encoded><![CDATA[<p>I tried to execute the mentioned query to get All tables and their primary keys, if exist, BUT IT&#8221;S GIVING ERROR:<br />
&#8220;Invalid Number of Argument&#8221; at concat&#8230;&#8230;&#8230;&#8230;</p>
<p>Can u plz explain how to get all tables having a common PK or FK&#8230;&#8230;in PL/SQL Dev</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sheeri Cabral</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-158529</link>
		<dc:creator>Sheeri Cabral</dc:creator>
		<pubDate>Thu, 07 Feb 2008 03:21:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-158529</guid>
		<description>On the forge at:

http://forge.mysql.com/snippets/view.php?id=119</description>
		<content:encoded><![CDATA[<p>On the forge at:</p>
<p><a href="http://forge.mysql.com/snippets/view.php?id=119" rel="nofollow">http://forge.mysql.com/snippets/view.php?id=119</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stoner</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-154396</link>
		<dc:creator>Stoner</dc:creator>
		<pubDate>Tue, 29 Jan 2008 00:20:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-154396</guid>
		<description>I&#039;ll incorporate variations of these into my MyHelper project (sourceforge.net).</description>
		<content:encoded><![CDATA[<p>I&#8217;ll incorporate variations of these into my MyHelper project (sourceforge.net).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roland Bouman</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-154392</link>
		<dc:creator>Roland Bouman</dc:creator>
		<pubDate>Mon, 28 Jan 2008 23:59:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-154392</guid>
		<description>Hi Sheeri! 

Heads up, you do CONCAT(t.table_name,&quot;.&quot;,t.table_schema) and you have a comma too much after 

as tbl, 

that said, I think it&#039;s somewhat cleaner and perhaps faster if you use 

TABLE_CONSTRAINTS rather than KEY_COLUMN_USAGE 

and 

CONSTRAINT_TYPE = &#039;PRIMARY KEY&#039; rather than CONSTRAINT_NAME = &#039;PRIMARY&#039;.

Also, if you don&#039;t check for the TABLE_TYPE, you will report VIEWs as not having a primary key. I&#039;m not sure if that&#039;s intended but if not I recommend including a 

TABLE_TYPE = &#039;BASE TABLE&#039; 

in the where and this will automatically get rid of the tables in the information_schema.

SELECT t.table_schema
,       t.table_name
FROM information_schema.TABLES t
LEFT JOIN information_schema.TABLE_CONSTRAINTS c
ON t.table_schema = c.table_schema
AND t.table_name = c.table_name
AND &#039;PRIMARY KEY&#039; = c.constraint_type
WHERE c.constraint_name IS NULL
AND t.table_type = &#039;BASE TABLE&#039;;

Also, I&#039;m not sure if it&#039;s intended, but the second query will return multiple rows if the primary key is a composite key (one row for each column in the pk). For reporting I would probably use GROUP_CONCAT the columns to get exactly as many rows as there are primary keys.</description>
		<content:encoded><![CDATA[<p>Hi Sheeri! </p>
<p>Heads up, you do CONCAT(t.table_name,&#8221;.&#8221;,t.table_schema) and you have a comma too much after </p>
<p>as tbl, </p>
<p>that said, I think it&#8217;s somewhat cleaner and perhaps faster if you use </p>
<p>TABLE_CONSTRAINTS rather than KEY_COLUMN_USAGE </p>
<p>and </p>
<p>CONSTRAINT_TYPE = &#8216;PRIMARY KEY&#8217; rather than CONSTRAINT_NAME = &#8216;PRIMARY&#8217;.</p>
<p>Also, if you don&#8217;t check for the TABLE_TYPE, you will report VIEWs as not having a primary key. I&#8217;m not sure if that&#8217;s intended but if not I recommend including a </p>
<p>TABLE_TYPE = &#8216;BASE TABLE&#8217; </p>
<p>in the where and this will automatically get rid of the tables in the information_schema.</p>
<p>SELECT t.table_schema<br />
,       t.table_name<br />
FROM information_schema.TABLES t<br />
LEFT JOIN information_schema.TABLE_CONSTRAINTS c<br />
ON t.table_schema = c.table_schema<br />
AND t.table_name = c.table_name<br />
AND &#8216;PRIMARY KEY&#8217; = c.constraint_type<br />
WHERE c.constraint_name IS NULL<br />
AND t.table_type = &#8216;BASE TABLE&#8217;;</p>
<p>Also, I&#8217;m not sure if it&#8217;s intended, but the second query will return multiple rows if the primary key is a composite key (one row for each column in the pk). For reporting I would probably use GROUP_CONCAT the columns to get exactly as many rows as there are primary keys.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jay Pipes</title>
		<link>http://www.pythian.com/news/803/find-all-tables-with-no-primary-key/#comment-154322</link>
		<dc:creator>Jay Pipes</dc:creator>
		<pubDate>Mon, 28 Jan 2008 20:18:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-154322</guid>
		<description>Hi!  Good ones.  Can you forge `em?</description>
		<content:encoded><![CDATA[<p>Hi!  Good ones.  Can you forge `em?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
