<?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; MySQL</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +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>MySQL Storage engine benchmarking</title>
		<link>http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384</link>
		<comments>http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384#comments</comments>
		<pubDate>Mon, 07 Nov 2011 20:49:37 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[InnoDB]]></category>
		<category><![CDATA[MyISAM]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384</guid>
		<description><![CDATA[Here&#8217;s a stored procedure I use to perform some simple benchmarking of inserts for MySQL. It takes three parameters; p_table_type which should be set to the storage engine you wish to benchmark i.e. &#8216;MyISAM&#8217;, &#8216;InnoDB&#8217;, p_inserts; set this to the number of inserts to perform. p_autocommit; set the autocommit variable (relevant to InnoDB only) to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384">MySQL Storage engine benchmarking</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a stored procedure I use to perform some simple benchmarking of inserts for <a title="MySQL" href="http://dev.mysql.com" target="_blank">MySQL</a>. It takes three parameters; <strong>p_table_type</strong> which should be set to the storage engine you wish to benchmark i.e. &#8216;MyISAM&#8217;, &#8216;InnoDB&#8217;, <strong>p_inserts</strong>; set this to the number of inserts to perform. <strong>p_autocommit</strong>; set the <a title="InnoDB autocommit variable" href="http://dev.mysql.com/doc/refman/5.0/en/innodb-transaction-model.html" target="_blank">autocommit</a> variable (relevant to <a title="InnoDB Storage Engine" href="http://www.innodb.com/" target="_blank">InnoDB</a> only) to 0 or 1.</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('p1384code5'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13845"><td class="code" id="p1384code5"><pre class="mysql" style="font-family:monospace;">DELIMITER $$
<span style="color: #990099; font-weight: bold;">USE</span> <span style="color: #008000;">`test`</span>$$
<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;">`table<span style="color: #008080; font-weight: bold;">_</span>engine<span style="color: #008080; font-weight: bold;">_</span>test`</span>$$
        <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;">`table<span style="color: #008080; font-weight: bold;">_</span>engine<span style="color: #008080; font-weight: bold;">_</span>test`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> p_table_type <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span>
                                      <span style="color: #990099; font-weight: bold;">IN</span> p_inserts    <span style="color: #999900; font-weight: bold;">INT</span><span style="color: #000033;">,</span>
                                      <span style="color: #990099; font-weight: bold;">IN</span> p_autocommit <span style="color: #999900; font-weight: bold;">TINYINT</span><span style="color: #FF00FF;">&#41;</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;">BEGIN</span>
&nbsp;
                <span style="color: #990099; font-weight: bold;">DECLARE</span> sql_string <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">300</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
                <span style="color: #808080; font-style: italic;"># Set session autocommit</span>
                <span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #990099; font-weight: bold;">SESSION</span> autocommit <span style="color: #CC0099;">=</span> p_autocommit<span style="color: #000033;">;</span>
&nbsp;
                <span style="color: #808080; font-style: italic;"># TABLE TO hold session times</span>
                <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> test_session
                             <span style="color: #FF00FF;">&#40;</span>
                                          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: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
                                          table_type <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">20</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>
                                          inserts <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>
                                          autocommit <span style="color: #999900; font-weight: bold;">TINYINT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
                                          started    <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>                          <span style="color: #000033;">,</span>
                                          finished   <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
                             <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
                <span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> test_session_inserts
                             <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: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #000033;">,</span>
                                          test_session_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: #000033;">,</span>
                                          started         <span style="color: #999900; font-weight: bold;">DATETIME</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>                          <span style="color: #000033;">,</span>
                                          finished        <span style="color: #999900; font-weight: bold;">DATETIME</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: #000033;">;</span>
&nbsp;
                <span style="color: #808080; font-style: italic;"># clean up ANY existing test TABLE</span>
                <span style="color: #990099; font-weight: bold;">DROP</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_table_type<span style="color: #000033;">;</span>
&nbsp;
                <span style="color: #990099; font-weight: bold;">SET</span> sql_string <span style="color: #CC0099;">=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'CREATE TABLE test<span style="color: #008080; font-weight: bold;">_</span>table<span style="color: #008080; font-weight: bold;">_</span>type
					(
						id INTEGER NOT NULL PRIMARY KEY AUTO<span style="color: #008080; font-weight: bold;">_</span>INCREMENT,
						random INTEGER,
						text1 VARCHAR(10) NOT NULL,
						text2 VARCHAR(10) NOT NULL
					)ENGINE = '</span><span style="color: #000033;">,</span> p_table_type<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
                <span style="color: #808080; font-style: italic;"># PREPARE SQL AND EXECUTE</span>
                <span style="color: #990099; font-weight: bold;">SET</span> @q <span style="color: #CC0099;">=</span> sql_string<span style="color: #000033;">;</span>
                PREPARE stmt <span style="color: #990099; font-weight: bold;">FROM</span> @q<span style="color: #000033;">;</span>
                EXECUTE stmt<span style="color: #000033;">;</span>
                DEALLOCATE PREPARE stmt<span style="color: #000033;">;</span>
                <span style="color: #808080; font-style: italic;"># START the test session</span>
                <span style="color: #990099; font-weight: bold;">INSERT</span>
                <span style="color: #990099; font-weight: bold;">INTO</span>   test_session
                       <span style="color: #FF00FF;">&#40;</span>
                              table_type<span style="color: #000033;">,</span>
                              inserts<span style="color: #000033;">,</span>
                              autocommit<span style="color: #000033;">,</span>
                              started
                       <span style="color: #FF00FF;">&#41;</span>
                       <span style="color: #990099; font-weight: bold;">VALUES</span>
                       <span style="color: #FF00FF;">&#40;</span>
                              p_table_type<span style="color: #000033;">,</span>
                              p_inserts<span style="color: #000033;">,</span>
                              p_autocommit<span style="color: #000033;">,</span>
                              <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
                       <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
                <span style="color: #990099; font-weight: bold;">SET</span> @id               <span style="color: #CC0099;">=</span> <span style="color: #000099;">LAST_INSERT_ID</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
                <span style="color: #990099; font-weight: bold;">SET</span> @i                <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span><span style="color: #000033;">;</span>
                while_loop: WHILE <span style="color: #FF00FF;">&#40;</span>@i <span style="color: #CC0099;">&lt;</span> p_inserts<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">DO</span>
                <span style="color: #808080; font-style: italic;"># Generate VALUES for insert</span>
                <span style="color: #990099; font-weight: bold;">SET</span> @random  <span style="color: #CC0099;">=</span> <span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
                <span style="color: #990099; font-weight: bold;">SET</span> @text1   <span style="color: #CC0099;">=</span> <span style="color: #000099;">SUBSTRING</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">MD5</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #CC0099;">-</span><span style="color: #008080;">10</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
                <span style="color: #990099; font-weight: bold;">SET</span> @text2   <span style="color: #CC0099;">=</span> <span style="color: #000099;">SUBSTRING</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">MD5</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">RAND</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #CC0099;">-</span><span style="color: #008080;">10</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
                <span style="color: #990099; font-weight: bold;">SET</span> @started <span style="color: #CC0099;">=</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
                <span style="color: #808080; font-style: italic;"># INSERT the test row</span>
                <span style="color: #990099; font-weight: bold;">INSERT</span>
                <span style="color: #990099; font-weight: bold;">INTO</span>   test_table_type
                       <span style="color: #FF00FF;">&#40;</span>
                              random<span style="color: #000033;">,</span>
                              text1 <span style="color: #000033;">,</span>
                              text2
                       <span style="color: #FF00FF;">&#41;</span>
                       <span style="color: #990099; font-weight: bold;">VALUES</span>
                       <span style="color: #FF00FF;">&#40;</span>
                              @random<span style="color: #000033;">,</span>
                              @text1 <span style="color: #000033;">,</span>
                              @text2
                       <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
                <span style="color: #808080; font-style: italic;"># record INSERT times</span>
                        <span style="color: #990099; font-weight: bold;">INSERT</span>
                        <span style="color: #990099; font-weight: bold;">INTO</span>   test_session_inserts
                               <span style="color: #FF00FF;">&#40;</span>
                                      test_session_id<span style="color: #000033;">,</span>
                                      started        <span style="color: #000033;">,</span>
                                      finished
                               <span style="color: #FF00FF;">&#41;</span>
                               <span style="color: #990099; font-weight: bold;">VALUES</span>
                               <span style="color: #FF00FF;">&#40;</span>
                                      @id     <span style="color: #000033;">,</span>
                                      @started<span style="color: #000033;">,</span>
                                      <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
                               <span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
                        <span style="color: #808080; font-style: italic;"># increment counter</span>
                        <span style="color: #990099; font-weight: bold;">SET</span> @i <span style="color: #CC0099;">=</span> @i <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #000033;">;</span>
                <span style="color: #009900;">END</span> WHILE while_loop<span style="color: #000033;">;</span>
                <span style="color: #808080; font-style: italic;"># Finish the session</span>
                <span style="color: #990099; font-weight: bold;">UPDATE</span> test_session
                <span style="color: #990099; font-weight: bold;">SET</span>    finished <span style="color: #CC0099;">=</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span>
                <span style="color: #990099; font-weight: bold;">WHERE</span>  id       <span style="color: #CC0099;">=</span> @id<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">END</span>$$
DELIMITER <span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Run your tests like so&#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('p1384code6'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13846"><td class="code" id="p1384code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CALL</span> table_engine_test<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'MyISAM'</span><span style="color: #000033;">,</span> <span style="color: #008080;">10000</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CALL</span> table_engine_test<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'InnoDB'</span><span style="color: #000033;">,</span> <span style="color: #008080;">10000</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CALL</span> table_engine_test<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'InnoDB'</span><span style="color: #000033;">,</span> <span style="color: #008080;">10000</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CALL</span> table_engine_test<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'MyISAM'</span><span style="color: #000033;">,</span> <span style="color: #008080;">100000</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CALL</span> table_engine_test<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'InnoDB'</span><span style="color: #000033;">,</span> <span style="color: #008080;">100000</span><span style="color: #000033;">,</span> <span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">CALL</span> table_engine_test<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'InnoDB'</span><span style="color: #000033;">,</span> <span style="color: #008080;">100000</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The <strong>test_session</strong> contains some summary information about each of the tests;</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('p1384code7'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13847"><td class="code" id="p1384code7"><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_session<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/11/mysql_table_engine_test.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="mysql_table_engine_test" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/11/mysql_table_engine_test_thumb.png" alt="mysql table engine test thumb MySQL Storage engine benchmarking" width="644" height="166" border="0" /></a></p>
<p>Calculate the time taken for each test with the following query;</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('p1384code8'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13848"><td class="code" id="p1384code8"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span><span style="color: #000033;">,</span> TIMEDIFF<span style="color: #FF00FF;">&#40;</span>finished<span style="color: #000033;">,</span> started<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> seconds
<span style="color: #990099; font-weight: bold;">FROM</span> test_session<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>Happy Benchmarking!</p>
<p><map name='google_ad_map_1384_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1384?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_1384_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1384&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fmysql-storage-engine-benchmarking%2F1384' title="MySQL Storage engine benchmarking" alt=" MySQL Storage engine benchmarking" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384">MySQL Storage engine benchmarking</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-storage-engine-benchmarking/1384/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Synchronize Mysql slave tables with mk-table-sync</title>
		<link>http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270</link>
		<comments>http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270#comments</comments>
		<pubDate>Tue, 21 Jun 2011 19:40:56 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[maatkit]]></category>
		<category><![CDATA[slaves]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270</guid>
		<description><![CDATA[I&#8217;ve been meaning to check out Maatkit for a while now. Today I had a reason to as one of our MySQL slaves got out of sync with the master. I&#8217;d heard about mk-table-sync, a tool that synchronizes tables, so I thought I&#8217;d give it a shot. As it turns out it&#8217;s this easy; mk-table-sync [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270">Synchronize Mysql slave tables with mk-table-sync</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been meaning to check out <a title="Maatkit MySQL tools" href="http://www.maatkit.org/" target="_blank">Maatkit</a> for a while now. Today I had a reason to as one of our MySQL slaves got out of sync with the master. I&#8217;d heard about <a href="http://www.maatkit.org/doc/mk-table-sync.html" target="_blank">mk-table-sync</a>, a tool that synchronizes tables, so I thought I&#8217;d give it a shot.</p>
<p>As it turns out it&#8217;s this easy;</p>
<pre>mk-table-sync --execute h=slave_server,u=username,p=secret,D=database,t=table_to_sync --sync-to-master</pre>
<p>I was a little worried about how this would pan out as I assumed it would change data on the slave. Cleverly, the Maatkit developers have thought of this;</p>
<p><em>&quot;When synchronizing a server that is a replication slave with the &#8211;replicate or &#8211;sync-to-master methods, it <strong>always</strong> makes the changes on the replication master, <strong>never</strong> the replication slave directly.&quot; </em><a href="http://www.maatkit.org/doc/mk-table-sync.html" target="_blank">ref</a></p>
<p>So in theory this should alleviate some of those replication pains. Happy days!</p>
<p><map name='google_ad_map_1270_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1270?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_1270_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1270&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fsynchronize-mysql-slave-tables-with-mk-table-sync%2F1270' title="Synchronize Mysql slave tables with mk table sync" alt=" Synchronize Mysql slave tables with mk table sync" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270">Synchronize Mysql slave tables with mk-table-sync</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/synchronize-mysql-slave-tables-with-mk-table-sync/1270/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Backing up the structure of MySQL databases</title>
		<link>http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063</link>
		<comments>http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063#comments</comments>
		<pubDate>Tue, 25 Jan 2011 21:00:20 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[mysqldump]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063</guid>
		<description><![CDATA[Today I wanted a quick and easy way to generate a backup of the structure of all MySQL databases in one easy hit. Here&#8217;s a couple of ways you can do this with the tools you&#8217;re likely to find everywhere. Firstly, we can use the following query to generate a list of mysqldump commands. The [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063">Backing up the structure of MySQL databases</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Today I wanted a quick and easy way to generate a backup of the structure of all MySQL databases in one easy hit. Here&#8217;s a couple of ways you can do this with the tools you&#8217;re likely to find everywhere.</p>
<p>Firstly, we can use the following query to generate a list of <a title="mysqldump database backup program" href="http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html" target="_blank">mysqldump</a> commands. The backups generated here pretty much just contain tables, triggers and functions so customise the command to your needs. Just change the output directory from .<strong>/home/rhys</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('p1063code12'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p106312"><td class="code" id="p1063code12"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'mysqldump -d --routines -h mysql<span style="color: #008080; font-weight: bold;">_</span>host -u username -pSecret '</span><span style="color: #000033;">,</span> SCHEMA_NAME<span style="color: #000033;">,</span> <span style="color: #008000;">' &gt; &quot;/home/rhys/Desktop/'</span><span style="color: #000033;">,</span> SCHEMA_NAME<span style="color: #000033;">,</span> <span style="color: #008000;">'.sql&quot;'</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">FROM</span> INFORMATION_SCHEMA.SCHEMATA
<span style="color: #990099; font-weight: bold;">WHERE</span> SCHEMA_NAME <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #990099; font-weight: bold;">IN</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'mysql'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'information<span style="color: #008080; font-weight: bold;">_</span>schema'</span><span style="color: #000033;">,</span> <span style="color: #008000;">'performance<span style="color: #008080; font-weight: bold;">_</span>schema'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>These commands can be copied and then pasted into a terminal to be executed.</p>
<p>Here&#8217;s a pure bash way of achieving the same thing. The script is called on the command line with the MySQL host, user name and password supplied as parameters. Backups are generated in the current users home directory in the format <strong>db_name.sql</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('p1063code13'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p106313"><td class="code" id="p1063code13"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># MySQL details passed on command line</span>
<span style="color: #007800;">HOST</span>=<span style="color: #007800;">$1</span>
<span style="color: #007800;">USER</span>=<span style="color: #007800;">$2</span>;
<span style="color: #007800;">PWD</span>=<span style="color: #007800;">$3</span>;
&nbsp;
<span style="color: #666666; font-style: italic;"># Query to get database excluding a few system ones</span>
<span style="color: #007800;">QUERY</span>=<span style="color: #ff0000;">&quot;SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME NOT IN ('mysql','information_schema','performance_schema')&quot;</span>;
<span style="color: #666666; font-style: italic;"># Run the query and get the results</span>
<span style="color: #007800;">results</span>=<span style="color: #000000; font-weight: bold;">`</span>mysql <span style="color: #660033;">-h</span> <span style="color: #007800;">$HOST</span> <span style="color: #660033;">-u</span> <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> <span style="color: #660033;">-N</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$QUERY</span>&quot;</span><span style="color: #000000; font-weight: bold;">`</span>;
&nbsp;
<span style="color: #666666; font-style: italic;"># Loop through each row</span>
<span style="color: #000000; font-weight: bold;">for</span> db <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #007800;">$results</span>
<span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #007800;">bkp</span>=<span style="color: #ff0000;">&quot;mysqldump -d --routines -h <span style="color: #007800;">$HOST</span> -u <span style="color: #007800;">$USER</span> -p<span style="color: #007800;">$PWD</span> <span style="color: #007800;">$db</span> &gt; '<span style="color: #007800;">$HOME</span>/<span style="color: #007800;">$db</span>.sql'&quot;</span>;
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Backing up <span style="color: #007800;">$db</span>...&quot;</span>;
	<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #007800;">$bkp</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>Save this to a file called <strong>backup_mysql.sh</strong> and don;t forget to do a <strong>chmod + x</strong> to make the script executable. The backup can be executed from the command line with;</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('p1063code14'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p106314"><td class="code" id="p1063code14"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>backup_mysql.sh localhost username password</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/8f94a578fd9d_11B87/mysqldump_backup_databases.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="mysqldump backup databases" border="0" alt="mysqldump backup databases thumb Backing up the structure of MySQL databases" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/8f94a578fd9d_11B87/mysqldump_backup_databases_thumb.png" width="644" height="429" /></a></p>
<p><map name='google_ad_map_1063_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1063?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_1063_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1063&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fbacking-up-the-structure-of-mysql-databases%2F1063' title="Backing up the structure of MySQL databases" alt=" Backing up the structure of MySQL databases" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063">Backing up the structure of MySQL databases</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/backing-up-the-structure-of-mysql-databases/1063/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parse MySQL Slow Logs with mysqlsla</title>
		<link>http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856</link>
		<comments>http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856#comments</comments>
		<pubDate>Tue, 10 Aug 2010 19:36:21 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856</guid>
		<description><![CDATA[Here&#8217;s a bash script that you can use to parse multiple MySQL Slow Query Log files, in one sweep, into something much more understandable. The script uses the handy utility mysqlsla so make sure this is in your path.&#160; mysqlsla parses, filters, analyzes and sorts MySQL slow, general, binary and microslow patched logs in order [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856">Parse MySQL Slow Logs with mysqlsla</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a bash script that you can use to parse multiple <a title="MySQL Slow Query Logs" href="http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html" target="_blank">MySQL Slow Query Log</a> files, in one sweep, into something much more understandable. The script uses the handy utility <a href="http://hackmysql.com/mysqlsla" target="_blank">mysqlsla</a> so make sure this is in your path.&nbsp;<br />
<blockquote>mysqlsla parses, filters, analyzes and sorts MySQL <a href="http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html">slow</a>, <a href="http://dev.mysql.com/doc/refman/5.0/en/query-log.html">general</a>, <a href="http://dev.mysql.com/doc/refman/5.0/en/binary-log.html">binary</a> and <a href="http://www.mysqlperformanceblog.com/2008/04/20/updated-msl-microslow-patch-installation-walk-through/">microslow patched</a> logs in order to create a customizable report of the queries and their meta-property values. Since these reports are customizable, they can be used for human consumption or be fed into other scripts to further analyze the queries. For example, to profile with <a href="http://maatkit.sourceforge.net/doc/mk-query-profiler.html">mk-query-profiler</a> (a script from Baron Schwartz&#8217;s <a href="http://www.maatkit.org/">Maatkit</a>) every unique SELECT statement using database foo from a slow log: <a title="mysqlsla" href="http://hackmysql.com/mysqlsla" target="_blank">source</a></p></blockquote>
<p>Place all your slow logs into a directory. Change the <strong>sl_dir</strong> variable to point at this directory.<strong> </strong>When you execute the script it will create a directory, within your slow logs directory, called reports. This will contain the reports produced by <a title="mysqlsla" href="http://hackmysql.com/mysqlsla" target="_blank">mysqlsla</a>.</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('p856code16'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p85616"><td class="code" id="p856code16"><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;"># Script to process multiple mysql slow logs</span>
<span style="color: #666666; font-style: italic;"># using mysqlsla http://hackmysql.com/mysqlsla</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Directory containing slow logs</span>
<span style="color: #007800;">sl_dir</span>=<span style="color: #ff0000;">&quot;/home/rhys/Desktop/slow_logs&quot;</span>;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span>;
<span style="color: #666666; font-style: italic;">#slow_logs=$(ls &quot;$sl_dir&quot;);</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Folder for reports</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>reports <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>reports;
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># process each slow log file</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$sl_dir</span>&quot;</span><span style="color: #000000; font-weight: bold;">/*</span>
<span style="color: #000000; font-weight: bold;">do</span>
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Processing file: <span style="color: #007800;">$file</span>&quot;</span>;
                <span style="color: #007800;">filename</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">basename</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
                mysqlsla <span style="color: #660033;">-lt</span> slow <span style="color: #ff0000;">&quot;<span style="color: #007800;">$file</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #ff0000;">&quot;reports/<span style="color: #007800;">$filename</span>.rpt&quot;</span>;
                <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Finished processing file: <span style="color: #007800;">$file</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span></pre></td></tr></table></div>

<p>The reports produced are much easier to work with than the raw mysql logs so this should be a good time saver when optimising those queries!</p>
<p><map name='google_ad_map_856_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/856?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_856_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=856&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fparse-mysql-slow-logs-with-mysqlsla%2F856' title="Parse MySQL Slow Logs with mysqlsla" alt=" Parse MySQL Slow Logs with mysqlsla" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856">Parse MySQL Slow Logs with mysqlsla</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/parse-mysql-slow-logs-with-mysqlsla/856/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('p815code19'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81519"><td class="code" id="p815code19"><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('p815code20'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81520"><td class="code" id="p815code20"><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>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/for-range-partitions-each-partition-must-be-defined/815" size="standard" count="true"></div></div>]]></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>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/error-dropping-database-cant-rmdir-database-errno-39/801/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Unsigned Integer Arithmetic in SQL</title>
		<link>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794</link>
		<comments>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794#comments</comments>
		<pubDate>Sat, 12 Jun 2010 16:45:27 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[integers]]></category>
		<category><![CDATA[unsigned]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794</guid>
		<description><![CDATA[Not the sexiest blog title in the world but I thought I’d knock up a little post on the behaviour of MySQL and SQL Server with integer subtraction. How would you expect a database system to behave with positive and negative data types? Microsoft SQL Server doesn’t really have unsigned data types. All integer types [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794">Unsigned Integer Arithmetic in SQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Not the sexiest blog title in the world but I thought I’d knock up a little post on the behaviour of MySQL and SQL Server with integer subtraction. How would you expect a database system to behave with positive and negative data types? Microsoft SQL Server doesn’t really have unsigned data types. All integer types can be positive and negative with the exception of <a href="http://msdn.microsoft.com/en-us/library/ms187745.aspx" target="_blank">TINYINT</a>. MySQL implements the concept of <a href="http://en.wikipedia.org/wiki/Signedness" target="_blank">signedness</a> so we can specify that TINYINT ranges from –128 to 127 or 0 to 255.</p>
<p>What would you expect SQL Server to do with this?</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('p794code24'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79424"><td class="code" id="p794code24"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @num1 <span style="color: #0000FF;">TINYINT</span> <span style="color: #808080;">=</span> <span style="color: #000;">50</span>, @num2 <span style="color: #0000FF;">TINYINT</span> <span style="color: #808080;">=</span> <span style="color: #000;">75</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @num1 <span style="color: #808080;">-</span> @num2;</pre></td></tr></table></div>

<p>Well it errors. Good RDBMS! </p>
<pre>Msg 8115, Level 16, State 2, Line 3
Arithmetic overflow error converting expression to data type tinyint.</pre>
<p>What does MySQL do? Lets see.</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('p794code25'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79425"><td class="code" id="p794code25"><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;">TABLE</span> nums
<span style="color: #FF00FF;">&#40;</span>
	num1 <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	num2 <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</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: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> nums
<span style="color: #FF00FF;">&#40;</span>
	num1<span style="color: #000033;">,</span>
	num2
<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;">50</span><span style="color: #000033;">,</span>
	<span style="color: #008080;">75</span>
<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> num1 <span style="color: #CC0099;">-</span> num2
<span style="color: #990099; font-weight: bold;">FROM</span> nums<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_unsigned_division.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mysql unsigned division" border="0" alt="mysql unsigned division thumb Unsigned Integer Arithmetic in SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_unsigned_division_thumb.png" width="644" height="216" /></a> </p>
<p>Oh dear not a pretty behaviour! Bad RDBMS! A colleague said to me “you need to set <a href="http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html" target="_blank">sql_mode</a> to traditional”. Mmmmmm, I thought I was running in traditional mode already. As it turns out we need to set the NO_UNSIGNED_SUBTRACTION option.</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('p794code26'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p79426"><td class="code" id="p794code26"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> @@<span style="color: #990099; font-weight: bold;">SESSION</span>.sql_mode <span style="color: #CC0099;">=</span> <span style="color: #008000;">'TRADITIONAL,NO<span style="color: #008080; font-weight: bold;">_</span>UNSIGNED<span style="color: #008080; font-weight: bold;">_</span>SUBTRACTION'</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> num1 <span style="color: #CC0099;">-</span> num2
<span style="color: #990099; font-weight: bold;">FROM</span> nums<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_correct_unsigned_division.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="mysql correct unsigned division" border="0" alt="mysql correct unsigned division thumb Unsigned Integer Arithmetic in SQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/06/mysql_correct_unsigned_division_thumb.png" width="644" height="219" /></a> </p>
<p>Got to love MySQL for keeping you on your toes!</p>
<p><map name='google_ad_map_794_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/794?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_794_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=794&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Funsigned-integer-arithmetic-in-sql%2F794' title="Unsigned Integer Arithmetic in SQL" alt=" Unsigned Integer Arithmetic in SQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794">Unsigned Integer Arithmetic in SQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/unsigned-integer-arithmetic-in-sql/794/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('p788code28'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p78828"><td class="code" id="p788code28"><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>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-alter-procedure-syntax/788/feed</wfw:commentRss>
		<slash:comments>2</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>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/mysql-client-ran-memory/773/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting out database objects with PHP</title>
		<link>http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755</link>
		<comments>http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755#comments</comments>
		<pubDate>Sun, 25 Apr 2010 14:22:44 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755</guid>
		<description><![CDATA[I&#8217;ve recently needed to script out the create sql for various MySQL database objects. No Powershell or SMO to help with this so I&#8217;ve quickly rolled a PHP script to get this done. This will script out all tables, views, triggers, stored procedures and functions from the specified database. One file per object in your [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755">Scripting out database objects with PHP</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently needed to script out the create sql for various MySQL database objects. No <a href="http://en.wikipedia.org/wiki/Windows_PowerShell">Powershell</a> or <a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx">SMO</a> to help with this so I&#8217;ve quickly rolled a <a href="http://php.net">PHP</a> script to get this done.</p>
<pre lang="PHP" xml:lang="PHP">
</pre>
<p>This will script out all tables, views, triggers, stored procedures and functions from the specified database. One file per object in your /tmp directory (you&#8217;ll need to change this if you&#8217;re running on Windows). Just change the $source_host, $source_db, $source_user and $source_pwd variables to point at the database you want to script out.</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('p755code30'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75530"><td class="code" id="p755code30"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set source and connection variables</span>
<span style="color: #000088;">$source_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$source_db</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xxxx&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$source_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xxxx&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$source_pwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;xxxx&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">///////////////////////////////////////////////</span>
<span style="color: #666666; font-style: italic;">// A few helper functions</span>
<span style="color: #666666; font-style: italic;">///////////////////////////////////////////////</span>
<span style="color: #000000; font-weight: bold;">function</span> getMySQLConnection<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pwd</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pwd</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$conn</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// returns a list of tables</span>
<span style="color: #000000; font-weight: bold;">function</span> getTables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$tables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$tables</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// returns a list of views</span>
<span style="color: #000000; font-weight: bold;">function</span> getViews<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$views</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'VIEW' AND TABLE_SCHEMA = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$views</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$views</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// returns a list of procs &amp; functions</span>
<span style="color: #000000; font-weight: bold;">function</span> getRoutines<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$routines</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES WHERE TABLE_SCHEMA = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$routines</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$routines</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// returns a list of triggers</span>
<span style="color: #000000; font-weight: bold;">function</span> getTriggers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$triggers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS WHERE TABLE_SCHEMA = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$database</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$triggers</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$triggers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$triggers</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// writes text files</span>
<span style="color: #000000; font-weight: bold;">function</span> writeTextFile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$contents</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$writer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to open file&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$writer</span><span style="color: #339933;">,</span> <span style="color: #000088;">$contents</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$writer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Gets the sql used to create table</span>
<span style="color: #000000; font-weight: bold;">function</span> getTableCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW CREATE TABLE `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// returns the sql used to create a routine</span>
<span style="color: #000000; font-weight: bold;">function</span> getRoutineCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$routine</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW CREATE PROCEDURE `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$routine</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// returns the sql used to create a trigger</span>
<span style="color: #000000; font-weight: bold;">function</span> getTriggerCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$trigger</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW CREATE TRIGGER `&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$trigger</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;`&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$connection</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/////////////////////////////////////////////////////////</span>
<span style="color: #666666; font-style: italic;">// EOF FUNCTIONS</span>
<span style="color: #666666; font-style: italic;">/////////////////////////////////////////////////////////</span>
&nbsp;
<span style="color: #000088;">$source_connection</span> <span style="color: #339933;">=</span> getMySQLConnection<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_pwd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tables</span> <span style="color: #339933;">=</span> getTables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get the create sql for each table</span>
<span style="color: #666666; font-style: italic;">// writing each one to a text file</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$create_sql</span> <span style="color: #339933;">=</span> getTableCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/tmp/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.sql&quot;</span><span style="color: #339933;">;</span>
    writeTextFile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$create_sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Generated create table sql for &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$table</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$views</span> <span style="color: #339933;">=</span> getViews<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the sql for each view</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$views</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$create_sql</span> <span style="color: #339933;">=</span> getTableCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/tmp/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$view</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.sql&quot;</span><span style="color: #339933;">;</span>
    writeTextFile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$create_sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Generated create view sql for &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$view</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$routines</span> <span style="color: #339933;">=</span> getRoutines<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// get the create sql for each routine</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$routines</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$routine</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$create_sql</span> <span style="color: #339933;">=</span> getRoutineCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$routine</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/tmp/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$routine</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.sql&quot;</span><span style="color: #339933;">;</span>
    writeTextFile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$create_sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Generated create routine sql for &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$routine</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$triggers</span> <span style="color: #339933;">=</span> getTriggers<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$source_db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// get the create sql for each trigger</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$triggers</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$trigger</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$create_sql</span> <span style="color: #339933;">=</span> getTriggerCreate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$source_connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$trigger</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/tmp/trg_&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$trigger</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.sql&quot;</span><span style="color: #339933;">;</span>
    writeTextFile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$create_sql</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Generated create trigger sql for &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$trigger</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><img src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/run_php_create_sql_script.gif" width="480" height="348" alt="run php create sql script Scripting out database objects with PHP"  title="Scripting out database objects with PHP" /><br />
<img src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/sql_files.gif" width="480" height="348" alt="sql files Scripting out database objects with PHP"  title="Scripting out database objects with PHP" /><br />
<img src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2010/04/generated_sql_for_store_table.gif" width="480" height="348" alt="generated sql for store table Scripting out database objects with PHP"  title="Scripting out database objects with PHP" /></p>
<p><map name='google_ad_map_755_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/755?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_755_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=755&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fscripting-out-database-objects-with-php%2F755' title="Scripting out database objects with PHP" alt=" Scripting out database objects with PHP" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755">Scripting out database objects with PHP</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/scripting-out-database-objects-with-php/755/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

