<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.6.5" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Find All Tables With No Primary Key</title>
	<link>http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key</link>
	<description>News and views from Pythian DBAs</description>
	<pubDate>Fri,  9 Jan 2009 21:15:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: Sheeri Cabral</title>
		<link>http://www.pythian.com/blogs/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>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'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/blogs/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>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"S GIVING ERROR:
"Invalid Number of Argument" 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/blogs/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>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/blogs/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>http://www.pythian.com/blogs/803/find-all-tables-with-no-primary-key#comment-154396</guid>
		<description>I'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/blogs/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>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,".",t.table_schema) and you have a comma too much after 

as tbl, 

that said, I think it's somewhat cleaner and perhaps faster if you use 

TABLE_CONSTRAINTS rather than KEY_COLUMN_USAGE 

and 

CONSTRAINT_TYPE = 'PRIMARY KEY' rather than CONSTRAINT_NAME = 'PRIMARY'.

Also, if you don't check for the TABLE_TYPE, you will report VIEWs as not having a primary key. I'm not sure if that's intended but if not I recommend including a 

TABLE_TYPE = 'BASE TABLE' 

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 'PRIMARY KEY' = c.constraint_type
WHERE c.constraint_name IS NULL
AND t.table_type = 'BASE TABLE';

Also, I'm not sure if it'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/blogs/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>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>
