<?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>TheHippo &#187; database</title>
	<atom:link href="http://blog.thehippo.de/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.thehippo.de</link>
	<description>if (i=1) throw null;</description>
	<lastBuildDate>Wed, 02 Mar 2011 18:06:45 +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>How to combine DELETE and JOIN in one MySQL query</title>
		<link>http://blog.thehippo.de/2011/01/server/delete-and-join-mysql-query/</link>
		<comments>http://blog.thehippo.de/2011/01/server/delete-and-join-mysql-query/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 18:33:30 +0000</pubDate>
		<dc:creator>Hippo</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://blog.thehippo.de/?p=334</guid>
		<description><![CDATA[Today I discovered that a while back on a PHP/HTML/MySQL project I made some serious mistakes on my database design. To explain it in short:

I have one a table, lets call it "table1", and another table, lets call it "table2". Table1 references a row from table2, but I didn't used foreign keys in these ...]]></description>
			<content:encoded><![CDATA[<p>Today I discovered that a while back on a PHP/HTML/MySQL project I made some serious mistakes on my database design. To explain it in short:</p>
<p>I have one a table, lets call it &#8220;table1&#8243;, and another table, lets call it &#8220;table2&#8243;. Table1 references a row from table2, but I didn&#8217;t used foreign keys in these days. (I don&#8217;t if I didn&#8217;t know that they are exist or if InnoDB wasn&#8217;t available.) The Website has an admin tool which edits all the tables through some HTML forms. My mistake was that it is possible to delete a row from table2 which leaves all the rows in table1 referencing this row in table2 somehow broken. Today someone discovered this mistake I needed to clean this up. My idea was to create a simple query which cleans table1 and the alter the PHP which deleting rows from table2 to also clean up table1.</p>
<p>I started which the following query:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">DELETE</span> <span class="kw1">FROM</span> table1 <span class="kw1">WHERE</span> id <span class="kw5">IN</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">SELECT</span> t1.id</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">FROM</span> table1 AS t1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">LEFT</span> <span class="kw1">JOIN</span> table2 AS t2 ON t1.key = t2.key</div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">WHERE</span> t2.key <span class="kw5">IS</span> <span class="kw3">NULL</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>Which leads to the following error:</p>
<pre>#1093 - You can't specify target table 'table1' for update in FROM clause</pre>
<p>I was some kind of shocked because I had no idea whats wrong with this query. After searching the web for a while I found out that it is some kind of bug in MySQL but there is a weird looking workaround:<span id="more-334"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">DELETE</span> <span class="kw1">FROM</span> table1 <span class="kw1">WHERE</span> id <span class="kw5">IN</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">SELECT</span> id</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">FROM</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">SELECT</span> t1.id</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">FROM</span> table1 AS t1</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">LEFT</span> <span class="kw1">JOIN</span> table2 AS t2 ON t1.key = t2.key</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">WHERE</span> t2.key <span class="kw5">IS</span> <span class="kw3">NULL</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#41;</span> AS temp</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>This works very nicely, but costs a lot of performance, because you need to add another subquery and you create a temporary table. So the better solution is to sanitise all your data before you remove some referenced rows <img src='http://blog.thehippo.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p class="wp-flattr-button"></p> <p><a href="http://blog.thehippo.de/?flattrss_redirect&amp;id=334&amp;md5=3bf6080bce612a67a9b7acb2481b073a" title="Flattr" target="_blank"><img src="http://blog.thehippo.de/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.thehippo.de/2011/01/server/delete-and-join-mysql-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Buddy &#8211; a PHPMyAdmin alternative</title>
		<link>http://blog.thehippo.de/2009/12/tools-and-software/sqlbuddy-a-phpmyadmin-alternative/</link>
		<comments>http://blog.thehippo.de/2009/12/tools-and-software/sqlbuddy-a-phpmyadmin-alternative/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 04:05:09 +0000</pubDate>
		<dc:creator>Hippo</dc:creator>
				<category><![CDATA[Tools and Software]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPMyAdmin]]></category>
		<category><![CDATA[SQL Buddy]]></category>

		<guid isPermaLink="false">http://blog.thehippo.de/?p=169</guid>
		<description><![CDATA[In the last time I am back to a lot of server development. This also means a lot of database engineering. For the most the time I have used PHPMyAdmin for nearly every I have to work with a MySQL database. After a while a got a little bit disappointed, because PHPMyAdmin is a ...]]></description>
			<content:encoded><![CDATA[<p>In the last time I am back to a lot of server development. This also means a lot of database engineering. For the most the time I have used <a href="http://www.phpmyadmin.net" target="_blank">PHPMyAdmin</a> for nearly every I have to work with a MySQL database. After a while a got a little bit disappointed, because PHPMyAdmin is a kind of bloated and sometimes it&#8217;s just take to long to perform a simple operation.<span id="more-169"></span></p>
<h2>The offline alternative</h2>
<p>A looked around for some alternatives and the first found was the MySQL Query Browser which is part of the <a href="http://dev.mysql.com/downloads/gui-tools/5.0.html" target="_blank">MySQL GUI Tools</a>. It is fast (okay, it is a installed application, but still its fast) and very comfortable for editing and creating tables and databases and also okay for just filling / editing some data inside the tables. Also nice is that is free.</p>
<div id="attachment_171" class="wp-caption aligncenter" style="width: 611px"><a href="http://blog.thehippo.de/wp-content/uploads/2009/12/mysql-query-browser-1.png"><img class="size-full wp-image-171" title="MySQL Query Browser" src="http://blog.thehippo.de/wp-content/uploads/2009/12/mysql-query-browser-1.png" alt="MySQL Query Browser" width="601" height="453" /></a><p class="wp-caption-text">MySQL Query Browser - result view</p></div>
<div id="attachment_172" class="wp-caption aligncenter" style="width: 548px"><a href="http://blog.thehippo.de/wp-content/uploads/2009/12/mysql-query-browser-2.png"><img class="size-full wp-image-172" title="MySQL Query Browser" src="http://blog.thehippo.de/wp-content/uploads/2009/12/mysql-query-browser-2.png" alt="MySQL Query Browser - Table option view" width="538" height="384" /></a><p class="wp-caption-text">MySQL Query Browser - Table option view</p></div>
<p>In the MySQL GUI Tools there is also a so called MySQL Administrator which is nice for some statistics, setting up users and creating and restoring backups.</p>
<h2>The online alternative</h2>
<p>There is only one but a big con with the MySQL Query browser. I can not run it on my server, because therefore I needed to open the MySQL port and that one thing I definitely will not do.</p>
<p>So I searched for a while on the web and the tool which looked best for me was <a href="http://www.sqlbuddy.com/" target="_blank">SQL Buddy</a>:</p>
<ul>
<li>PHP-based</li>
<li>makes heavy use of AJAX</li>
<li>enough options for the most common operations</li>
<li>size just 1 MB (instead of 11 MB for the full version of PHPMyAdmin)</li>
</ul>
<div id="attachment_179" class="wp-caption aligncenter" style="width: 620px"><a href="http://blog.thehippo.de/wp-content/uploads/2009/12/SQL-Buddy-1.png"><img class="size-full wp-image-179 " title="SQL Buddy" src="http://blog.thehippo.de/wp-content/uploads/2009/12/SQL-Buddy-1.png" alt="SQL Buddy - result view" width="610" height="437" /></a><p class="wp-caption-text">SQL Buddy - result view</p></div>
<div id="attachment_180" class="wp-caption aligncenter" style="width: 620px"><a href="http://blog.thehippo.de/wp-content/uploads/2009/12/SQL-Buddy-2.png"><img class="size-full wp-image-180 " title="SQL Buddy" src="http://blog.thehippo.de/wp-content/uploads/2009/12/SQL-Buddy-2.png" alt="SQL Buddy - Table option view" width="610" height="437" /></a><p class="wp-caption-text">SQL Buddy - Table option view</p></div>
<p>For the most common tasks SQL Buddy is perfect. It is nearly as fast as installed application and easy to use.</p>
<p>But nobody is perfect, there are few thing I found out, that could not be archived with SQL Buddy. There is no option to create and import xx.sql.bz2 files, and currently no handling of the different storage engines, which means that you could not use foreign keys for example.</p>
<h2>Conclusion</h2>
<p>For you local computer you are okay with the MySQL GUI Tools and SQL Buddy, on the server I will use SQL Buddy but for the more complex tasks I will stick to PHPMyAdmin.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://blog.thehippo.de/2009/12/tools-and-software/sqlbuddy-a-phpmyadmin-alternative/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.226 seconds -->

