<?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/tag/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>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="p29225"><td class="code" id="p2922code5"><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="p29226"><td class="code" id="p2922code6"><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="p29227"><td class="code" id="p2922code7"><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="p29228"><td class="code" id="p2922code8"><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>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 target="_blank" href="http://www.doctrine-project.org/projects">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 target="_blank" href="http://php.net/manual/en/language.namespaces.php">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="p277313"><td class="code" id="p2773code13"><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="p277314"><td class="code" id="p2773code14"><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="p277315"><td class="code" id="p2773code15"><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="p277316"><td class="code" id="p2773code16"><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 target="_blank" href="http://www.toadworld.com/DOWNLOADS/Freeware/ToadforMySQLFreeware/tabid/561/Default.aspx">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>Adminer &#8211; a fast MySQL administration tool</title>
		<link>http://www.codediesel.com/mysql/adminer-a-fast-mysql-administrator-tool/</link>
		<comments>http://www.codediesel.com/mysql/adminer-a-fast-mysql-administrator-tool/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 10:58:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1413</guid>
		<description><![CDATA[mysql administration program, adminer]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.codediesel.com/wp-content/uploads/2009/08/adminer1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/08/adminer1.gif" alt="adminer1" title="adminer1" width="64" height="68" class="alignleft size-full wp-image-1435" /></a>Before you go ahead and think that you don&#8217;t need yet another MySQL management tool, I think you should at least take a quick look at this one. <a target="_blank" href="http://www.adminer.org/en/">Adminer</a> is a single file (yes! everything in a single php file) tool to manage all your MySQL related tasks. All you have to do is upload the adminer.php file to your server and voila! you are ready to go. No messing around with huge software&#8217;s to manage your database. Weighing only 170KB, its easy to upload to any server. A demo of the same is included on the <a href="http://www.adminer.org/en/">Adminer</a> site.<br />
<span id="more-1413"></span><br />
Of course other tools such as phpMyadmin, MySQL Workbench, SQLyog MySQL GUI have their advantages with the various advanced features they provide, but if you need a quick way to manage your database with all of the standard tasks, then Adminer will surely suit your purpose. </p>
<p>Let us take a quick look at some features Adminer provides:</p>
<p># Connect to a database server with username and password<br />
# Select an existing database or create a new one<br />
# List fields, indexes, foreign keys and triggers of table<br />
# Change name, engine, collation, auto_increment and comment of table<br />
# Alter name, type, collation, comment and default values of columns<br />
# Add and drop tables and columns<br />
# Create, alter, drop and search by indexes including fulltext<br />
# Create, alter, drop and link lists by foreign keys<br />
# Create, alter, drop and select from views<br />
# Create, alter, drop and call stored procedures and functions<br />
# Create, alter and drop triggers<br />
# List data in tables with search, aggregate, sort and limit results<br />
# Insert new records, update and delete the existing ones<br />
# Supports all data types, blobs through file transfer<br />
# Execute any SQL command from a text field or a file<br />
# Export table structure, data, views, routines, databases to SQL or CSV<br />
# Print database schema connected by foreign keys<br />
# Show processes and kill them<br />
# Display users and rights and change them<br />
# Display variables with links to documentation<br />
# Manage events and table partitions (MySQL 5.1)</p>
<p>I don&#8217;t know about you but all this features will get me through the day.</p>
<h4>Compiled files</h4>
<p>But the most interesting idea I liked about Adminer is that if you download the source zip you will see that the software is actually composed of different files (which also are executable). The root folder contains a <em>compile.php</em> script that when executed combines all the required files in a single <em>adminer.php</em> file. During compilation all the php files are compressed (by removing the spaces), the images are base64 encoded and the javascript minified. In the true sense of the word the files are really not compiled but rather compressed and squeezed together in a single file.</p>
<h4>Skining</h4>
<p>You can also add your own skins to the application by creating a <em>adminer.css</em> file and putting it in the directory where adminer.php is located.</p>
<h4>Extensions</h4>
<p>Adminer also allows to to override some default functionality by writing your own custom code. Although I&#8217;ve not played much with this feature yet.</p>
<h4>In conclusion</h4>
<p>A fine product, which I find immensely helpful when I need to quickly perform some database task on a clients server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/adminer-a-fast-mysql-administrator-tool/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Copying tables in MySQL</title>
		<link>http://www.codediesel.com/mysql/copying-tables-in-mysql/</link>
		<comments>http://www.codediesel.com/mysql/copying-tables-in-mysql/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:16:13 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1368</guid>
		<description><![CDATA[easily copy tables in mysql]]></description>
			<content:encoded><![CDATA[<p>Whether you need to copy a test table to a production database, or you need to duplicate a table with only some selected rows or you just need to backup the original table, copying tables is a frequent task most of us regularly do. In this post we will see some quick methods to make copies of MySQL database tables.<br />
<span id="more-1368"></span><br />
For these examples I&#8217;ve used the following table structure.</p>

<div class="wp_codebox"><table><tr id="p136817"><td class="code" id="p1368code17"><pre class="sql" style="font-family:monospace;">id      username    password
<span style="color: #808080; font-style: italic;">-----------------------------------</span>
<span style="color: #cc66cc;">1</span>       admin       <span style="color: #66cc66;">*************</span>
<span style="color: #cc66cc;">2</span>       sameer      <span style="color: #66cc66;">*************</span>
<span style="color: #cc66cc;">3</span>       stewart     <span style="color: #66cc66;">*************</span>
&nbsp;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`admin`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">unsigned</span> <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>
  <span style="color: #ff0000;">`username`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</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: #993333; font-weight: bold;">default</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`password`</span> 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;">default</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><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM  <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1 <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">4</span> ;</pre></td></tr></table></div>

<h4>Quickly copying tables.</h4>
<p>The most common task is to copy the table structures. The following statement copies the structure of the &#8216;admin&#8217; table to a new table called &#8216;newadmin&#8217;.</p>

<div class="wp_codebox"><table><tr id="p136818"><td class="code" id="p1368code18"><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> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> admin</pre></td></tr></table></div>

<p>Note that only the structure and not the data is copied to the new table. To also copy the data use the following statement.</p>

<div class="wp_codebox"><table><tr id="p136819"><td class="code" id="p1368code19"><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> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span>
    <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>In the above examples it is assumed that the source and the destination tables which we want to copy are in the same database, but you can also copy tables from another database by prefixing the database name before the table. The following for example will copy the structure of the &#8216;admin&#8217; table from the &#8216;shop&#8217; database to the &#8216;newadmin&#8217; table in the current database.</p>

<div class="wp_codebox"><table><tr id="p136820"><td class="code" id="p1368code20"><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> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> shop<span style="color: #66cc66;">.</span>admin</pre></td></tr></table></div>

<p>You can also specify the destination database in the query.</p>

<div class="wp_codebox"><table><tr id="p136821"><td class="code" id="p1368code21"><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> newshop<span style="color: #66cc66;">.</span>newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> shop<span style="color: #66cc66;">.</span>admin</pre></td></tr></table></div>

<p>This will copy the &#8216;admin&#8217; table from the &#8216;shop&#8217; database to the new table &#8216;newadmin&#8217; in the &#8216;newshop&#8217; database.</p>
<p><strong>Note</strong>: In all the above examples the DATA DIRECTORY or INDEX DIRECTORY table options are <em>not copied</em> to the new table. So if you need an exact replica of a table you have to create two separate statements.</p>

<div class="wp_codebox"><table><tr id="p136822"><td class="code" id="p1368code22"><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> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> admin;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> newadmin <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin;</pre></td></tr></table></div>

<p>This will copy the table &#8216;admin&#8217; <em>exactly</em> to the &#8216;newadmin&#8217; table with all the correct table definitions, but we lose the flexibility we can have with the other method, which we will be seeing next.</p>
<p>To copy some select columns to our new table, specify the column names in a SELECT statement.</p>

<div class="wp_codebox"><table><tr id="p136823"><td class="code" id="p1368code23"><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> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> username<span style="color: #66cc66;">,</span> password <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This will only copy the &#8216;username&#8217; and &#8216;password&#8217; columns to the new table. This can be really useful when the original table contains over fifty columns and you want to copy only a few.</p>
<p>While copying a table you can also change the column names of the new table.</p>

<div class="wp_codebox"><table><tr id="p136824"><td class="code" id="p1368code24"><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> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> username <span style="color: #993333; font-weight: bold;">AS</span> uname<span style="color: #66cc66;">,</span> password <span style="color: #993333; font-weight: bold;">AS</span> pass <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>By now you may have already guessed that you could copy any queried data to the new table using the SELECT statement. The following for example copies all the rows from &#8216;admin&#8217; to the new table where the username starts with an &#8216;s&#8217;.</p>

<div class="wp_codebox"><table><tr id="p136825"><td class="code" id="p1368code25"><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> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'s'</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Copies the following two rows from the &#8216;admin&#8217; table.</p>

<div class="wp_codebox"><table><tr id="p136826"><td class="code" id="p1368code26"><pre class="sql" style="font-family:monospace;">id      username    password
<span style="color: #808080; font-style: italic;">-----------------------------------</span>
<span style="color: #cc66cc;">2</span>       sameer      <span style="color: #66cc66;">*************</span>
<span style="color: #cc66cc;">3</span>       stewart     <span style="color: #66cc66;">*************</span></pre></td></tr></table></div>

<p>While copying you can also change the table structure of the new table by specifying the same in your query.</p>

<div class="wp_codebox"><table><tr id="p136827"><td class="code" id="p1368code27"><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> newadmin
<span style="color: #66cc66;">&#40;</span>
    id INTEGER <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;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>This will copy the &#8216;admin&#8217; table and also assign the appropriate primary key to the &#8216;id&#8217; column in the new table. </p>
<h4>In conclusion</h4>
<p>So basically there are two methods of copying tables.<br />
a. By using the LIKE and AS keywords in single sql statement.</p>

<div class="wp_codebox"><table><tr id="p136828"><td class="code" id="p1368code28"><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> newadmin <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span>
    <span style="color: #993333; font-weight: bold;">FROM</span> admin
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>b. By using two different statements.</p>

<div class="wp_codebox"><table><tr id="p136829"><td class="code" id="p1368code29"><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> newadmin <span style="color: #993333; font-weight: bold;">LIKE</span> admin;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> newadmin <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> admin;</pre></td></tr></table></div>

<p>The first one offers flexibility while copying tables but does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions. </p>
<p>The second method truthfully copies the tables but does not offer the flexibility of the first method. By truthfully I mean only the DATA DIRECTORY or INDEX DIRECTORY table options are not copied.</p>
<p>Which method you use will depend on your specific requirement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/copying-tables-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SchemaBank: Browser based SQL schema designer</title>
		<link>http://www.codediesel.com/mysql/schemabank-browser-based-sql-schema-designer/</link>
		<comments>http://www.codediesel.com/mysql/schemabank-browser-based-sql-schema-designer/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 07:30:59 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=760</guid>
		<description><![CDATA[Whether desktop or web based, there has recently been a proliferation of SQL design tools on the net. SchemaBank may look like one more in the box. But a detailed look at the features will show you more on offer. SchemaBank is a visual data modelling tool which can help you create ERD diagrams within [...]]]></description>
			<content:encoded><![CDATA[<p>Whether desktop or web based, there has recently been a proliferation of SQL design tools on the net. <a target="_blank" href="http://schemabank.com/">SchemaBank</a> may look like one more in the box. But a detailed look at the features will show you more on offer. SchemaBank is a visual data modelling tool which can help you create ERD diagrams within all modern browsers. It supports MySQL and PostgreSQL.<br />
<span id="more-760"></span><br />
Some of the features of the product are listed below.<br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/04/erd-pane.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/04/erd-pane-300x225.gif" alt="erd-pane" title="erd-pane" width="300" height="225" class="alignleft size-medium wp-image-768" /></a></p>
<p><strong>Forward engineering</strong><br />
SchemaBank lets you generate SQL statements and other database objects directly from the the ER diagram. Of course many other tools provide it, so no surprises there.</p>
<p><strong>Reverse engineering</strong><br />
SchemaBank also lets you reverse engineer your existing database schema and import it into SchemaBank for editing. </p>
<p><strong>Versioning and Change Management</strong><br />
One of the unique features of the product, not found in most schema designers, is that of versioning. SchemaBank lets you commit ERD data, Text Objects and database Options to a repository. This lets you easily play with you database schemas without fear of losing vital information, or if you are creating various versions of the database like say for testing and production release. You can also generate diff report showing the difference between two versions of the schemas.</p>
<p><strong>Sharing Database Projects</strong><br />
Database projects created can also be shared with other developers, with the appropriate &#8216;read&#8217; or &#8216;write&#8217; access granted by the schema owner. The other developers should have an account with SchemaBank to access the project.</p>
<p><strong>Database objects</strong><br />
You can enter the SQL statements directly for database objects like views, functions, triggers, etc.</p>
<p><strong>GUI</strong><br />
The product also supports various color themes, if you do care about such things. The interface could also have been made a little bit more cleaner, specially the top part. Based on <a target="_blank" href="http://extjs.com/">ExtJS</a>, it drags a bit on slower connections but not so much that it breaks your flow of thought. As it relies heavily on Javascript, a faster browser like Chrome can make it run more smoother.</p>
<p><strong>Pricing</strong><br />
SchemaBank provides a free version with a limit of a single project. The other options cost upward of dollar forty per month.</p>
<p><strong>In closing</strong><br />
 The product is still in beta, so expect some positive changes before the final release.  A live demo can be found <a target="_blank" href="http://draw.schemabank.com/demo.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/schemabank-browser-based-sql-schema-designer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selecting all except some columns in MySQL</title>
		<link>http://www.codediesel.com/mysql/selecting-all-except-some-columns-in-mysql/</link>
		<comments>http://www.codediesel.com/mysql/selecting-all-except-some-columns-in-mysql/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 09:16:42 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=376</guid>
		<description><![CDATA[The MySQL SELECT is a ubiquitous statement. You can select rows using the &#8216;*&#8217; operator or by listing the individual column names. But many times you may require using all the columns from a table except a couple of them. For example you may have a table containing twelve columns from which you require only [...]]]></description>
			<content:encoded><![CDATA[<p>The MySQL SELECT is a ubiquitous statement. You can select rows using the &#8216;*&#8217; operator or by listing the individual column names. But many times you may require using all the columns from a table except a couple of them. For example you may have a table containing twelve columns from which you require only eleven columns. Most of us will use the &#8216;*&#8217; operator in the SELECT statement rather than explicitly list all the eleven column names. But what if the column we do not require contains a large amount of BLOB data. It is inefficient to include the BLOB column in the query as it will unnecessarily increase the query load. What we want is to select all the columns except the BLOB column. We can construct the required statement using the &#8216;SHOW COLUMNS&#8217; statement.<br />
<span id="more-376"></span><br />
The following php function will return all the column names for a given table.</p>

<div class="wp_codebox"><table><tr id="p37633"><td class="code" id="p376code33"><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;">function</span> get_column_names<span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SHOW COLUMNS FROM <span style="color: #006699; font-weight: bold;">{$table_name}</span>&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: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #009933; font-style: italic;">/* Store the column names retrieved in an array */</span>
        <span style="color: #000088;">$column_names</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;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$column_names</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Field'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$column_names</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now we create a function that returns a comma separated list of column names which we can then pass to the SELECT statement. The function also takes a array of column names we want to exclude.</p>

<div class="wp_codebox"><table><tr id="p37634"><td class="code" id="p376code34"><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;">function</span> create_statement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_name</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$exclude</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* $exclude contains the columns we do not want */</span>
    <span style="color: #000088;">$column_names</span> <span style="color: #339933;">=</span> get_column_names<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$statement</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;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$column_names</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$name</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: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exclude</span><span style="color: #009900;">&#41;</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;">$statement</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #000088;">$statement</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #000088;">$statement</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;,&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name</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;">return</span> <span style="color: #000088;">$statement</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now we create a list of columns and pass it to the SELECT statement.</p>

<div class="wp_codebox"><table><tr id="p37635"><td class="code" id="p376code35"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/*
We want to exclude columns 'post_author' and
'comment_count' from the table wp_posts
*/</span>
<span style="color: #000088;">$exclude</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;post_author&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;comment_count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$stmt_list</span> <span style="color: #339933;">=</span> create_statement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;wp_posts&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$exclude</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT <span style="color: #006699; font-weight: bold;">{$stmt_list}</span> FROM wp_posts&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/selecting-all-except-some-columns-in-mysql/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>HeidiSQL &#8211; Lightweight MySQL IDE</title>
		<link>http://www.codediesel.com/mysql/heidisql-lightweight-mysql-ide/</link>
		<comments>http://www.codediesel.com/mysql/heidisql-lightweight-mysql-ide/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 06:36:27 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=288</guid>
		<description><![CDATA[HeidiSQL is an easy-to-use ide for web-developers using the popular MySQL-Database. It allows you to manage and browse your databases and tables from an intuitive Windows® interface. With a download size of less than 2Mb, it is quite lightweight and fast to load. Some of the features of HeidiSQL: # generate nice SQL-exports # synchronize [...]]]></description>
			<content:encoded><![CDATA[<p><a title="heidisql" href="http://www.heidisql.com/" target="_blank">HeidiSQL</a> is an easy-to-use ide for web-developers using the popular MySQL-Database. It allows you to manage and browse your databases and tables from an intuitive Windows® interface. With a download size of less than 2Mb, it is quite lightweight and fast to load.<br />
<span id="more-288"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/02/database.png"><img class="alignnone size-medium wp-image-289" title="database" src="http://www.codediesel.com/wp-content/uploads/2009/02/database-300x204.png" alt="database" width="300" height="204" /></a></p>
<p><strong>Some of the features of HeidiSQL:</strong><br />
# generate nice SQL-exports<br />
# synchronize tables between two databases<br />
# manage user-privileges<br />
# import text-files<br />
# export table-data as CSV, HTML and XML<br />
# browse and edit table-data using a comfortable grid<br />
# batch-insert ascii or binary files into tables<br />
# write queries with customizable syntax-highlighting and code-completion<br />
# monitor and kill client-processes</p>
<p>Version 4.0, which will release soon also supports query completion and various grid editors for column data.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/02/query.png"><img class="alignnone size-medium wp-image-295" title="query" src="http://www.codediesel.com/wp-content/uploads/2009/02/query-300x181.png" alt="query" width="300" height="181" /></a></p>
<p>HeidiSQL is also available for Apple iPhone in the form of a Java port <a title="jHeidi" href="http://www.heidisql.com/jheidi/" target="_blank">jHeidi</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/heidisql-lightweight-mysql-ide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting MySQL table size with PHP</title>
		<link>http://www.codediesel.com/mysql/getting-mysql-table-size-with-php/</link>
		<comments>http://www.codediesel.com/mysql/getting-mysql-table-size-with-php/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 14:12:19 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=273</guid>
		<description><![CDATA[Below is a small script that lets you read the table sizes of a MySQL database. &#60;?php &#160; $link = mysql_connect&#40;'host', 'username', 'password'&#41;; &#160; $db_name = &#34;your database name here&#34;; $tables = array&#40;&#41;; &#160; mysql_select_db&#40;$db_name, $link&#41;; $result = mysql_query&#40;&#34;SHOW TABLE STATUS&#34;&#41;; &#160; while&#40;$row = mysql_fetch_array&#40;$result&#41;&#41; &#123; /* We return the size in Kilobytes */ $total_size [...]]]></description>
			<content:encoded><![CDATA[<p>Below is a small script that lets you read the table sizes of a MySQL database.</p>

<div class="wp_codebox"><table><tr id="p27338"><td class="code" id="p273code38"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'host'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;your database name here&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SHOW TABLE STATUS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* We return the size in Kilobytes */</span>
    <span style="color: #000088;">$total_size</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;Data_length&quot;</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> 
                   <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span> <span style="color: #0000ff;">&quot;Index_length&quot;</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$tables</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%.2f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total_size</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</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><span id="more-273"></span><br />
It will return data like the following for a &#8216;wordpress&#8217; database:</p>

<div class="wp_codebox"><table><tr id="p27339"><td class="code" id="p273code39"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>wp_comments<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">80.00</span>
    <span style="color: #009900;">&#91;</span>wp_links<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">48.00</span>
    <span style="color: #009900;">&#91;</span>wp_options<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">224.00</span>
    <span style="color: #009900;">&#91;</span>wp_pollsa<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">16.00</span>
    <span style="color: #009900;">&#91;</span>wp_pollsip<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">16.00</span>
    <span style="color: #009900;">&#91;</span>wp_pollsq<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">16.00</span>
    <span style="color: #009900;">&#91;</span>wp_postmeta<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">48.00</span>
    <span style="color: #009900;">&#91;</span>wp_posts<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">112.00</span>
    <span style="color: #009900;">&#91;</span>wp_term_relationships<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">32.00</span>
    <span style="color: #009900;">&#91;</span>wp_term_taxonomy<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">32.00</span>
    <span style="color: #009900;">&#91;</span>wp_terms<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">48.00</span>
    <span style="color: #009900;">&#91;</span>wp_usermeta<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">48.00</span>
    <span style="color: #009900;">&#91;</span>wp_users<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">48.00</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/getting-mysql-table-size-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Browser based SQL schema designer</title>
		<link>http://www.codediesel.com/mysql/browser-based-sql-schema-designer/</link>
		<comments>http://www.codediesel.com/mysql/browser-based-sql-schema-designer/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 11:11:29 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=253</guid>
		<description><![CDATA[WWW SQL Designer is a free database designer. It allows developers to create database schemas using a graphical interface. The designs can be further saved/loaded and exported to SQL scripts. The software supports a number of databases and languages. This tool allows you to draw and create E-R diagrams in your browser using Javascript without [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/wwwsqldesigner/">WWW SQL Designer</a> is a free database designer. It allows developers to create database schemas using a graphical interface. The designs can be further saved/loaded and exported to SQL scripts. The software supports a number of databases and languages.<br />
<span id="more-253"></span><br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/02/image11.gif" target="_self"><img class="aligncenter size-full wp-image-267" style="border: 1px solid #c0c0c0;" title="image11" src="http://www.codediesel.com/wp-content/uploads/2009/02/image11.gif" alt="image11" width="973" height="533" /></a></p>
<p>This tool allows you to draw and create E-R diagrams in your browser using Javascript without the need for any external plugins like Flash, Java or Flex.</p>
<p>Many database features are supported, such as keys, foreign key constraints, comments and indexes. You can either save your design , print it or export as SQL script. It is possible to import schema from existing database. The application supports MySQL, Sqlite, PostgreSQL ,MSSql.</p>
<p>The application allows you to:<br />
1. Draw E-R designs<br />
2. Edit tables and rows<br />
3. Manage keys<br />
4. Create relations (FK constraints)<br />
5. Save &amp; Load designs<br />
6. Import DB schemas</p>
<p>The importing of MySQL databases can take some time, so make sure you set PHP timeout to more than 60 seconds to avoid errors. A sample WordPress 2.7 schema imported is shown below.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/02/image21.gif" target="_self"><img class="aligncenter size-full wp-image-264" style="border: 1px solid #c0c0c0;" title="image21" src="http://www.codediesel.com/wp-content/uploads/2009/02/image21.gif" alt="image21" width="884" height="457" /></a></p>
<p><strong>Installation:</strong><br />
Installation is simple. Just download the code from <a href="http://code.google.com/p/wwwsqldesigner/downloads/list">here</a>, change your database settings in the &#8216;sqldesigner\backend\php-mysql\index.php&#8217; (if you are using PHP &amp; MySQL) file and you are ready.</p>
<p>A live demo of the application can be found <a href="http://ondras.zarovi.cz/sql/demo/?keyword=default">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/browser-based-sql-schema-designer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

