<?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>code-diesel &#187; mysql</title>
	<atom:link href="http://www.codediesel.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How to check when a MySQL table was last updated</title>
		<link>http://www.codediesel.com/mysql/how-to-check-when-a-mysql-table-was-last-updated/</link>
		<comments>http://www.codediesel.com/mysql/how-to-check-when-a-mysql-table-was-last-updated/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 05:10:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2926</guid>
		<description><![CDATA[I recently had to update a MySQL schema and import new data into the table. But before I could do that I needed to check that no one had updated the table during the last 7 days and no new data had been stored. As the table itself did not have any update field of [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to update a MySQL schema and import new data into the table. But before I could do that I needed to check that no one had updated the table during the last 7 days and no new data had been stored. As the table itself did not have any update field of itself the only other option was to look into the MySQL &#8216;information_schema&#8217; database.</p>
<p>The &#8216;information_schema&#8217; database contains a &#8216;tables&#8217; table which contain the update information for each database and its tables. So all you have to do is grab that information.<br />
<span id="more-2926"></span><br />
The following small SQL script list all the databases and its tables that where updated within the last 7 days. You can change the interval as per your requirement.</p>

<div class="wp_codebox"><table><tr id="p29264"><td class="code" id="p2926code4"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> information_schema<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #990099; font-weight: bold;">DISTINCT</span> TABLE_SCHEMA <span style="color: #000033;">,</span> TABLE_NAME 
<span style="color: #990099; font-weight: bold;">FROM</span> 
<span style="color: #990099; font-weight: bold;">TABLES</span> 
<span style="color: #990099; font-weight: bold;">WHERE</span> UPDATE_TIME <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span> 
     UPDATE_TIME <span style="color: #CC0099;">&gt;</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">-</span> <span style="color: #CC0099; font-weight: bold;">INTERVAL</span> <span style="color: #008080;">7</span> <span style="color: #9900FF; font-weight: bold;">DAY</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span> 
     TABLE_SCHEMA  <span style="color: #CC0099;">&lt;&gt;</span> <span style="color: #008000;">'information<span style="color: #008080; font-weight: bold;">_</span>schema'</span></pre></td></tr></table></div>

<p>If you want rather to check if a particular table was modified during a specific period you can use the following. For example if we want to check if the table &#8216;prices&#8217; from the database &#8216;store&#8217; had been modified during the last 7 days.</p>

<div class="wp_codebox"><table><tr id="p29265"><td class="code" id="p2926code5"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> information_schema<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> TABLE_SCHEMA <span style="color: #000033;">,</span> TABLE_NAME 
<span style="color: #990099; font-weight: bold;">FROM</span> 
<span style="color: #990099; font-weight: bold;">TABLES</span> 
<span style="color: #990099; font-weight: bold;">WHERE</span> UPDATE_TIME <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span> 
     UPDATE_TIME <span style="color: #CC0099;">&gt;</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">-</span> <span style="color: #CC0099; font-weight: bold;">INTERVAL</span> <span style="color: #008080;">7</span> <span style="color: #9900FF; font-weight: bold;">DAY</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span> 
     TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #008000;">'store'</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span>
     TABLE_NAME <span style="color: #CC0099;">=</span> <span style="color: #008000;">'prices'</span></pre></td></tr></table></div>

<p>Another example : to check if some table from the &#8216;store&#8217; database has been modified during the last 3 hours.</p>

<div class="wp_codebox"><table><tr id="p29266"><td class="code" id="p2926code6"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">USE</span> information_schema<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> TABLE_SCHEMA <span style="color: #000033;">,</span> TABLE_NAME 
<span style="color: #990099; font-weight: bold;">FROM</span> 
<span style="color: #990099; font-weight: bold;">TABLES</span> 
<span style="color: #990099; font-weight: bold;">WHERE</span> UPDATE_TIME <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span> 
     UPDATE_TIME <span style="color: #CC0099;">&gt;</span> <span style="color: #000099;">NOW</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">-</span> <span style="color: #CC0099; font-weight: bold;">INTERVAL</span> <span style="color: #008080;">3</span> <span style="color: #9900FF; font-weight: bold;">HOUR</span> 
   <span style="color: #CC0099; font-weight: bold;">AND</span> 
     TABLE_SCHEMA <span style="color: #CC0099;">=</span> <span style="color: #008000;">'store'</span></pre></td></tr></table></div>

<p>Note that as far as I know update_time is available for MyISAM tables while InnoDB table stores the update_time field as null. Another thing I found is that update_time is not consistently updated on Windows so I would not trust that value, but it works on Linux systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/how-to-check-when-a-mysql-table-was-last-updated/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Splitting large MySQL dump files</title>
		<link>http://www.codediesel.com/php/splitting-large-mysql-dump-files/</link>
		<comments>http://www.codediesel.com/php/splitting-large-mysql-dump-files/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 11:48:09 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[data]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2922</guid>
		<description><![CDATA[One of the frustrating things with working with MySQL is of importing large sql dump files. Either you get a &#8216;max execution time exceeded&#8217; error from PHP or a &#8216;Max_allowed_packet_size&#8217; from MySQL. In a recent task I needed to import a table of around a million records on a remote host, which quickly became an [...]]]></description>
			<content:encoded><![CDATA[<p>One of the frustrating things with working with MySQL is of importing large sql dump files. Either you get a &#8216;max execution time exceeded&#8217; error from PHP or a &#8216;Max_allowed_packet_size&#8217; from MySQL. In a recent task I needed to import a table of around a million records on a remote host, which quickly became an exercise in frustration due to various limitations on the server. SSH was of no help as changing the configuration files was restricted to the root user.</p>
<p>My last resort was to split the huge &#8216;INSERT&#8217; statements into smaller size files. Manually doing the same is obviously time consuming and error prone; the only other solution is to write a small script to split the insert statements. This was to be a quick hack so the parsing code was to be of minimum complexity. Splitting a sql dump containing extended insert statements is somewhat complex so you need to have the dump file in a simple format &#8211; each insert statement should be on its own line as shown below.<br />
<span id="more-2922"></span></p>

<div class="wp_codebox"><table><tr id="p292211"><td class="code" id="p2922code11"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`dt<span style="color: #008080; font-weight: bold;">_</span>codes`</span> <span style="color: #990099; font-weight: bold;">VALUES</span> ...
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`dt<span style="color: #008080; font-weight: bold;">_</span>codes`</span> <span style="color: #990099; font-weight: bold;">VALUES</span> ...
<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`dt<span style="color: #008080; font-weight: bold;">_</span>codes`</span> <span style="color: #990099; font-weight: bold;">VALUES</span> ...
.
.</pre></td></tr></table></div>

<p>You can create the initial dump file in a simple format using the following command or you can do it using phpMyAdmin.</p>

<div class="wp_codebox"><table><tr id="p292212"><td class="code" id="p2922code12"><pre class="mysql" style="font-family:monospace;">mysqldump <span style="color: #CC0099;">-</span>uUSER <span style="color: #CC0099;">-</span>pPASS <span style="color: #CC0099;">--</span><span style="color: #990099; font-weight: bold;">databases</span> DATABASE_NAME <span style="color: #CC0099;">--</span><span style="color: #990099; font-weight: bold;">tables</span> TABLE_NAME \
<span style="color: #CC0099;">--</span>extended<span style="color: #CC0099;">-</span><span style="color: #990099; font-weight: bold;">insert</span> <span style="color: #CC0099;">=</span> <span style="color: #9900FF; font-weight: bold;">FALSE</span> <span style="color: #CC0099;">&gt;</span> dump.sql</pre></td></tr></table></div>

<p>Once you have the big dump file you can use the following PHP script for splitting the file into smaller chunks. Change the variable &#8216;$max_lines_per_split&#8217; to whatever value you think will work on your system.</p>

<div class="wp_codebox"><table><tr id="p292213"><td class="code" id="p2922code13"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">600</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Number of 'insert' statements per file */</span>
<span style="color: #000088;">$max_lines_per_split</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50000</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$dump_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dump.sql&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$split_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dump-split-<span style="color: #009933; font-weight: bold;">%d</span>.sql&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dump_directory</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;./sql-dump/&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$line_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$file_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total_lines</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dump_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* Only read 'insert' statements */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/insert/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$buffer</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$line</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$line_count</span><span style="color: #339933;">++;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Copy buffer to the split file */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line_count</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$max_lines_per_split</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dump_directory</span> <span style="color: #339933;">.</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$split_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file_count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$out_write</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out_write</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</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;">$out_write</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$line_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$file_count</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$buffer</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* Write out the remaining buffer */</span>
        <span style="color: #000088;">$file_name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dump_directory</span> <span style="color: #339933;">.</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$split_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file_count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$out_write</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$out_write</span><span style="color: #339933;">,</span> <span style="color: #000088;">$buffer</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;">$out_write</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;done.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once the dump file has been split into smaller ones, you can gzip them to reduce the size further.</p>

<div class="wp_codebox"><table><tr id="p292214"><td class="code" id="p2922code14"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">gzip</span> dump-split-<span style="color: #000000; font-weight: bold;">*</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/splitting-large-mysql-dump-files/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Migrating Access MDB to MySQL</title>
		<link>http://www.codediesel.com/data/migrating-access-mdb-to-mysql/</link>
		<comments>http://www.codediesel.com/data/migrating-access-mdb-to-mysql/#comments</comments>
		<pubDate>Mon, 26 Sep 2011 09:46:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2917</guid>
		<description><![CDATA[A recent task needed me to import a large amount of data from a Access MDB database to MySQL. My first choice for the job was the mdb-tools set of utilities. mdb-tools provides a set of tools and applications to export MDB data and schema to other databases such as MySQL, Oracle, Sybase, PostgreSQL, and [...]]]></description>
			<content:encoded><![CDATA[<p>A recent task needed me to import a large amount of data from a Access MDB database to MySQL. My first choice for the job was the <a href="http://mdbtools.sourceforge.net/" rel="nofollow"  title="mdb-tools" target="_blank">mdb-tools</a> set of utilities. mdb-tools provides a set of tools and applications to export MDB data and schema to other databases such as MySQL, Oracle, Sybase, PostgreSQL, and others. </p>
<p>mdb-tools is available for Linux systems, and as I use Ubuntu, installation was a breeze using the package manager. To install is from the shell use the following.<br />
<span id="more-2917"></span></p>
<h4>Installing mdbtools</h4>

<div class="wp_codebox"><table><tr id="p291724"><td class="code" id="p2917code24"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> mdbtools</pre></td></tr></table></div>

<p>The following command lines tools are available for migrating a MDB to a different database.</p>
<table class="posts_table">
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top">mdb-tables</td>
<td align="left" valign="top">list tables in the specified file</td>
</tr>
<tr>
<td align="left" valign="top">mdb-schema</td>
<td align="left" valign="top">generate schema DDL for the specified file</td>
</tr>
<tr>
<td align="left" valign="top">mdb-export</td>
<td align="left" valign="top">generate CSV style output for a table</td>
</tr>
</tbody>
</table>
<p>Just to check if the correct tables are in the Access database, we will use the &#8216;mdb-tables&#8217; command which will list the table names in the database.</p>

<div class="wp_codebox"><table><tr id="p291725"><td class="code" id="p2917code25"><pre class="bash" style="font-family:monospace;">mdb-tables database.mdb</pre></td></tr></table></div>

<h4>Exporting schema</h4>
<p>Once we are satisfied that the appropriate tables are there we can generate a schema to be used with MySQL. For this we will use the &#8216;mdb-schema&#8217; command. mdb-schema produces data definition language output for the given database. This can be further passed to another database engine (MySQL, Oracle, Sybase, PostgreSQL etc) to create a replica of the original access table format.</p>

<div class="wp_codebox"><table><tr id="p291726"><td class="code" id="p2917code26"><pre class="bash" style="font-family:monospace;">mdb-schema database.mdb  <span style="color: #000000; font-weight: bold;">&gt;</span> schema.sql</pre></td></tr></table></div>

<p>The above will generate the schema for all the tables in the &#8216;database.mdb&#8217; database and save it to the schema.sql file. If you want to output the schema for a particular table rather than all the tables, you will need to use the &#8216;-T&#8217; flag. The &#8216;-T&#8217; flag will restrict the schema for a particular table.</p>

<div class="wp_codebox"><table><tr id="p291727"><td class="code" id="p2917code27"><pre class="bash" style="font-family:monospace;">mdb-schema <span style="color: #660033;">-T</span> table_name database.mdb <span style="color: #000000; font-weight: bold;">&gt;</span> schema.sql</pre></td></tr></table></div>

<p>Rather than saving the schema to a file we can directly import it into MySQL. The following will directly create a MySQL table using the schema from the MDB database by piping the output of mdb-schema to MySQL.</p>

<div class="wp_codebox"><table><tr id="p291728"><td class="code" id="p2917code28"><pre class="bash" style="font-family:monospace;">mdb-schema database.mdb <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-u</span> username <span style="color: #660033;">-p</span> database_name</pre></td></tr></table></div>

<h4>Exporting data</h4>
<p>mdb-tools lets you export the data in two formats &#8211; CSV or SQL. To export the data in a CSV format use the following command. Note that you need to specify the table name from which the data will be exported.</p>

<div class="wp_codebox"><table><tr id="p291729"><td class="code" id="p2917code29"><pre class="bash" style="font-family:monospace;">mdb-export database.mdb table_name <span style="color: #000000; font-weight: bold;">&gt;</span> export.csv</pre></td></tr></table></div>

<p>You can export the data as SQL &#8216;INSERT&#8217; statements you need to use the &#8216;-I&#8217; flag.</p>

<div class="wp_codebox"><table><tr id="p291730"><td class="code" id="p2917code30"><pre class="bash" style="font-family:monospace;">mdb-export <span style="color: #660033;">-I</span> database.mdb table_name <span style="color: #000000; font-weight: bold;">&gt;</span> export.sql</pre></td></tr></table></div>

<p>But there is one problem here. All the export statements end with a newline and not a semi-colon as MySQL expects, so we need to add one. The following line shows how with the help of the &#8216;-R&#8217; flag. This flag lets you specify a row delimiter.</p>

<div class="wp_codebox"><table><tr id="p291731"><td class="code" id="p2917code31"><pre class="bash" style="font-family:monospace;">mdb-export <span style="color: #660033;">-I</span> <span style="color: #660033;">-R</span> <span style="color: #ff0000;">';'</span> database.mdb table_name <span style="color: #000000; font-weight: bold;">&gt;</span> export.sql</pre></td></tr></table></div>

<p>This will now end all INSERT statements with a semi-colon instead of a newline. This is fine for small data, but for a huge number of INSERT statements this can be a problem as all the statements are now on one single line ( the newlines are replaced by the semi-colon). To solve this problem we will need to use &#8216;sed&#8217; to insert the semi-color before the ending newline and the closing parenthesis of the INSERT statement. This is shown below.</p>

<div class="wp_codebox"><table><tr id="p291732"><td class="code" id="p2917code32"><pre class="bash" style="font-family:monospace;">mdb-export <span style="color: #660033;">-I</span> database.mdb table_name <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/)$/)\;/'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> export.sql</pre></td></tr></table></div>

<h4>Using Toad for MySQL to import CSV</h4>
<p>For my data import job I needed to import a select few columns from a particular table to MySQL. mdb-export does not allow me to select columns from a table, so I needed to find another way to import the data to MySQL. As I already had <a href="http://toadformysql.com/index.jspa" rel="nofollow"  title="Toad for MySQL" target="_blank">Toad MySQL</a> installed I decided to use it.</p>
<p>First I exported the data from the MDB table to a CSV file and then used the Toad MySQL importer to import particular columns from the CSV file to my new MySQL database. The Toad importer lets you map columns from the CSV file to the MySQL schema as shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/09/toad-mysql-import.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/09/toad-mysql-import-300x201.gif" alt="" title="toad-mysql-import" width="300" height="201" class="aligncenter size-medium wp-image-2919" /></a><br />
<em>Click to enlarge</em></p>
<p>For large files the Toad importer can take a while to complete. If you have a Toad version prior to 6.0 then upgrade it to the latest version. On my previous version of 5.x the importer used to crash before writing the imported data to the disk; the bug seems to have been resolved in the new version.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/data/migrating-access-mdb-to-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatically create PHP classes from MySQL</title>
		<link>http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/</link>
		<comments>http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 06:37:54 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2912</guid>
		<description><![CDATA[Creating a database driven web application involves commonly used paradigms for data modification, which we commonly refer to as CRUD. Frameworks provides nice ORM wrappers to help the programmer. But for small projects frameworks can be an overkill. Still the programmer needs to design the basic database CRUD functionality, which can be quite tedious and [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a database driven web application involves commonly used paradigms for data modification, which we commonly refer to as CRUD. Frameworks provides nice ORM wrappers to help the programmer. But for small projects frameworks can be an overkill. Still the programmer needs to design the basic database CRUD functionality, which can be quite tedious and repetitive. This is where auto database class generators can be helpful. If you have your database schema ready, you can automatically create the respective class wrappers for the tables.<br />
<span id="more-2912"></span><br />
I found two libraries which accomplish the task without much of an overhead.<br />
<a href="http://www.structy.com/home.html" rel="nofollow"  title="structy" target="_blank">Structy</a> and <a href="https://github.com/stevenflesch/table2class" rel="nofollow"  title="table2class" target="_blank">table2class</a>.</p>
<p>Take a sample database schema shown below.</p>

<div class="wp_codebox"><table><tr id="p291236"><td class="code" id="p2912code36"><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> <span style="color: #008000;">`user`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`username`</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</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: #990099; font-weight: bold;">default</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`password`</span> <span style="color: #999900; font-weight: bold;">varchar</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</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: #990099; font-weight: bold;">default</span> <span style="color: #008000;">''</span><span style="color: #000033;">,</span>
  <span style="color: #990099; font-weight: bold;">KEY</span> <span style="color: #008000;">`username`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`username`</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span><span style="color: #CC0099;">=</span>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>latin1<span style="color: #000033;">;</span></pre></td></tr></table></div>

<p>The following is a php class automatically generated by &#8216;table2class&#8217; from the above schema. The generated class file also includes additional support database initialization files. Note that some functions here may need modification to suit your particular application, but still, the major code required to get the application up and running is there. Any automation is better than nothing. And each such help to the programmer means faster project delivery time for the client.</p>

<div class="wp_codebox"><table><tr id="p291237"><td class="code" id="p2912code37"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/*************************************************************************
* Class Name:       user
* File Name:        class.user.php
* Generated:        Thursday, Sep 15, 2011 - 6:39:33 UTC
*  - for Table:     user
*   - in Database:  user
**************************************************************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Files required by class:</span>
<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class.database.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Begin Class &quot;user&quot;</span>
<span style="color: #000000; font-weight: bold;">class</span> user <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">// Variable declaration</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// Primary Key</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$database</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Class Constructor</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SetSettings</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// Class Destructor</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// GET Functions</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getusername<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">// SET Functions</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setusername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mValue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mValue</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> select<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009933; font-style: italic;">// SELECT Function</span>
        <span style="color: #009933; font-style: italic;">// Execute SQL Query to get record.</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM user WHERE username = <span style="color: #006699; font-weight: bold;">$mID</span>;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oRow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oResult</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">// Assign results to class.</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$oRow</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// Primary Key</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insert<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span> <span style="color: #009933; font-style: italic;">// Remove primary key value for insert</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO user () VALUES ();&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastinsertid</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE user SET (username = '<span style="color: #006699; font-weight: bold;">$this-&gt;username</span>') 
                 WHERE username = <span style="color: #006699; font-weight: bold;">$mID</span>;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mID</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$sSQL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE FROM user WHERE username = <span style="color: #006699; font-weight: bold;">$mID</span>;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$oResult</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">database</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sSQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #009933; font-style: italic;">// End Class &quot;user&quot;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>&#8216;Structy&#8217; provides a somewhat different flavor as you can see from the code class generated by the library. This is more complete than &#8216;table2class&#8217; but also a little complex. The &#8216;RET&#8217; constant you see in the code below is generated by the library and is automatically defined in the other support files.</p>

<div class="wp_codebox"><table><tr id="p291238"><td class="code" id="p2912code38"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**********************************************************************
user.class.php
Generated by STRUCTY 2011.09.15 08:33:41.
Copyright 2011 Structy, Frédéric Aebi. All rights reserved.
**********************************************************************/</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;USER&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> user <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setusername<span style="color: #009900;">&#40;</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">=</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setpassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">=</span><span style="color: #000088;">$pArg</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getusername<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getpassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> readObject<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT *&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;FROM &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$record</span> <span style="color: #339933;">=</span> Database<span style="color: #339933;">::</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</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: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</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: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$record</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setusername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setpassword</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> readArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT *&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;FROM &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$recordset</span> <span style="color: #339933;">=</span> Database<span style="color: #339933;">::</span><span style="color: #004000;">select</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$class_objects</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: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recordset</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$recordset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$class_object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> user<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$class_object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setusername</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$class_object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setpassword</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$class_objects</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$class_object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getusername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$class_object</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$class_objects</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$update</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$update</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;UPDATE &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;SET&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getusername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getpassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
    <span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
            Database<span style="color: #339933;">::</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; (&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;username, password&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;) VALUES (&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getusername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getpassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span>
            <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
            Database<span style="color: #339933;">::</span><span style="color: #004000;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> delete<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$qry</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DELETE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;FROM &quot;</span><span style="color: #339933;">.</span>USER<span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;WHERE&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;username = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$qry</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$and</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;password = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
            <span style="color: #000088;">$and</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;AND&quot;</span><span style="color: #339933;">.</span>RET<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        Database<span style="color: #339933;">::</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$qry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/automatically-create-php-classes-from-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating SQL schemas with Doctrine DBAL</title>
		<link>http://www.codediesel.com/mysql/creating-sql-schemas-with-doctrine-dbal/</link>
		<comments>http://www.codediesel.com/mysql/creating-sql-schemas-with-doctrine-dbal/#comments</comments>
		<pubDate>Mon, 20 Dec 2010 07:34:33 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[schemas]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2773</guid>
		<description><![CDATA[A tedious task during web development is that of database schema creation. A schema containing a few tables comprising of a small set of rows is quick, while that containing dozens of tables and large numbers of columns is a tedious process. I usually resort to a small php script with some regular expression tossed [...]]]></description>
			<content:encoded><![CDATA[<p>A tedious task during web development is that of database schema creation. A schema containing a few tables comprising of a small set of rows is quick, while that containing dozens of tables and large numbers of columns is a tedious process. I usually resort to a small php script with some regular expression tossed in to automatically create a schema from a text file definition. But that is a little buggy as I&#8217;ve to manually add the indexes and other small things. Now that Doctrine has released a DBAL library, this will provide a nice ability to automatically create sql schemas.<br />
<span id="more-2773"></span></p>
<h4>Doctrine DBAL</h4>
<p><a href="http://www.doctrine-project.org/projects" rel="nofollow" target="_blank" >Doctrine DBAL</a> (available as RC4 as of this writing) provides a wonderful abstraction layer to process all the needed database actions. DBAL is a lightweight layer built around a PDO-like API which offers features like database schema introspection, schema migration, database access and manipulation through an OO API. Note that DBAL API can be used independently of the Doctrine ORM API. So you can use the DBAL library even if you are unfamiliar with the ORM API.</p>
<h4>Creating a MySQL schema from DBAL </h4>
<p>In this post I&#8217;ve created a small example script using the DBAL API to generate a MySQL schema for a couple of tables. Note that the DBAL API uses <a href="http://php.net/manual/en/language.namespaces.php" rel="nofollow" target="_blank" >php namespaces</a>, so you will need to be familiar with them to understand the code, which also means that you must be at least using PHP 5.3.0</p>

<div class="wp_codebox"><table><tr id="p277343"><td class="code" id="p2773code43"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> Doctrine\Common\ClassLoader<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Doctrine uses a class loader to autoload the required classes */</span>
<span style="color: #000000; font-weight: bold;">require</span> <span style="color: #0000ff;">'Doctrine/doctrine-dbal/Doctrine/Common/ClassLoader.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Lets first load the Doctrine DBAL lbrary */</span>
<span style="color: #000088;">$classLoader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassLoader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Doctrine'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'./Doctrine/doctrine-dbal/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$classLoader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">register</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Provide DBAL with some initial database infor */</span>
<span style="color: #000088;">$config</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\DBAL\Configuration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$connectionParams</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'dbname'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'user'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'password'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'host'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'driver'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pdo_mysql'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Connect to the database */</span>
<span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> \Doctrine\DBAL\DriverManager<span style="color: #339933;">::</span><span style="color: #004000;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$connectionParams</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* We will now generate a table definition, 
   but first lets get a Schema object .
*/</span>
<span style="color: #000088;">$schema</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\DBAL\Schema\Schema<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Now use the Schema object to create a 'users' table */</span>
<span style="color: #000088;">$usersTable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$schema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Add some columns to the table */</span>
<span style="color: #000088;">$usersTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;integer&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unsigned&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$usersTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;first_name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;length&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$usersTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;last_name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;length&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$usersTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;length&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">256</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$usersTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;website&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;length&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">256</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Add a primary key */</span>
<span style="color: #000088;">$usersTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* Create another table called 'login' and add some columns */</span>
<span style="color: #000088;">$loginTable</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$schema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createTable</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loginTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;integer&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unsigned&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loginTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;length&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loginTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;string&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;length&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">64</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loginTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addUniqueIndex</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loginTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPrimaryKey</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Assign a foreign key constraint to the table */</span>
<span style="color: #000088;">$loginTable</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addForeignKeyConstraint</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usersTable</span><span style="color: #339933;">,</span> 
                        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;onDelete&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;CASCADE&quot;</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set the Schema output platform, as we are using MySQL
   a Mysql schema will be generated. */</span>
<span style="color: #000088;">$platform</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$conn</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDatabasePlatform</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* The 'queries' variable will now hold the 
   an array of sql statements.
*/</span>
<span style="color: #000088;">$queries</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$schema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toSql</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$platform</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will generate the following MySQL statements.</p>

<div class="wp_codebox"><table><tr id="p277344"><td class="code" id="p2773code44"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> users
  <span style="color: #66cc66;">&#40;</span>
     id         INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     first_name VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     last_name  VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     email      VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     website    VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
ENGINE <span style="color: #66cc66;">=</span> INNODB 
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> login
  <span style="color: #66cc66;">&#40;</span>
     id       INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     username VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     PASSWORD VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
     <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> login_username_uniq <span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
     <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
ENGINE <span style="color: #66cc66;">=</span> INNODB 
&nbsp;
<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> login <span style="color: #993333; font-weight: bold;">ADD</span> CONSTRAINT 
login_id_fk <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> users<span style="color: #66cc66;">&#40;</span>
id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DELETE</span> CASCADE</pre></td></tr></table></div>

<h4>Migrating schemas to another platform</h4>
<p>Now that you have generated the script to create sql schemas, you can easily migrate the generated schemas to another platform using a couple of statements. For example we can migrate the above MySQL statements to Oracle using the following code.</p>

<div class="wp_codebox"><table><tr id="p277345"><td class="code" id="p2773code45"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$myPlatform</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \Doctrine\DBAL\Platforms\OraclePlatform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$queries</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$schema</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toSql</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$myPlatform</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<h4>Basic column types</h4>
<p>Below are the basic types that you can use in the &#8216;addColumn&#8217; method when creating tables.</p>

<div class="wp_codebox"><table><tr id="p277346"><td class="code" id="p2773code46"><pre class="text" style="font-family:monospace;">'bigint', 'boolean', 
'datetime', 'date', 'time', 
'decimal', 'integer', 'smallint',
'object', 'string', 'text'.</pre></td></tr></table></div>

<p>In the next few posts we will explore some more features of Doctrine DBAL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/creating-sql-schemas-with-doctrine-dbal/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Increase your MySQL productivity with Toad</title>
		<link>http://www.codediesel.com/mysql/increase-your-mysql-productivity-with-toad/</link>
		<comments>http://www.codediesel.com/mysql/increase-your-mysql-productivity-with-toad/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 06:07:18 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2736</guid>
		<description><![CDATA[Recently my favorite MySQL Swiss Army Knife has to be Toad for MySQL. Not only does it have a plethora of tools and interesting features, it is free. A few days back I’d to compare database schemas of different versions of a application to see if some table fields had changed between versions. Comparing a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2010/10/toad-for-mysql.jpg"><img style="border: 1px solid #c0c0c0;" src="http://www.codediesel.com/wp-content/uploads/2010/10/toad-for-mysql-300x205.jpg" alt="" title="toad-for-mysql" width="300" height="205" class="alignleft size-medium wp-image-2737" /></a>Recently my favorite MySQL Swiss Army Knife has to be <a href="http://www.toadworld.com/DOWNLOADS/Freeware/ToadforMySQLFreeware/tabid/561/Default.aspx" rel="nofollow" target="_blank" >Toad for MySQL</a>. Not only does it have a plethora of tools and interesting features, it is free. A few days back I’d to compare database schemas of different versions of a application to see if some table fields had changed between versions. Comparing a database schema containing above 200 tables can be a time consuming job if you do not have the right tools. Toad makes the work easier with its schema and data compare feature, which lets you easily compare schema and also the data  from two different databases. You can even synchronize the two schemas or the data therein so both the databases contain the same schema and data.</p>
<p>Besides these it has a nice query builder, somewhat like the one in Microsoft Access. Toad also can manage your Amazon EC2 instances with its built-in tool, which I&#8217;ve yet to try. Other than these it has the other regular features &#8211; a database explorer, table builder, database diagram creator etc. Toad is now my favorite tool for managing MySQL data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/increase-your-mysql-productivity-with-toad/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Changing MySQL clients default prompt</title>
		<link>http://www.codediesel.com/mysql/changing-mysql-clients-default-prompt/</link>
		<comments>http://www.codediesel.com/mysql/changing-mysql-clients-default-prompt/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 12:51:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[configuration]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2725</guid>
		<description><![CDATA[Although graphical clients are preferred when accessing MySQL databases, for many users the mysql command line client is the first choice when connecting to the MySQL server. And many times this is the only option available when you are accessing remote machines through SSH. The default mysql prompt is the simple: mysql&#62; Nice and simple, [...]]]></description>
			<content:encoded><![CDATA[<p>Although graphical clients are preferred when accessing MySQL databases, for many users the <em>mysql</em> command line client is the first choice when connecting to the MySQL server. And many times this is the only option available when you are accessing remote machines through SSH.<br />
<span id="more-2725"></span><br />
The default mysql prompt is the simple:</p>

<div class="wp_codebox"><table><tr id="p272552"><td class="code" id="p2725code52"><pre class="text" style="font-family:monospace;">mysql&gt;</pre></td></tr></table></div>

<p>Nice and simple, but if you have multiple mysql client windows open it can be a little harder to know which window is connected to which user and database. Changing the prompt to display the database and user makes it easier to quickly tell which window is connected to which client and database.</p>
<h4>Changing the mysql prompt</h4>
<p>To change the prompt open your MySQL <strong>my.cnf</strong> (or <strong>my.ini</strong> if you are using Windows) and add the following line in the [mysql] section. If the file does not have one, create it by adding the following line.</p>

<div class="wp_codebox"><table><tr id="p272553"><td class="code" id="p2725code53"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #990000;">mysql</span><span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<p>After the above line add the following option.</p>

<div class="wp_codebox"><table><tr id="p272554"><td class="code" id="p2725code54"><pre class="php" style="font-family:monospace;">prompt<span style="color: #339933;">=</span>\u<span style="color: #339933;">@</span>\h<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>\d<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span>\_</pre></td></tr></table></div>

<p>The &#8216;\u&#8217; and &#8216;\h&#8217; display the username and hostname respectively. The &#8216;\d&#8217; displays the default database selected. Restarting the client will display the changed prompt, the following is what it looks like on my machine. &#8216;wordpress&#8217; is my default database.</p>

<div class="wp_codebox"><table><tr id="p272555"><td class="code" id="p2725code55"><pre class="php" style="font-family:monospace;">codediesel<span style="color: #339933;">@</span>localhost<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>wordpress<span style="color: #009900;">&#93;</span><span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>If you do not want to make changes to the <strong>my.ini</strong> file or are unable to do so, you can also change the prompt interactively as shown below.</p>

<div class="wp_codebox"><table><tr id="p272556"><td class="code" id="p2725code56"><pre class="text" style="font-family:monospace;">shell&gt; mysql --prompt=&quot;\u@\h:[\d]&gt;\_&quot; -uUSERNAME -p</pre></td></tr></table></div>

<h4>List of options</h4>
<p>The &#8216;prompt&#8217; parameter supports many options, a complete list is shown in the following table.</p>
<table class="posts_table">
<colgroup>
<col>
<col>
</colgroup>
<tbody>
<tr>
<td><span class="bold"><strong>Option</strong></span></td>
<td><span class="bold"><strong>Description</strong></span></td>
</tr>
<tr>
<td><code class="literal">\c</code></td>
<td>A counter that increments for each statement you issue</td>
</tr>
<tr>
<td><code class="literal">\D</code></td>
<td>The full current date</td>
</tr>
<tr>
<td><code class="literal">\d</code></td>
<td>The default database</td>
</tr>
<tr>
<td><code class="literal">\h</code></td>
<td>The server host</td>
</tr>
<tr>
<td><code class="literal">\l</code></td>
<td>The current delimiter (new in 5.0.25)</td>
</tr>
<tr>
<td><code class="literal">\m</code></td>
<td>Minutes of the current time</td>
</tr>
<tr>
<td><code class="literal">\n</code></td>
<td>A newline character</td>
</tr>
<tr>
<td><code class="literal">\O</code></td>
<td>The current month in three-letter format (Jan, Feb, …)</td>
</tr>
<tr>
<td><code class="literal">\o</code></td>
<td>The current month in numeric format</td>
</tr>
<tr>
<td><code class="literal">\P</code></td>
<td>am/pm</td>
</tr>
<tr>
<td><code class="literal">\p</code></td>
<td>The current TCP/IP port or socket file</td>
</tr>
<tr>
<td><code class="literal">\R</code></td>
<td>The current time, in 24-hour military time (0–23)</td>
</tr>
<tr>
<td><code class="literal">\r</code></td>
<td>The current time, standard 12-hour time (1–12)</td>
</tr>
<tr>
<td><code class="literal">\S</code></td>
<td>Semicolon</td>
</tr>
<tr>
<td><code class="literal">\s</code></td>
<td>Seconds of the current time</td>
</tr>
<tr>
<td><code class="literal">\t</code></td>
<td>A tab character</td>
</tr>
<tr>
<td><code class="literal">\U</code></td>
<td>Your full user_name@host_name account name</td>
</tr>
<tr>
<td><code class="literal">\u</code></td>
<td>Your user name</td>
</tr>
<tr>
<td><code class="literal">\v</code></td>
<td>The server version</td>
</tr>
<tr>
<td><code class="literal">\w</code></td>
<td>The current day of the week in three-letter format (Mon, Tue, …)</td>
</tr>
<tr>
<td><code class="literal">\Y</code></td>
<td>The current year, four digits</td>
</tr>
<tr>
<td><code class="literal">\y</code></td>
<td>The current year, two digits</td>
</tr>
<tr>
<td><code class="literal">\_</code></td>
<td>A space</td>
</tr>
<tr>
<td><code class="literal">\ </code></td>
<td>A space (a space follows the backslash)</td>
</tr>
<tr>
<td><code class="literal">\'</code></td>
<td>Single quote</td>
</tr>
<tr>
<td><code class="literal">\"</code></td>
<td>Double quote</td>
</tr>
<tr>
<td><code class="literal">\\</code></td>
<td>A literal &#8220;<span class="quote"><code class="literal">\</code></span>&#8221; backslash character</td>
</tr>
<tr>
<td><code class="literal">\<em class="replaceable"><code>x</code></em></code></td>
<td>x, for any &#8216;x&#8217; not listed above</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/changing-mysql-clients-default-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sequence gaps in MySQL</title>
		<link>http://www.codediesel.com/mysql/sequence-gaps-in-mysql/</link>
		<comments>http://www.codediesel.com/mysql/sequence-gaps-in-mysql/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 10:36:50 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2697</guid>
		<description><![CDATA[Auto-increment sequences are a common way to define a primary key in MySQL. This types of artificial keys, known as surrogate keys are commonly used by developers to quickly construct a database table. A common reason developers use artificial keys is due to the fact that most do not take the time to search for [...]]]></description>
			<content:encoded><![CDATA[<p>Auto-increment sequences are a common way to define a primary key in MySQL. This types of artificial keys, known as surrogate keys are commonly used by developers to quickly construct a database table. A common reason developers use artificial keys is due to the fact that most do not take the time to search for a natural key in their model, especially SQL newbies. The question of whether we should use a surrogate key or a natural key is a debate we should leave to the database experts. The purpose of this post is to present some queries to find if a auto-increment sequence contains gaps.<br />
<span id="more-2697"></span></p>
<h4>Gaps in auto-increment columns</h4>
<p>In a table with a auto-increment column gaps gradually develop over time, due to repeated deletion and insertion of rows. An example of such a table is shown below. Note the sequence numbers of the &#8216;id&#8217; column which is missing a few numbers. Not that the gaps are harmful in any way (at-least for small schemas), just that a cursory glance at a solution is satisfying and sometimes useful.</p>
<p><strong>Table 1.</strong></p>

<div class="wp_codebox"><table><tr id="p269765"><td class="code" id="p2697code65"><pre class="text" style="font-family:monospace;">+----+-------+------------+
| id | name  | birthdate  |
+----+-------+------------+
|  1 | sam   | 1986-09-10 |
|  3 | grey  | 1979-01-19 |
|  5 | jenny | 1979-12-12 |
|  6 | bill  | 1991-04-27 |
|  7 | tom   | 1995-09-15 |
| 10 | jack  | 1964-02-09 |
+----+-------+------------+</pre></td></tr></table></div>

<p>To find out if a table with a defined auto-increment column, like the above, has gaps in it, we can use the following query. The query returns a single row with the value &#8216;gap&#8217; if there are any gaps in the sequence, or a empty result for a non-gaped sequence.</p>

<div class="wp_codebox"><table><tr id="p269766"><td class="code" id="p2697code66"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span> <span style="color: #ff0000;">'gap'</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`testtable`</span> 
   <span style="color: #993333; font-weight: bold;">HAVING</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;&gt;</span> MAX<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This will return the following result for <strong>Table 1</strong>.</p>

<div class="wp_codebox"><table><tr id="p269767"><td class="code" id="p2697code67"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-----+</span>
<span style="color: #66cc66;">|</span> gap <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-----+</span>
<span style="color: #66cc66;">|</span> gap <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-----+</span></pre></td></tr></table></div>

<p>Note that the above will only work if the sequence starts from the number &#8217;1&#8242;. If you have a auto-increment column that starts with some other number, say 1000, than the following query can be used.</p>

<div class="wp_codebox"><table><tr id="p269768"><td class="code" id="p2697code68"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span> <span style="color: #ff0000;">'gap'</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`testtable`</span> 
    <span style="color: #993333; font-weight: bold;">HAVING</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;&gt;</span> MAX<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">999</span> #Start sequence number <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span></pre></td></tr></table></div>

<h4>How the query works</h4>
<p>The query counts the total number of rows in ‘testtable’ and if the total count is different from the maximum &#8216;id&#8217; (auto-increment column) value than it returns a column with the value ‘gap’. Say the table has 20 rows and the auto-increment column number starts with ‘1’. So if there are no gaps in the sequence than the MAX(id) should be ‘20’, which is equal to COUNT(*).  Now suppose the 20th row is deleted and a new row inserted, then the &#8216;id&#8217; value of the new row will be ‘21’ which will imply that there is a gap in the sequence as the row value sequence jumps from ‘19’ to ‘21’.</p>
<p>The above query works for auto-increment columns that start with &#8217;1&#8242;, for rows that start with some other number use the following query.</p>

<div class="wp_codebox"><table><tr id="p269769"><td class="code" id="p2697code69"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">DISTINCT</span> <span style="color: #ff0000;">'gap'</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">`testtable`</span> 
    <span style="color: #993333; font-weight: bold;">HAVING</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #66cc66;">&#40;</span>MAX<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#40;</span>MIN<span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<h4>Finding missing numbers in a sequence</h4>
<p>Finding if a sequence has gaps is easy, finding the exact list of missing numbers is a little involved. The following query lists the missing numbers from a given auto-increment column.</p>

<div class="wp_codebox"><table><tr id="p269770"><td class="code" id="p2697code70"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> a<span style="color: #66cc66;">.</span>id<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> start<span style="color: #66cc66;">,</span> MIN<span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">.</span>id<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> end
    <span style="color: #993333; font-weight: bold;">FROM</span> testtable <span style="color: #993333; font-weight: bold;">AS</span> a<span style="color: #66cc66;">,</span> testtable <span style="color: #993333; font-weight: bold;">AS</span> b
    <span style="color: #993333; font-weight: bold;">WHERE</span> a<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">&lt;</span> b<span style="color: #66cc66;">.</span>id
    <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> a<span style="color: #66cc66;">.</span>id
    <span style="color: #993333; font-weight: bold;">HAVING</span> start <span style="color: #66cc66;">&lt;</span> MIN<span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">.</span>id<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>For example running the above query on <strong>Table 1.</strong> gives the following result. <em>Note: For a table containing a large number of rows, the above query can take a substantial amount of time.</em></p>

<div class="wp_codebox"><table><tr id="p269771"><td class="code" id="p2697code71"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------+------+</span>
<span style="color: #66cc66;">|</span> start <span style="color: #66cc66;">|</span> end  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------+------+</span>
<span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>     <span style="color: #cc66cc;">8</span> <span style="color: #66cc66;">|</span>    <span style="color: #cc66cc;">9</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">-------+------+</span></pre></td></tr></table></div>

<h4>Visualizing sequence gaps</h4>
<p>A nice way to represent the missing values in a sequence is through the visual medium. The following is a 1-dimensional graph of the sequence numbers in a auto-increment column. The black areas represent the missing numbers. As you can easily see there are large sections of unused numbers in the sequence. To create the following graph I used the &#8216;wp_comments&#8217; table from a WordPress database and the wonderful <a href="http://processing.org/" rel="nofollow" target="_blank" >Processing</a> language for drawing the graphs.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2010/09/sequence_gap.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/09/sequence_gap.gif" alt="" title="sequence_gap" width="500" height="48" class="aligncenter size-full wp-image-2699" /></a></p>
<p>For the curious the code is shown below. As the Processing code needs to connect to the MySQL database, you will also need to download the MySQL library for Processing from <a href="http://bezier.de/processing/libs/sql/" rel="nofollow" target="_blank" >here</a>.</p>

<div class="wp_codebox"><table><tr id="p269772"><td class="code" id="p2697code72"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.bezier.data.sql.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">String</span> user     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_db_username&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">String</span> pass     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your_db_password&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">String</span> database <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  size<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span>, <span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  background<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">206</span>,<span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  stroke<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  MySQL msql<span style="color: #339933;">;</span>
  <span style="color: #003399;">String</span> query<span style="color: #339933;">;</span>
&nbsp;
  msql <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySQL<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;localhost&quot;</span>, database, user, pass <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>msql.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* Get the MAX and MIN values from of the column */</span>
      msql.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT MAX(comment_ID) as max_n, &quot;</span> <span style="color: #339933;">+</span>
                 <span style="color: #0000ff;">&quot;MIN(comment_ID) AS min_n FROM wp_comments&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      msql.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">int</span> max_n <span style="color: #339933;">=</span> msql.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max_n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">int</span> min_n <span style="color: #339933;">=</span> msql.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;min_n&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* Get the ranges of missing numbers. 
         I've used a Wordpress 'wp_comments' table here.
       */</span>
      query <span style="color: #339933;">=</span>  <span style="color: #0000ff;">&quot;SELECT a.comment_ID+1 AS start, &quot;</span> <span style="color: #339933;">+</span>
               <span style="color: #0000ff;">&quot;MIN(b.comment_ID) - 1 AS end &quot;</span> <span style="color: #339933;">+</span>
               <span style="color: #0000ff;">&quot;FROM wp_comments AS a, wp_comments AS b &quot;</span> <span style="color: #339933;">+</span>
               <span style="color: #0000ff;">&quot;WHERE a.comment_ID &lt; b.comment_ID &quot;</span> <span style="color: #339933;">+</span>
               <span style="color: #0000ff;">&quot;GROUP BY a.comment_ID &quot;</span><span style="color: #339933;">+</span>
               <span style="color: #0000ff;">&quot;HAVING start &lt; MIN(b.comment_ID)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      msql.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span> query <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* Plot the missing numbers as vertical lines */</span>
      <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>msql.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">int</span> from <span style="color: #339933;">=</span> msql.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;start&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">int</span> to <span style="color: #339933;">=</span> msql.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;end&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span>from<span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span>to<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #666666; font-style: italic;">/* Map the numbers in the column to the sketch window */</span>
              <span style="color: #000066; font-weight: bold;">float</span> m <span style="color: #339933;">=</span> map<span style="color: #009900;">&#40;</span>i, min_n, max_n, <span style="color: #cc66cc;">0</span>, width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              line<span style="color: #009900;">&#40;</span>m, <span style="color: #cc66cc;">0</span>, m, <span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      print<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;connection failed !&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/sequence-gaps-in-mysql/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Multi column sequences in MySQL</title>
		<link>http://www.codediesel.com/mysql/multi-column-sequences-in-mysql/</link>
		<comments>http://www.codediesel.com/mysql/multi-column-sequences-in-mysql/#comments</comments>
		<pubDate>Wed, 18 Aug 2010 09:28:25 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2686</guid>
		<description><![CDATA[One of the most common used attributes in MySQL is definitely AUTO_INCREMENT. This is quite helpful when one needs to generate unique identities for the table rows. By default when a AUTO_INCREMENT column is the only column in a index, whether PRIMARY KEY or UNIQUE, it generates a single monotonic sequence of numbers : 1,2,3,4,&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2010/08/sequence.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2010/08/sequence.jpg" alt="" title="sequence" width="150" height="150" class="alignleft size-full wp-image-2688" /></a>One of the most common used attributes in MySQL is definitely AUTO_INCREMENT. This is quite helpful when one needs to generate unique identities for the table rows. By default when a AUTO_INCREMENT column is the only column in a index, whether PRIMARY KEY or UNIQUE, it generates a single monotonic sequence of numbers : 1,2,3,4,&#8230; etc. But for <a href="http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html" rel="nofollow" target="_blank" >MyISAM</a> storage engine it is possible to create complex sequences in a table containing an AUTO_INCREMENT column.<br />
<span id="more-2686"></span><br />
Take the simple table definition below:</p>

<div class="wp_codebox"><table><tr id="p268680"><td class="code" id="p2686code80"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> directors
<span style="color: #66cc66;">&#40;</span>
    id INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span>
    director VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    film VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> MyISAM;</pre></td></tr></table></div>

<p>Add a few rows to the table and you can see the &#8216;id&#8217; column sequence marching steadily in a uniform fashion.</p>

<div class="wp_codebox"><table><tr id="p268681"><td class="code" id="p2686code81"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> director   <span style="color: #66cc66;">|</span> film                  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> The Sugarland Express <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> Minority Report       <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> Empire of the Sun     <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span> soderbergh <span style="color: #66cc66;">|</span> Traffic               <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">|</span> soderbergh <span style="color: #66cc66;">|</span> Erin Brockovich       <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">6</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> The Goonies           <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span></pre></td></tr></table></div>

<h4>Complex sequences</h4>
<p>Linear sequences are fine, but sometimes we need to generate some complex sequence in our AUTO_INCREMENT column. A complex sequence is something that does not increase monotonically, but according to some pattern. In our example we want a separate linear sequence in the &#8216;id&#8217; column for each director. To create a such a sequence we change the table definition a little by combining two columns for the PRIMARY KEY; the &#8216;id&#8217; column (which is a AUTO_INCREMENT type) and the &#8216;director&#8217; column.</p>

<div class="wp_codebox"><table><tr id="p268682"><td class="code" id="p2686code82"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> directors
<span style="color: #66cc66;">&#40;</span>
    id INT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
    director VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
    film VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
    <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>director<span style="color: #66cc66;">,</span> id<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span> ENGINE <span style="color: #66cc66;">=</span> MyISAM;</pre></td></tr></table></div>

<p>Now add a few rows and the below table is what you will get.</p>

<div class="wp_codebox"><table><tr id="p268683"><td class="code" id="p2686code83"><pre class="sql" style="font-family:monospace;"><span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> director   <span style="color: #66cc66;">|</span> film                  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> The Sugarland Express <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> Minority Report       <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> Empire of the Sun     <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> soderbergh <span style="color: #66cc66;">|</span> Traffic               <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> soderbergh <span style="color: #66cc66;">|</span> Erin Brockovich       <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> The Goonies           <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span></pre></td></tr></table></div>

<p>Although the &#8216;id&#8217; column may now look random, sorting it by the &#8216;director&#8217; field will make it clear. The &#8216;id&#8217; column sequence is now linked up with the &#8216;director&#8217; field.</p>

<div class="wp_codebox"><table><tr id="p268684"><td class="code" id="p2686code84"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> directors <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> director;
&nbsp;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> director   <span style="color: #66cc66;">|</span> film                  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> soderbergh <span style="color: #66cc66;">|</span> Traffic               <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> soderbergh <span style="color: #66cc66;">|</span> Erin Brockovich       <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> The Sugarland Express <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> Minority Report       <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> Empire of the Sun     <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">|</span> spielberg  <span style="color: #66cc66;">|</span> The Goonies           <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+------------+-----------------------+</span></pre></td></tr></table></div>

<p>In the above example we have a two column index, but the concept can be applied to a n-column index, where the last column is the AUTO_INCREMENT column. MySQL than generates a independent sequence for each unique non-AUTO_INCREMENT column combination.</p>
<h4>Sequence calculation</h4>
<p>Incidentally, this is how MySQL calculates the next sequence number for a multi column index.</p>

<div class="wp_codebox"><table><tr id="p268685"><td class="code" id="p2686code85"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">MAX</span><span style="color: #009900;">&#40;</span>auto_increment_column<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span> WHERE prefix<span style="color: #339933;">=</span>given<span style="color: #339933;">-</span>prefix</pre></td></tr></table></div>

<p>So to get the next sequence number for &#8216;soderbergh&#8217;, MySQL runs something like the following query.</p>

<div class="wp_codebox"><table><tr id="p268686"><td class="code" id="p2686code86"><pre class="php" style="font-family:monospace;">SELECT <span style="color: #990000;">MAX</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span> FROM directors WHERE director <span style="color: #339933;">=</span> <span style="color: #0000ff;">'soderbergh'</span></pre></td></tr></table></div>

<h4>Some notes</h4>
<p><strong>Note</strong>:<br />
The &#8216;id&#8217; (AUTO_INCREMENT) field comes last in the key definition. Not placing the AUTO_INCREMENT field last will give you a normal AUTO_INCREMENT sequence.</p>
<p>If any of the non AUTO_INCREMENT columns to be indexed will contain a NULL value, create a UNIQUE index instead of a PRIMARY KEY.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/multi-column-sequences-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Benchmarking WordPress SQL using FirePHP</title>
		<link>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/</link>
		<comments>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 09:10:16 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/php/benchmarking-wordpress-sql-using-firephp/</guid>
		<description><![CDATA[Benchmarking your Wordpress SQL]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.codediesel.com/wp-content/uploads/2010/02/firephp_thumb.gif" alt="firephp_thumb" title="firephp_thumb" width="331" height="185" class="alignleft size-full wp-image-2478" />Building and running a WordPress site is a simple matter. WordPress is a considerably fast CMS system, until you start to add more and more plugins and one day you notice that Worpdress has started to slow down. It may be the case that SQL queries within some plugins are not optimized and are taking an increased amount of time executing them, this can considerably slow down your site. The first thing you can do to rectify the situation is to find out where exactly the bottleneck resides by analyzing the time each SQL query takes to executes. Some inquisitive people among you may also be interested in knowing in what sequence the WordPress SQL queries themselves are being run. Not that all bottlenecks occur due to unoptimized SQL, most are due to poor coding practices. Whatever the reason; the following post will show you how to look inside the SQL query execution of WordPress.<br />
<span id="more-2443"></span></p>
<h4>Logging WordPress SQL queries</h4>
<p>By default WordPress doesn&#8217;t log any queries. But you can instruct WordPress to do so  by adding the following configuration option to your WordPress &#8216;wpconfig.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p244387"><td class="code" id="p2443code87"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* wp-config.php */</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SAVEQUERIES'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once that is done you can add the following lines at the end of your WordPress themes &#8216;footer.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p244388"><td class="code" id="p2443code88"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* footer.php */</span>
<span style="color: #339933;">..</span>
<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>This will display the SQL statements executed by your WordPress installation in a raw format as shown below. It displays a total of three items &#8211; the executed query, the execution time of the query and the function that called the query.</p>

<div class="wp_codebox"><table><tr id="p244389"><td class="code" id="p2443code89"><pre class="text" style="font-family:monospace;">Array
(
    [0] =&gt; Array
        (
            [0] =&gt; SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'
            [1] =&gt; 0.004619836807251
            [2] =&gt; require, require_once, require_once, require_once, is_blog_installed, wp_load_alloptions
        )
&nbsp;
    [1] =&gt; Array
        (
            [0] =&gt; SELECT option_value FROM wp_options WHERE option_name = 'sidebars_widgets' LIMIT 1
            [1] =&gt; 0.0015749931335449
            [2] =&gt; require, require_once, require_once, require_once, do_action, call_user_func_array, wp_widgets_init, register_widget, WP_Widget_Factory-&gt;register, WP_Widget_Recent_Comments-&gt;WP_Widget_Recent_Comments, is_active_widget, wp_get_sidebars_widgets, get_option
        )
.
.</pre></td></tr></table></div>

<p>As you can see the raw information is not interesting to look at and it also clutters the actual site. A better way is to use FireBug and FirePHP to log the SQL queries without interfering with the WordPress output.</p>
<h4>Installing FirePHP</h4>
<p>Before starting you need to have <a href="https://addons.mozilla.org/en-US/firefox/addon/6149/" rel="nofollow" target="_blank" >FirePHP</a> and <a href="https://addons.mozilla.org/en-US/firefox/addon/1843" rel="nofollow" target="_blank" >FireBug</a> installed. FirePHP uses FireBugs console window to output the debug messages.  Once you have installed FirePHP and FireBug, you need to install the FirePHP <a href="http://www.firephp.org/HQ/Install.htm" rel="nofollow" target="_blank" >server libraries</a>. Copy the FirePHP server libraries to your WordPress themes directory. For e.g &#8216;wordpress\wp-content\themes\default\FirePHPCore&#8217;.</p>
<p>If you are unfamiliar with FirePHP a nice tutorial can be found at <a href="http://sixrevisions.com/web-development/how-to-debug-php-using-firefox-with-firephp/" rel="nofollow" target="_blank" >Six Revisions</a>.</p>
<h4>Displaying formatted information with FirePHP</h4>
<p>Once we are done with the above setup, you can add the following code at the end of your WordPress themes &#8216;footer.php&#8217; file, replacing the code we previously added.</p>

<div class="wp_codebox"><table><tr id="p244390"><td class="code" id="p2443code90"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* footer.php */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FirePHPCore/FirePHP.class.PHP&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$query_table</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SQL&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Execution Time&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Calling Function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$expensive_query_time</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$expensive_query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$total_time</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$query_table</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</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: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009933; font-style: italic;">/* Get the most expensive query */</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$expensive_query_time</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$expensive_query_time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</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: #000088;">$expensive_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$total_time</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$query</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>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$total_queries</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span> <span style="color: #339933;">=</span> FirePHP<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Display the queries in a formatted table */</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">group</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Query'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collapsed'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>  
                                    <span style="color: #0000ff;">'Color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'#000'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">table</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_queries</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; queries took &quot;</span> <span style="color: #339933;">.</span> 
                    <span style="color: #000088;">$total_time</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; seconds.&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query_table</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Display the query summary */</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">group</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Query Summary'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'Collapsed'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>  
                                            <span style="color: #0000ff;">'Color'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'#000'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$total_queries</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Total Queries'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$expensive_query</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Expensive Query'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$firephp</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$expensive_query_time</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Expensive Query Time'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Refresh your WordPress page and you should see the following output in the FireBug console. Click on the image below to view the complete debug screen. (Some queries have been removed for size.) As you can see FirePHP displays a nicely formatted output than the previous one.</p>
<p><a href="http://www.codediesel.com/data/images/firephp_large.gif"><img src="http://www.codediesel.com/wp-content/uploads/2010/02/firephp.gif" alt="firephp" title="firephp" width="563" height="247" class="aligncenter size-full wp-image-2455" /></a></p>
<h4>Modifying the query data returned by WordPress</h4>
<p>WordPress by default saves and returns the following three parameters for each SQL query:<br />
1. SQL query<br />
2. Query Execution time<br />
3. The method chain that called the query.</p>
<p>The code section where this happens is located in the query() method of the &#8216;wp-db.php&#8217; file in the WordPress &#8216;wp-includes&#8217; directory. The following snippet shows that section of code:</p>

<div class="wp_codebox"><table><tr id="p244391"><td class="code" id="p2443code91"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/* wp-db.php */</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #009933; font-style: italic;">// Log how the function was called</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">func_call</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\$</span>db-&gt;query(<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$query</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Keep track of the last query for debug..</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">// Perform the query via std mysql_query function..</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SAVEQUERIES'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> SAVEQUERIES <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">++</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">num_queries</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">defined</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SAVEQUERIES'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> SAVEQUERIES <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queries</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer_stop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                              <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_caller</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>You can modify the code to add some extra parameters as per your requirements, or modify the format in which it is returned. For example you could add code to save the data to a xml or csv file for latter retrieval and analysis.</p>
<p><strong>Note:</strong> Turning on the <em>define(&#8216;SAVEQUERIES&#8217;, true)</em> option can slow down your WordPress site, so make sure you disable it after your work is done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/benchmarking-wordpress-sql-using-firephp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

