<?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>youdidwhatwithtsql.com &#187; DBA</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/category/dba/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Sat, 24 Jul 2010 16:26:10 +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>Can&#8217;t reopen table: &#8216;t1&#8242;</title>
		<link>http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823</link>
		<comments>http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823#comments</comments>
		<pubDate>Mon, 19 Jul 2010 21:24:08 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823</guid>
		<description><![CDATA[I&#8217;m quite often jumping between MySQL and SQL Server so remembering the quirks and limitations of each system can be difficult. With MySQL, if you attempt to reference a temporary table more than once in the same query, you will encounter the following error; Error Code : 1137 Can't reopen table: 't1’ The following provides [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823">Can&#8217;t reopen table: &#8216;t1&#8242;</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m quite often jumping between <a href="http://www.mysql.com" target="_blank">MySQL</a> and <a href="http://www.microsoft.com/en/gb/sqlserver/default.aspx" target="_blank">SQL Server</a> so remembering the quirks and limitations of each system can be difficult. With MySQL, if you attempt to reference a temporary table more than once in the same query, you will encounter the following error;
<pre>Error Code : 1137
Can't reopen table: 't1’</pre>
<p>The following provides an example of this&#8230;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p823code4'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8234"><td class="code" id="p823code4"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test
<span style="color: #FF00FF;">&#40;</span>
                Id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> test <span style="color: #990099; font-weight: bold;">AS</span> t2
     <span style="color: #990099; font-weight: bold;">ON</span> t1.Id <span style="color: #CC0099;">=</span> t2.Id<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>It&#8217;s not just self-joins that have this issue <strong>UNIONS</strong> do as well;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p823code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8235"><td class="code" id="p823code5"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t2<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>There&#8217;s a <a href="http://stackoverflow.com/questions/343402/getting-around-mysql-cant-reopen-table-error">thread over on Stackoverflow</a> discussing this problem. Here&#8217;s a solution I commonly use to get around the problem;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p823code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8236"><td class="code" id="p823code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Create temp table</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test
<span style="color: #FF00FF;">&#40;</span>
                Id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Insert some test data</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test <span style="color: #FF00FF;">&#40;</span>Id<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">3</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">4</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">5</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Clone the table. This will do structure &amp; indices but no data.</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test2 <span style="color: #CC0099; font-weight: bold;">LIKE</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Insert the data into the new table</span>
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test2
<span style="color: #990099; font-weight: bold;">SELECT</span> Id
<span style="color: #990099; font-weight: bold;">FROM</span> test<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Now our queries will work if we use the tables clone</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> test2 <span style="color: #990099; font-weight: bold;">AS</span> t2
      <span style="color: #990099; font-weight: bold;">ON</span> t1.Id <span style="color: #CC0099;">=</span> t2.Id<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test <span style="color: #990099; font-weight: bold;">AS</span> t1
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test2 <span style="color: #990099; font-weight: bold;">AS</span> t2<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Clean up</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> test<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">TEMPORARY</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> test2<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><map name='google_ad_map_823_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/823?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_823_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=823&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcant-reopen-table-t1%2F823' title="Cant reopen table: t1" alt=" Cant reopen table: t1" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823">Can&#8217;t reopen table: &#8216;t1&#8242;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/cant-reopen-table-t1/823/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rename MySQL Stored Procedures</title>
		<link>http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819</link>
		<comments>http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819#comments</comments>
		<pubDate>Wed, 14 Jul 2010 20:47:05 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819</guid>
		<description><![CDATA[I’ve previously blogged about the limitations of MySQL Alter Procedure Syntax and I came across a thread on the MySQL forums with a possible solution. I thought it might be handy to wrap this up into a stored procedure akin to SQL Server’s sp_rename. This procedure will allow you to easily rename MySQL Stored Procedures [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819">Rename MySQL Stored Procedures</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I’ve previously blogged about the limitations of <a href="http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788" target="_blank">MySQL Alter Procedure Syntax</a> and I came across a thread on the <a href="http://forums.mysql.com/read.php?10,274538,274563#msg-274563" target="_blank">MySQL forums</a> with a possible solution. I thought it might be handy to wrap this up into a stored procedure akin to <a href="http://msdn.microsoft.com/en-us/library/ms188351.aspx" target="_blank">SQL Server’s sp_rename</a>. </p>
<p>This procedure will allow you to easily rename MySQL Stored Procedures in any database. Please be aware that this does update the MySQL system tables and has only had minimal testing. As with all tips you find on the Internet please use with caution!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p819code9'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8199"><td class="code" id="p819code9"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
&nbsp;
<span style="color: #990099; font-weight: bold;">USE</span> <span style="color: #008000;">`mysql`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`mysp<span style="color: #008080; font-weight: bold;">_</span>rename<span style="color: #008080; font-weight: bold;">_</span>proc`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DEFINER</span><span style="color: #CC0099;">=</span><span style="color: #008000;">`root`</span>@<span style="color: #008000;">`<span style="color: #008080; font-weight: bold;">%</span>`</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`mysp<span style="color: #008080; font-weight: bold;">_</span>rename<span style="color: #008080; font-weight: bold;">_</span>proc`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> p_proc_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> p_new_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> p_db <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">64</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">MODIFIES SQL DATA</span>
    <span style="color: #990099; font-weight: bold;">SQL SECURITY</span> <span style="color: #990099; font-weight: bold;">INVOKER</span>
    <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Use to rename stored procedures.'</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
    proc: <span style="color: #990099; font-weight: bold;">BEGIN</span>
	<span style="color: #808080; font-style: italic;"># A few tests to see if the input is sensible</span>
	<span style="color: #009900;">IF</span> <span style="color: #000099;">CHARACTER_LENGTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #009900;">IFNULL</span><span style="color: #FF00FF;">&#40;</span>p_proc_name<span style="color: #000033;">,</span> <span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #CC0099; font-weight: bold;">OR</span> <span style="color: #000099;">CHARACTER_LENGTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #009900;">IFNULL</span><span style="color: #FF00FF;">&#40;</span>p_new_name<span style="color: #000033;">,</span> <span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span>
		<span style="color: #CC0099; font-weight: bold;">OR</span> <span style="color: #000099;">CHARACTER_LENGTH</span><span style="color: #FF00FF;">&#40;</span><span style="color: #009900;">IFNULL</span><span style="color: #FF00FF;">&#40;</span>p_db<span style="color: #000033;">,</span> <span style="color: #008000;">''</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Error: One of more of the input parameters is zero in length.'</span> <span style="color: #990099; font-weight: bold;">AS</span> Error<span style="color: #000033;">;</span>
		LEAVE proc<span style="color: #000033;">;</span> 
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	ELSEIF <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span>
		 <span style="color: #990099; font-weight: bold;">FROM</span> mysql.proc
		 <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_proc_name
		 <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`type`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span>
		 <span style="color: #CC0099; font-weight: bold;">AND</span> db <span style="color: #CC0099;">=</span> p_db<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Error: The procedure specified in p<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>name does not exist in this database.'</span> <span style="color: #990099; font-weight: bold;">AS</span> Error<span style="color: #000033;">;</span>
		LEAVE proc<span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	ELSEIF <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span>
		 <span style="color: #990099; font-weight: bold;">FROM</span> mysql.proc
		 <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_new_name
		 <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`type`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span>
		 <span style="color: #CC0099; font-weight: bold;">AND</span> db <span style="color: #CC0099;">=</span> p_db<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">'Error: Unable to rename the procedure specified in p<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>name as it already exists in this database.'</span> <span style="color: #990099; font-weight: bold;">AS</span> Error<span style="color: #000033;">;</span>
		LEAVE proc<span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Rename the proc</span>
	<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`mysql`</span>.<span style="color: #008000;">`proc`</span>
	<span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_new_name<span style="color: #000033;">,</span>
	specific_name <span style="color: #CC0099;">=</span> p_new_name
	<span style="color: #990099; font-weight: bold;">WHERE</span> db <span style="color: #CC0099;">=</span> p_db 
	<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`name`</span> <span style="color: #CC0099;">=</span> p_proc_name
	<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`type`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Update any associated privileges</span>
	<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`mysql`</span>.<span style="color: #008000;">`procs<span style="color: #008080; font-weight: bold;">_</span>priv`</span>
	<span style="color: #990099; font-weight: bold;">SET</span> Routine_name <span style="color: #CC0099;">=</span> p_new_name
	<span style="color: #990099; font-weight: bold;">WHERE</span> db <span style="color: #CC0099;">=</span> p_db 
	<span style="color: #CC0099; font-weight: bold;">AND</span> Routine_name <span style="color: #CC0099;">=</span> p_proc_name
	<span style="color: #CC0099; font-weight: bold;">AND</span> Routine_type <span style="color: #CC0099;">=</span> <span style="color: #008000;">'PROCEDURE'</span><span style="color: #000033;">;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># Check update rowcount to see if privileges need to be flushed</span>
	<span style="color: #009900;">IF</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">ROW_COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">&gt;</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
	<span style="color: #990099; font-weight: bold;">BEGIN</span>
		FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
    <span style="color: #009900;">END</span> proc<span style="color: #000033;">;</span>
    <span style="color: #009900;">END</span>$$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Usage is as follows;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p819code10'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81910"><td class="code" id="p819code10"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> mysp_rename_proc<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'usp<span style="color: #008080; font-weight: bold;">_</span>proc'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'usp<span style="color: #008080; font-weight: bold;">_</span>new<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>name'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'database<span style="color: #008080; font-weight: bold;">_</span>proc<span style="color: #008080; font-weight: bold;">_</span>exists<span style="color: #008080; font-weight: bold;">_</span>in'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>As the thread poster mentions the Stored Procedure is callable by it’s old name as well as the new one until you reconnect. There doesn’t seem to be any suitable <a href="http://dev.mysql.com/doc/refman/5.1/en/flush.html" target="_blank">FLUSH</a> command to resolve this.</p>
<p>If you liked this you may like;</p>
<p><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_spaceused/636" target="_blank">MySQL Clone of SP_SpaceUsed</a></p>
<p><a href="http://www.youdidwhatwithtsql.com/mysql-clone-of-sp_msforeachtable/624" target="_blank">MySQL Clone of SP_MsForEachTable</a></p>
<p><map name='google_ad_map_819_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/819?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_819_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=819&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Frename-mysql-stored-procedures%2F819' title="Rename MySQL Stored Procedures" alt=" Rename MySQL Stored Procedures" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819">Rename MySQL Stored Procedures</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/rename-mysql-stored-procedures/819/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Purge MySQL Binary Logs</title>
		<link>http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816</link>
		<comments>http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816#comments</comments>
		<pubDate>Mon, 12 Jul 2010 10:58:03 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=816</guid>
		<description><![CDATA[From time-to-time you may need to manually purge binary logs on your MySQL slaves to free up a bit of disk space. We can achieve this by using the PURGE BINARY LOGS command from the MySQL command line client. MySQL advises the following procedure when purging these logs To safely purge binary log files, follow [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816">Purge MySQL Binary Logs</a></p>
]]></description>
			<content:encoded><![CDATA[<p>From time-to-time you may need to manually purge binary logs on your <a title="MySQL replication" href="http://dev.mysql.com/doc/refman/5.1/en/replication.html" target="_blank">MySQL slaves</a> to free up a bit of disk space. We can achieve this by using the <a title="PURGE BINARY LOGS" href="http://dev.mysql.com/doc/refman/5.5/en/purge-binary-logs.html" target="_blank">PURGE BINARY LOGS command</a> from the MySQL command line client. MySQL advises the following procedure when purging these logs</p>
<blockquote><p>To safely purge binary log files, follow this procedure:</p>
<div>
<ol type="1">
<li>On each slave server, use <a title="12.4.5.35. SHOW SLAVE STATUS Syntax" href="http://dev.mysql.com/doc/refman/5.5/en/show-slave-status.html"><code>SHOW SLAVE             STATUS</code></a> to check which log file it is reading.</li>
<li>Obtain a listing of the binary log files on the master             server with <a title="12.4.5.2. SHOW BINARY LOGS Syntax" href="http://dev.mysql.com/doc/refman/5.5/en/show-binary-logs.html"><code>SHOW BINARY LOGS</code></a>.</li>
<li>Determine the earliest log file among all the slaves. This             is the target file. If all the slaves are up to date, this             is the last log file on the list.</li>
<li>Make a backup of all the log files you are about to delete.             (This step is optional, but always advisable.)</li>
<li>Purge all log files up to but not including the target file. <a title="MySQL purge binary logs" href="http://dev.mysql.com/doc/refman/5.5/en/purge-binary-logs.html" target="_blank">source</a></li>
</ol>
</div>
</blockquote>
<p>The below example will purge all binary logs older than the one called <strong>backup-master.001271</strong>.</p>
<pre>Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2640654
Server version: 5.1.34-log SUSE MySQL RPM

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql&gt; PURGE BINARY LOGS TO 'backup-master.001271'
</pre>
<p>Provided your slave is running this command is reasonably safe as MySQL will prevent you from purging any log files that are currently being read. If the slave thread is not running then you do have to make sure you are not purging a needed file. If you purge a needed file the slave will break when it is restarted. You can also use the <a title="expire_logs_days in my.cnf" href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_expire_logs_days" target="_blank">expire_logs_days</a> variable to automatically control when binary logs are purged.</p>
<p><map name='google_ad_map_816_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/816?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_816_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=816&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fpurge-mysql-binary-logs%2F816' title="Purge MySQL Binary Logs" alt=" Purge MySQL Binary Logs" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816">Purge MySQL Binary Logs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/purge-mysql-binary-logs/816/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>For RANGE partitions each partition must be defined</title>
		<link>http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815</link>
		<comments>http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815#comments</comments>
		<pubDate>Fri, 09 Jul 2010 12:08:31 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[partition]]></category>
		<category><![CDATA[partitioning]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815</guid>
		<description><![CDATA[If you encounter the following error when trying to create a partitioned table in MySQL Error Code : 1492 For RANGE partitions each partition must be defined Assuming you have defined your partitions then you probably have a syntax error. Take the following incorrect example. ?View Code MYSQLCREATE TABLE People &#40; PersonId INTEGER NOT NULL [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815">For RANGE partitions each partition must be defined</a></p>
]]></description>
			<content:encoded><![CDATA[<p>If you encounter the following error when trying to create a partitioned table in MySQL</p>
<pre>Error Code : 1492
For RANGE partitions each partition must be defined</pre>
<p>Assuming you have defined your partitions then you probably have a syntax error. Take the following incorrect example.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p815code13'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81513"><td class="code" id="p815code13"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> People
<span style="color: #FF00FF;">&#40;</span>
	PersonId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
	FirstName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	LastName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	DateOfBirth <span style="color: #999900; font-weight: bold;">DATE</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Telephone <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Email <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	GroupId <span style="color: #999900; font-weight: bold;">SMALLINT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span>PersonId<span style="color: #000033;">,</span> GroupId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span>
PARTITION BY RANGE <span style="color: #FF00FF;">&#40;</span>GroupId<span style="color: #FF00FF;">&#41;</span>
PARTITION p0 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p1 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p2 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">300</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p3 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">400</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p4 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">500</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p5 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">600</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
PARTITION p6 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">700</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The above statement is not syntactically correct but the error thrown in this case is not particularly helpful. All that is missing here is a couple of braces around the partition range definitions. The below DDL statement is correct.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p815code14'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81514"><td class="code" id="p815code14"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> People
<span style="color: #FF00FF;">&#40;</span>
	PersonId <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
	FirstName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	LastName <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	DateOfBirth <span style="color: #999900; font-weight: bold;">DATE</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Telephone <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	Email <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	GroupId <span style="color: #999900; font-weight: bold;">SMALLINT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span>PersonId<span style="color: #000033;">,</span> GroupId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span>
PARTITION BY RANGE <span style="color: #FF00FF;">&#40;</span>GroupId<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#40;</span>
	PARTITION p0 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p1 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">200</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p2 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">300</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p3 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">400</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p4 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">500</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p5 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">600</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
	PARTITION p6 <span style="color: #990099; font-weight: bold;">VALUES</span> LESS THAN <span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">700</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><map name='google_ad_map_815_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/815?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_815_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=815&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ffor-range-partitions-each-partition-must-be-defined%2F815' title="For RANGE partitions each partition must be defined" alt=" For RANGE partitions each partition must be defined" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815">For RANGE partitions each partition must be defined</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error dropping database (can&#8217;t rmdir &#8216;./database&#8217;, errno: 39)</title>
		<link>http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801</link>
		<comments>http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801#comments</comments>
		<pubDate>Wed, 23 Jun 2010 21:12:55 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[drop database]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801</guid>
		<description><![CDATA[Just a very quick post today! If you encounter this error when attempting to drop a MySQL database; Error dropping database (can't rmdir './database', errno: 39) Then you probably have some rogue files in the folder where the database files are located. If you cd into this directory you will be able to view these [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801">Error dropping database (can&#8217;t rmdir &#8216;./database&#8217;, errno: 39)</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Just a very quick post today! If you encounter this error when attempting to drop a <a href="http://www.mysql.com" target="_blank">MySQL</a> database;</p>
<pre>Error dropping database (can't rmdir './database', errno: 39)</pre>
<p>Then you probably have some rogue files in the folder where the database files are located. If you <a href="http://www.computerhope.com/unix/ucd.htm" target="_blank">cd</a> into this directory you will be able to view these files. In my case I had a <a href="http://www.gnu.org/software/tar/" target="_blank">tar archive</a> containing the files the database was restored from. Just delete any files in this directory and you’ll be able to complete the drop.</p>
<p><map name='google_ad_map_801_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/801?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_801_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=801&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ferror-dropping-database-cant-rmdir-database-errno-39%2F801' title="Error dropping database (cant rmdir ./database, errno: 39)" alt=" Error dropping database (cant rmdir ./database, errno: 39)" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801">Error dropping database (can&#8217;t rmdir &#8216;./database&#8217;, errno: 39)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL ALTER PROCEDURE Syntax</title>
		<link>http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788</link>
		<comments>http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788#comments</comments>
		<pubDate>Thu, 03 Jun 2010 11:15:37 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=788</guid>
		<description><![CDATA[I usually use SQLYog to write any stored procedures for MySQL. Whenever you alter a procedure the editor essentially generates SQL to drop and then recreate it. ?View Code MYSQLDELIMITER $$ &#160; USE `db`$$ &#160; DROP PROCEDURE IF EXISTS `my_proc`$$ &#160; CREATE DEFINER=`root`@`%` PROCEDURE `my proc`&#40;&#41; MODIFIES SQL DATA SQL SECURITY INVOKER COMMENT 'Just an [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788">MySQL ALTER PROCEDURE Syntax</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I usually use <a title="SQLYog MySQL GUI Tool" href="http://www.webyog.com/en/sqlyog_feature_matrix.php" target="_blank">SQLYog</a> to write any stored procedures for MySQL. Whenever you alter a procedure the editor essentially generates SQL to drop and then recreate it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p788code16'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78816"><td class="code" id="p788code16"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
&nbsp;
<span style="color: #990099; font-weight: bold;">USE</span> <span style="color: #008000;">`db`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #009900;">IF</span> <span style="color: #990099; font-weight: bold;">EXISTS</span> <span style="color: #008000;">`my<span style="color: #008080; font-weight: bold;">_</span>proc`</span>$$
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DEFINER</span><span style="color: #CC0099;">=</span><span style="color: #008000;">`root`</span>@<span style="color: #008000;">`<span style="color: #008080; font-weight: bold;">%</span>`</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`my proc`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">MODIFIES SQL DATA</span>
    <span style="color: #990099; font-weight: bold;">SQL SECURITY</span> <span style="color: #990099; font-weight: bold;">INVOKER</span>
    <span style="color: #990099; font-weight: bold;">COMMENT</span> <span style="color: #008000;">'Just an example proc.'</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
       <span style="color: #808080; font-style: italic;"># // proc defintition</span>
<span style="color: #009900;">END</span>$$
&nbsp;
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>I&#8217;ve always wondered why it did this as opposed to using the <a title="MySQL ALTER PROCEDURE syntax" href="http://dev.mysql.com/doc/refman/5.5/en/alter-procedure.html" target="_blank">ALTER PROCEDURE Syntax</a>.  I attempted to use this syntax but couldn&#8217;t get it to parse whatever I did. As usual the manual explains all;</p>
<blockquote><p>This statement can be used to change the characteristics of a stored procedure. More than one change may be specified in an ALTER PROCEDURE statement. However, you cannot change the parameters or body of a stored procedure using this statement; to make such changes, you must drop and re-create the procedure using DROP PROCEDURE and CREATE PROCEDURE. <a title="MySQL ALTER PROCEDURE Syntax" href="http://dev.mysql.com/doc/refman/5.0/en/alter-procedure.html" target="_blank">source</a></p></blockquote>
<p>Okay! The alter procedure statement is virtually useless to us then! The current method of dropping and then recreating routines to modify them is not atomic and can cause <a title="MySQL ALTER PROCEDURE Syntax" href="http://bugs.mysql.com/bug.php?id=9588" target="_blank">problems on production systems</a>. The stored procedure is temporarily unavailable after the first drop statement so this might freak out any other thread calling it at that moment.</p>
<p>I admit this isn&#8217;t a massive problem, and I assume it wouldn&#8217;t be that difficult to implement, but it&#8217;s something you&#8217;d expect DBMS vendors to put in place for stored procedures. I enjoy working with MySQL but some days you can get irrated by a number of these little issues!</p>
<p><map name='google_ad_map_788_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/788?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_788_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=788&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmysql-alter-procedure-syntax%2F788' title="MySQL ALTER PROCEDURE Syntax" alt=" MySQL ALTER PROCEDURE Syntax" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788">MySQL ALTER PROCEDURE Syntax</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cross database foreign keys?</title>
		<link>http://www.youdidwhatwithtsql.com/cross-database-foreign-keys/784</link>
		<comments>http://www.youdidwhatwithtsql.com/cross-database-foreign-keys/784#comments</comments>
		<pubDate>Wed, 02 Jun 2010 19:43:21 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[cross datasbase]]></category>
		<category><![CDATA[Foreign keys]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/cross-database-foreign-keys/784</guid>
		<description><![CDATA[Colleague: “Can you have foreign keys referencing other databases?” Me: “Erm… I don’t know, but it I’ll find out.” I’ve always thought tables with these relationships should exist in the same database so I’ve never attempted this. But I don’t like not knowing things so I set to find out. The answer, as it often [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cross-database-foreign-keys/784">Cross database foreign keys?</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Colleague: “<em>Can you have foreign keys referencing other databases?”</em></p>
<p>Me: “<em>Erm… I don’t know, but it I’ll find out.”</em></p>
<p>I’ve always thought tables with these relationships should exist in the same database so I’ve never attempted this. But I don’t like not knowing things so I set to find out. The answer, as it often seems to be, is “<em>it depends!”</em></p>
<p><strong>Q1: Does MySQL support cross-database foreign keys?</strong></p>
<p>First lets create some test databases and tables. The table called <strong>test1</strong> will reference a table called <strong>test2</strong> in another database.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code23'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78423"><td class="code" id="p784code23"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> test1<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> test2<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">USE</span> test1<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test1
<span style="color: #FF00FF;">&#40;</span>
	idnum <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
	fk_id <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">=</span> <span style="color: #990099; font-weight: bold;">INNODB</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">USE</span> test2<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> test2
<span style="color: #FF00FF;">&#40;</span>
	idnum <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
	fk_id <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #990099; font-weight: bold;">FOREIGN KEY</span> fk_idnum <span style="color: #FF00FF;">&#40;</span>fk_id<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">REFERENCES</span> test1.test1<span style="color: #FF00FF;">&#40;</span>idnum<span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">=</span> <span style="color: #990099; font-weight: bold;">INNODB</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>OK, MySQL seemed to parse that without complaint. Now lets try to insert a record into table <strong>test2</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code24'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78424"><td class="code" id="p784code24"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test2<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test2
<span style="color: #FF00FF;">&#40;</span>
	fk_id
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008080;">1</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>We get an error which shows the constraint is working as you would expect.</p>
<pre># Error Code : 1452
# Cannot add or update a child row: a foreign key constraint fails (`test2`.`test2`, CONSTRAINT `test2_ibfk_1` FOREIGN KEY (`fk_id`) REFERENCES `test1`.`test1` (`idnum`)</pre>
<p>Insert some data into both tables now.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code25'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78425"><td class="code" id="p784code25"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> test1<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test1
<span style="color: #FF00FF;">&#40;</span>
	fk_id
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008080;">1</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">USE</span> test2<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> test2
<span style="color: #FF00FF;">&#40;</span>
	fk_id
<span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">VALUES</span>
<span style="color: #FF00FF;">&#40;</span>
	<span style="color: #008080;">1</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test2.test2
<span style="color: #990099; font-weight: bold;">UNION</span> <span style="color: #990099; font-weight: bold;">ALL</span>
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span>
<span style="color: #990099; font-weight: bold;">FROM</span> test1.test1<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>What happens if we try to drop the <strong>test1</strong> database?</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code26'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78426"><td class="code" id="p784code26"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> test1<span style="color: #000033;">;</span></pre></td></tr></table></div>

<pre># Error Code : 1217
# Cannot delete or update a parent row: a foreign key constraint fail</pre>
<p>To drop both the databases we need to drop them in the below sequence</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code27'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78427"><td class="code" id="p784code27"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Clean up </span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> test2<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DROP</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> test1<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>A1: Yes! MySQL does support cross-database foreign key references. *</strong></p>
<p>* Assuming the <a href="http://dev.mysql.com/doc/refman/5.0/en/storage-engines.html" target="_blank">storage engine</a> in use supports foreign keys.</p>
<p><strong><font color="#666666">Q2: Does SQL Server support cross-database foreign keys?</font></strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p784code28'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78428"><td class="code" id="p784code28"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> test1;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> test2;
&nbsp;
<span style="color: #0000FF;">USE</span> test1;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> test1
<span style="color: #808080;">&#40;</span>
	idnum <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
	fk_id <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">USE</span> test2;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> test2
<span style="color: #808080;">&#40;</span>
	idnum <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,
	fk_id <span style="color: #0000FF;">INT</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>,
	<span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">&#40;</span>fk_id<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">REFERENCES</span> test1.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">test1</span><span style="color: #808080;">&#40;</span>idnum<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<pre>Msg 1763, Level 16, State 0, Line 1
Cross-database foreign key references are not supported. Foreign key 'test1.dbo.test1'.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.</pre>
<p><strong><font color="#666666">A2: No! SQL Server does not support cross-database foreign key references.</font></strong></p>
<p><a href="http://en.wikiquote.org/wiki/Chris_Rock" target="_blank">Chris Rock</a> once said <em>“You can drive a car with your feet if you want to, that don&#8217;t make it a good ******* idea!”. </em>That’s kind of how I feel about cross-database foreign keys. Just because you can, doesn’t mean you should! I’d be inclined to have no foreign key relationship if we’re spitting up data for performance reasons. Otherwise I’d put both tables in the same database where they logically belong.</p>
<p><map name='google_ad_map_784_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/784?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_784_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=784&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcross-database-foreign-keys%2F784' title="Cross database foreign keys?" alt=" Cross database foreign keys?" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/cross-database-foreign-keys/784">Cross database foreign keys?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/cross-database-foreign-keys/784/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL client ran out of memory</title>
		<link>http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773</link>
		<comments>http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773#comments</comments>
		<pubDate>Fri, 21 May 2010 11:38:45 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=773</guid>
		<description><![CDATA[I&#8217;ve been building utilities with PHP and MySQL command-line tools to clone databases. I ran into an issue when exporting data from multi-gigabyte tables using the mysql client program. mysql: Out of memory (Needed 4179968 bytes) ERROR 2008 (HY000) at line 1: MySQL client ran out of memory The fix for this is easy; just use [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773">MySQL client ran out of memory</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been building utilities with <a title="PHP" href="http://php.net" target="_blank">PHP</a> and <a title="MySQL command line tools" href="http://dev.mysql.com/doc/refman/5.0/en/programs-client.html" target="_blank">MySQL command-line tools</a> to clone databases. I ran into an issue when exporting data from multi-gigabyte tables using the <a title="MySQL client program" href="http://dev.mysql.com/doc/refman/5.0/en/mysql.html" target="_blank">mysql client program</a>.</p>
<pre>mysql: Out of memory (Needed 4179968 bytes)
ERROR 2008 (HY000) at line 1: MySQL client ran out of memory</pre>
<p>The fix for this is easy; just use the <a title="MySQL client quick option" href="http://dev.mysql.com/doc/refman/5.1/en/mysql-command-options.html#option_mysql_quick" target="_blank">&#8211;quick</a> option in your call to the mysql command. This prevents the client from caching the resultset forcing it to write each row as it is received.</p>
<pre>mysql -q -h localhost -P 3306 -u user -psecret -D db -e 'SELECT * FROM table' &gt; '/tmp/table.csv'</pre>
<p><map name='google_ad_map_773_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/773?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_773_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=773&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmysql-client-ran-memory%2F773' title="MySQL client ran out of memory" alt=" MySQL client ran out of memory" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773">MySQL client ran out of memory</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check disk space with Bash</title>
		<link>http://www.youdidwhatwithtsql.com/check-disk-space-bash/770</link>
		<comments>http://www.youdidwhatwithtsql.com/check-disk-space-bash/770#comments</comments>
		<pubDate>Tue, 18 May 2010 18:34:31 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Disk Space]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=770</guid>
		<description><![CDATA[Now I&#8217;m working mainly with Linux and MySQL I&#8217;ve had to learn how to accomplish basic tasks in entirely new ways. As a DBA I like to keep an eye on disk space. I wanted something like my Check disk space with Powershell script, but this only works with Windows, so naturally I turned to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-disk-space-bash/770">Check disk space with Bash</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Now I&#8217;m working mainly with Linux and MySQL I&#8217;ve had to learn how to accomplish basic tasks in entirely new ways. As a <a title="Database Administrator" href="http://en.wikipedia.org/wiki/Database_administrator" target="_blank">DBA</a> I like to keep an eye on disk space. I wanted something like my <a title="Check disk space with Powershell" href="http://www.youdidwhatwithtsql.com/check-disk-space-with-powershell-2/195" target="_blank">Check disk space with Powershell</a> script, but this only works with Windows, so naturally I turned to <a title="GNU Bash" href="http://www.gnu.org/software/bash/" target="_blank">Bash</a>. Here&#8217;s a very basic solution that will allow you to check the disk space on multiple Linux servers quickly.</p>
<p>Save the script below to your home directory called <strong>chk_dsk_space.sh </strong>and mark this as executable. Open a terminal and enter <strong>./chk_dsk_space.sh</strong>. You will be prompted for the password for each server before it displays disk usage information.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p770code30'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p77030"><td class="code" id="p770code30"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Set computer names here to check</span>
<span style="color: #007800;">computers</span>=<span style="color: #ff0000;">&quot;user@server1 user@server2 user@server3&quot;</span>
<span style="color: #666666; font-style: italic;"># Work through each computer in the array</span>
<span style="color: #000000; font-weight: bold;">for</span> c <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$computers</span>
<span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;=================================================&quot;</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;= <span style="color: #007800;">$c</span>&quot;</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;=================================================&quot;</span>
                <span style="color: #007800;">command</span>=<span style="color: #ff0000;">&quot;ssh <span style="color: #007800;">$c</span> 'df -h'&quot;</span>
                <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #007800;">$command</span>
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Here&#8217;s some sample output.</p>
<pre>rhys@linux-n0sm:~&gt; ./chk_dsk_space.sh
=================================================
= rhys@server1
=================================================
Password:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda6             6.7G  4.2G  2.2G  67% /
udev                  1.7G  444K  1.7G   1% /dev
/dev/sda7             8.9G  417M  8.0G   5% /home
/dev/sda2              75G   71G  4.1G  95% /windows/C
/dev/sda3              55G   47G  7.6G  86% /windows/D
=================================================
= user@server2
=================================================
Password:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda6             6.7G  4.2G  2.2G  67% /
udev                  1.7G  444K  1.7G   1% /dev
/dev/sda7             8.9G  417M  8.0G   5% /home
/dev/sda2              75G   71G  4.1G  95% /windows/C
/dev/sda3              55G   47G  7.6G  86% /windows/D
=================================================
= user@server3
=================================================
Password:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda6             6.7G  4.2G  2.2G  67% /
udev                  1.7G  444K  1.7G   1% /dev
/dev/sda7             8.9G  417M  8.0G   5% /home
/dev/sda2              75G   71G  4.1G  95% /windows/C
/dev/sda3              55G   47G  7.6G  86% /windows/D
</pre>
<p>Not as elegant as my <a title="Check disk space with Powershell" href="http://www.youdidwhatwithtsql.com/check-disk-space-with-powershell-2/195" target="_blank">Powershell script</a> but it&#8217;s functional and saves me a little time each day. I&#8217;m not finding <a title="GNU Bash" href="http://www.gnu.org/software/bash/" target="_blank">Bash</a> as easy to work with as <a title="Powershell" href="http://technet.microsoft.com/en-us/library/bb978526.aspx" target="_blank">Powershell</a> but that&#8217;s probably more due to my lack of experience than anything else. There&#8217;s an <a title="Open Source Powershell" href="http://igorshare.wordpress.com/2008/04/06/pash-cross-platform-powershell-is-out-in-the-wild-announcement/" target="_blank">Open Source implementation of Powershell</a> I&#8217;ve been thinking of checking out, it doesn&#8217;t implement the Windows specific <a title="Powershell cmdlet" href="http://msdn.microsoft.com/en-us/library/ms714395%28VS.85%29.aspx" target="_blank">cmdlets</a>, but it&#8217;s been good to look at another way of doing things with <a title="GNU Bash" href="http://www.gnu.org/software/bash/" target="_blank">Bash</a>.</p>
<p><map name='google_ad_map_770_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/770?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_770_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=770&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcheck-disk-space-bash%2F770' title="Check disk space with Bash" alt=" Check disk space with Bash" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-disk-space-bash/770">Check disk space with Bash</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/check-disk-space-bash/770/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running multiple instances of MySQL</title>
		<link>http://www.youdidwhatwithtsql.com/running-multiple-instances-mysql/756</link>
		<comments>http://www.youdidwhatwithtsql.com/running-multiple-instances-mysql/756#comments</comments>
		<pubDate>Sat, 15 May 2010 21:26:09 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=756</guid>
		<description><![CDATA[It&#8217;s reasonably easy to run multiple instances of MySQL with the mysqld_multi bash script. This can be really useful in development environments where you need to give several developers their own instance. To install multiple Microsoft SQL Server instances we have to get the install DVD and go through a laborious series of wizards. MySQL [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/running-multiple-instances-mysql/756">Running multiple instances of MySQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s reasonably easy to run multiple instances of MySQL with the <a href="http://dev.mysql.com/doc/refman/5.5/en/mysqld-multi.html">mysqld_multi</a> bash script. This can be really useful in development environments where you need to give several developers their own instance. To install multiple Microsoft SQL Server instances we have to get the install DVD and go through a laborious series of wizards. <a title="MySQL homepage" href="http://www.mysql.com">MySQL</a> makes this easy with a few configuration files changes. In this example I&#8217;m going to outline how to configure 2 instances of MySQL by cloning an existing single instance.<br />
Login to your existing mysql instance and run the below statement to create a user.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p756code32'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75632"><td class="code" id="p756code32"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">GRANT</span> SHUTDOWN <span style="color: #990099; font-weight: bold;">ON</span> <span style="color: #CC0099;">*</span>.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">TO</span> <span style="color: #008000;">'multi<span style="color: #008080; font-weight: bold;">_</span>admin'</span>@<span style="color: #008000;">'localhost'</span> IDENTIFIED BY <span style="color: #008000;">'secret'</span><span style="color: #000033;">;</span>
FLUSH <span style="color: #990099; font-weight: bold;">PRIVILEGES</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The mysqld_multi script needs this user, on each instance, to function correctly. This user only needs the <a title="MySQL SHUTDOWN privilege" href="http://dev.mysql.com/doc/refman/5.1/en/privileges-.html#priv_shutdown">SHUTDOWN privilege</a>.<br />
If you already have an instance of MySQL running then you&#8217;ll want to shut it down before beginning. You can do this at the command prompt with&#8230;</p>
<pre>sudo /sbin/service mysql stop</pre>
<p>Next we need to edit the <a title="MySQL configuration file" href="http://dev.mysql.com/tech-resources/articles/mysql_intro.html#SECTION0001500000">my.cnf</a> file.</p>
<p>First we need to comment out a few lines in the mysqld section.</p>
<pre># The MySQL server
[mysqld]
#port           = 3306
#socket         = /var/run/mysql/mysql.sock
# Change following line if you want to store your database elsewhere
#datadir        = /var/lib/mysql
</pre>
<p>Any other configuration options you leave in the mysqld section will serve as defaults for all MySQL instances. I&#8217;m keeping things simple here but, for production environments, you&#8217;ll want to pay more attention to these settings. I&#8217;m particulary thinking of memory usage for each instance.</p>
<pre># The MySQL server
[mysqld]
#port           = 3306
#socket         = /var/run/mysql/mysql.sock
# Change following line if you want to store your database elsewhere
#datadir        = /var/lib/mysql
</pre>
<p>Add the lines below replacing the username and password values the <strong>multi_admin</strong> user you created earlier. The mysqld line tells the mysqld_multi script the MySQL binary to use.</p>
<pre>[mysqld_multi]
mysqld     = /usr/bin/mysqld_safe # location MySQL binary
mysqladmin = /usr/bin/mysqladmin
log        = /var/log/mysqld_multi.log
user       = multi_admin
password   = secret
</pre>
<p>Add the below line to create 2 instances. It is critical that each instance has its own unique values or the server will fail to start or your data could be corrupted.</p>
<pre>[mysqld1]
port       = 3306
datadir    = /var/lib/mysql
pid-file   = /var/lib/mysql/mysqld.pid
socket     = /var/lib/mysql/mysql.sock
user       = mysql
log-error  = /var/log/mysql1.err

[mysqld2]
port       = 3307
datadir    = /var/lib/mysql-databases/mysqld2
pid-file   = /var/lib/mysql-databases/mysqld2/mysql.pid
socket     = /var/lib/mysql-databases/mysqld2/mysql.sock
user       = mysql
log-error  = /var/log/mysql2.err</pre>
<p>Next create all the required directories on your system.</p>
<pre>rhys@linux-n0sm:~&gt; sudo mkdir /var/lib/mysql-databases/
rhys@linux-n0sm:~&gt; sudo mkdir /var/lib/mysql-databases/mysqld2</pre>
<p>Create a directory for the mysql database for instance 2.</p>
<pre>rhys@linux-n0sm:~&gt; sudo mkdir /var/lib/mysql-databases/mysql</pre>
<p>Copy the mysql database files from the original instance to the second instances database directory. Then this instance will have all of the same users as instance 1.</p>
<pre>sudo cp -r /var/lib/mysql/mysql/ /var/lib/mysql-databases/mysqld2/mysql</pre>
<p>You can also copy across any other databases you want the new server to host. Change the owner of the data directory to the <strong>mysql</strong> user so the instance can read them.</p>
<pre>sudo chown -R mysql:mysql /var/lib/mysql-databases</pre>
<p>Finally we are ready to start up the instances.</p>
<pre>rhys@linux-n0sm:~&gt; mysqld_multi start</pre>
<p>To check both instances have started correctly execute the below command.</p>
<pre>rhys@linux-n0sm:~&gt; mysqld_multi report</pre>
<pre>Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
</pre>
<p>You can see that the mysqld_multi script has started multiple mysql processes with the following commands.</p>
<pre>ps -e | grep "mysql"
10779 pts/2    00:00:00 mysqld_safe
11002 pts/2    00:00:00 mysqld
11166 pts/2    00:00:00 mysqld_safe
11279 pts/2    00:00:00 mysqld
</pre>
<p>To stop both instances just execute the below command.</p>
<pre>rhys@linux-n0sm:~&gt; mysqld_multi stop</pre>
<p>We are also able to control individual instances by referring to the assigned number.</p>
<pre>rhys@linux-n0sm:~&gt; mysqld_multi stop 1
rhys@linux-n0sm:~&gt; mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is not running
MySQL server from group: mysqld2 is running
rhys@linux-n0sm:~&gt; mysqld_multi start 1
rhys@linux-n0sm:~&gt; mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
</pre>
<p>Check the manual for more details on <a title="mysqld_multi" href="http://dev.mysql.com/doc/refman/5.0/en/mysqld-multi.html" target="_self">mysqld_multi</a>.</p>
<p><map name='google_ad_map_756_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/756?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_756_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=756&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Frunning-multiple-instances-mysql%2F756' title="Running multiple instances of MySQL" alt=" Running multiple instances of MySQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/running-multiple-instances-mysql/756">Running multiple instances of MySQL</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/running-multiple-instances-mysql/756/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
