<?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>KADIMI &#187; MySQL</title>
	<atom:link href="http://www.kadimi.com/en/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kadimi.com/en</link>
	<description>[Web developper, Linux addict, Technical translator...]</description>
	<lastBuildDate>Sun, 15 Jan 2012 10:48:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>MySQL World Cities database installation</title>
		<link>http://www.kadimi.com/en/mysql-cities-690</link>
		<comments>http://www.kadimi.com/en/mysql-cities-690#comments</comments>
		<pubDate>Wed, 01 Sep 2010 03:00:07 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=690</guid>
		<description><![CDATA[MaxMind offers a free cities database, the database contains more than 2.7 mil. locations, you can download it at MaxMind&#8217;s website. Once you have downloaded the compressed database, extract the file worldcitiespop.txt and invoke the MySQL client (preferably from the same directory where the worldcitiespop.txt file resides). Create the world database and the cities table [...]]]></description>
			<content:encoded><![CDATA[<p>MaxMind offers a free cities database, the database contains more than 2.7 mil. locations, you can download it at <a href="http://www.maxmind.com/app/worldcities">MaxMind&#8217;s website</a>.</p>
<p>Once you have downloaded the compressed database, extract the file worldcitiespop.txt and invoke the MySQL client (preferably from the same directory where the worldcitiespop.txt file resides).</p>
<h2>Create the world database and the cities table</h2>
<p>Here is the structure for the world database (remove the first two lines if you already have a database):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Database: `world`</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #ff0000;">`world`</span>;
<span style="color: #993333; font-weight: bold;">USE</span> <span style="color: #ff0000;">`world`</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- Table: `cities`</span>
<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;">`cities`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`Country`</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_unicode_ci <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: #ff0000;">`City`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><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;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_unicode_ci <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: #ff0000;">`AccentCity`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><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;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_unicode_ci <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: #ff0000;">`Region`</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_unicode_ci <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: #ff0000;">`Population`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</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: #66cc66;">,</span>
  <span style="color: #ff0000;">`Latitude`</span> <span style="color: #993333; font-weight: bold;">FLOAT</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: #ff0000;">`Longitude`</span> <span style="color: #993333; font-weight: bold;">FLOAT</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;">KEY</span> <span style="color: #ff0000;">`AccentCity`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`AccentCity`</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`City`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`City`</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</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>utf8;</pre></td></tr></table></div>

<h2>Import cities from worldcitiespop.txt into database</h2>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">LOAD</span> <span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #993333; font-weight: bold;">LOCAL</span> <span style="color: #993333; font-weight: bold;">INFILE</span> _path_to_file_
  <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`cities`</span>
  <span style="color: #993333; font-weight: bold;">FIELDS</span>
    <span style="color: #993333; font-weight: bold;">TERMINATED</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">','</span>
  <span style="color: #993333; font-weight: bold;">LINES</span>
    <span style="color: #993333; font-weight: bold;">TERMINATED</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>
  <span style="color: #993333; font-weight: bold;">IGNORE</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">LINES</span>;</pre></td></tr></table></div>

<p>_path_the_file_ can be either an absolute path or relative path (relative to the folder from where you invoked the MySQL client), so in case you followed my instructions and invoked the MySQL client from the directory that contains the file worldcitiespop.txt, _path_to_file_ will be worldcitiespop.txt.</p>
<p>The file worldcitiespop.txt is as big as 130 MB so the process may take very much longer than a few seconds</p>
<h2>What&#8217;s next? up to you&#8230;</h2>
<p>Now that the database is installed you can use it for your application, I use it on one of my websites where users can choose one or more cities on their profiles, then they get a daily weather forecast for those cities.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/mysql-cities-690/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>MySQL statement terminators</title>
		<link>http://www.kadimi.com/en/mysql-query-terminators-440</link>
		<comments>http://www.kadimi.com/en/mysql-query-terminators-440#comments</comments>
		<pubDate>Thu, 28 Jan 2010 01:15:32 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=440</guid>
		<description><![CDATA[There is the semicolon &#8220;;&#8221; that every one knows, and &#8220;\g&#8221; which is just the same: mysql &#62; SELECT * FROM wp_links; mysql &#62; SELECT * FROM wp_links\g Those are exactly the same, and will produce the same result, here is an example: mysql&#62; select * from wp_links \g +---------+-----------------------+--------------+------------+-------------+---------------+------------------+--------------+------------+-------------+---------------------+----------+------------+-----------------------+ &#124; link_id &#124; link_url &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>There is the semicolon &#8220;;&#8221; that every one knows, and &#8220;\g&#8221; which is just the same:</p>
<blockquote><p><code>mysql &gt; SELECT * FROM wp_links;<br />
mysql &gt; SELECT * FROM wp_links\g</code></p></blockquote>
<p>Those are exactly the same, and will produce the same result, here is an example:</p>
<blockquote><p><code>mysql&gt; select * from wp_links \g<br />
<span style="color: #999999;">+---------+-----------------------+--------------+------------+-------------+---------------+------------------+--------------+------------+-------------+---------------------+----------+------------+-----------------------+<br />
| link_id | link_url              | link_name    | link_image | link_target | link_category | link_description | link_visible | link_owner | link_rating | link_updated        | link_rel | link_notes | link_rss              |<br />
+---------+-----------------------+--------------+------------+-------------+---------------+------------------+--------------+------------+-------------+---------------------+----------+------------+-----------------------+<br />
|       1 | http://www.ar-wp.com/ | WP arabic |            |             |             0 |                  | Y            |          1 |           0 | 0000-00-00 00:00:00 |          |            | http://www.ar-wp.com/ |<br />
+---------+-----------------------+--------------+------------+-------------+---------------+------------------+--------------+------------+-------------+---------------------+----------+------------+-----------------------+<br />
1 row in set (0.01 sec)</span><br />
</code></p></blockquote>
<p>\G is another valid statement terminator, it tells MySQL to produce vertical output, this comes in handy when the result has too much columns, here is the same statement again but with the \G terminator:</p>
<blockquote><p><code>mysql&gt; select * from wp_links\G<br />
<span style="color: #999999;">*************************** 1. row ***************************<br />
link_id: 1<br />
link_url: http://www.ar-wp.com/<br />
link_name: WP arabic<br />
link_image:<br />
link_target:<br />
link_category: 0<br />
link_description:<br />
link_visible: Y<br />
link_owner: 1<br />
link_rating: 0<br />
link_updated: 0000-00-00 00:00:00<br />
link_rel:<br />
link_notes:<br />
link_rss: http://www.ar-wp.com/<br />
1 row in set (0.00 sec)</span></code></p></blockquote>
<p>As you can see this is a more user friendly fashion for displaying data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/mysql-query-terminators-440/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL script files how-to</title>
		<link>http://www.kadimi.com/en/script-files-149</link>
		<comments>http://www.kadimi.com/en/script-files-149#comments</comments>
		<pubDate>Mon, 22 Jun 2009 19:38:33 +0000</pubDate>
		<dc:creator>Nabil</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.kadimi.com/en/?p=149</guid>
		<description><![CDATA[What is a MySQL script file?.. Why use it? A MySQL script file (aka MySQL batch file) is a regular text file containing MySQL statements separeted by terminantors, statements in a MySQL script file can be run from the MySQL client, this is very useful, many PHP script available online come with MySQL script files [...]]]></description>
			<content:encoded><![CDATA[<h2>What is a MySQL script file?.. Why use it?</h2>
<p>A <strong>MySQL script file</strong> (aka MySQL batch file) is a regular text file containing MySQL statements separeted by terminantors, statements in a MySQL script file can be run from the MySQL client, this is very useful, many PHP script available online come with MySQL script files that need to be run as part of the PHP script installation process, <span id="more-149"></span>when you have PHPMyAdmin (or some other tool) available, you can copy the contents of the MySQL script file then paste those statements into PHPMyAdmin and run the SQL queries, this works fine on local servers and/or for small MySQL script files, but if you have 20000 MySQL queries (maybe you are trying to restore your forum database), using copy/paste and PHPMyAdmin is a very <span style="color: #ff0000;">bad idea</span>.</p>
<h2>Run MySQL script files from MySQL client</h2>
<p>You can run MySQL script files from the MySQL client like this</p>
<blockquote><p><code>mysql&gt; SOURCE <em>path_to_script_file</em>;</code></p></blockquote>
<p>or</p>
<blockquote><p><code>mysql&gt; \. <em>path_to_script_file</em>;</code></p></blockquote>
<p>path_to_script_file is an absolute or relative path (relative to the directory from which you invoked the MySQL client), examples:</p>
<blockquote><p><code>mysql&gt; SOURCE queries.sql;<br />
mysql&gt; SOURCE ./../sql/queries.sql;<br />
mysql&gt; SOURCE C:\wamp\sql\queries.sql;</code></p>
<p>&#8211; exactly the same as :</p>
<p><code>mysql&gt; \. queries.sql;<br />
mysql&gt; \. ./../sql/queries.sql;<br />
mysql&gt; \. C:\wamp\sql\queries.sql;</code></p></blockquote>
<h2>Run MySQL script files from the shell</h2>
<p>You can execute a MySQL script file directly from the shell with the <code>mysql</code> command like this:</p>
<blockquote><p><code><br />
shell&gt; mysql <em>database_name</em> &lt; <em>path_to_script_file</em><br />
or<br />
shell&gt; mysql -f <em>database_name</em> &lt; <em>path_to_script_file</em><br />
or<br />
shell&gt; mysql --force <em>database_name</em> &lt; <em>path_to_script_file</em><br />
</code></p></blockquote>
<p>The -f (or &#8211;force) option tells mysql to continue the execution of upcoming queries even if it encounters errors, some examples:</p>
<blockquote><p><code><br />
shell&gt; mysql <em>database_name</em> &lt; script.sql<br />
shell&gt; mysql -f <em>database_name</em> &lt; ../sql/script_with_errors.sql<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.kadimi.com/en/script-files-149/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

