<?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>botsko &#187; MySQL</title>
	<atom:link href="http://www.botsko.net/blog/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.botsko.net/blog</link>
	<description>continuing education</description>
	<lastBuildDate>Sat, 24 Jul 2010 21:42:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Transitioning MySQL Field to Foreign Key</title>
		<link>http://www.botsko.net/blog/2009/12/30/transitioning-mysql-field-to-foreign-key/</link>
		<comments>http://www.botsko.net/blog/2009/12/30/transitioning-mysql-field-to-foreign-key/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 00:42:40 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/?p=433</guid>
		<description><![CDATA[When working with MySQL databases it&#8217;s often necessary to convert a regular field into one that refers to a second table using a foreign key. The process for this requires several steps: Insert distinct column values into new secondary table. Replace (update) existing table fields with proper foreign key for the related record in the [...]]]></description>
			<content:encoded><![CDATA[<p>When working with MySQL databases it&#8217;s often necessary to convert a regular field into one that refers to a second table using a foreign key. The process for this requires several steps:</p>
<ol>
<li>Insert distinct column values into new secondary table.</li>
<li>Replace (update) existing table fields with proper foreign key for the related record in the secondary table.</li>
</ol>
<p>This process can be time consuming if attempted manually. Luckily, MySQL 4.1+ allows for subqueries as well as the INSERT&#8230; SELECT&#8230; syntax.<br />
<span id="more-433"></span><br />
The first step just requires running that insert select.</p>
<p><code>INSERT INTO contact_specialties (specialty)<br />
SELECT DISTINCT specialty FROM `contacts` WHERE specialty != '' ORDER BY specialty;</code></p>
<p>Now that you have the distinct values in the second table, you can run an update query that uses a subquery to identify the proper foreign key for the old value.</p>
<p><code>UPDATE contacts SET specialty = (<br />
SELECT id FROM contact_specialties WHERE contact_specialties.specialty=contacts.specialty);<br />
</code></p>
<p>This method assumes that you&#8217;re moving to a many-to-one relationship. But what if you need to add a third table to transform it further into a many-to-many relationship?</p>
<p><code>INSERT INTO contact_specialties_link (contact_id, specialty_id)<br />
SELECT contacts.id, contact_specialties.id<br />
FROM contacts<br />
LEFT JOIN contact_specialties ON contact_specialties.specialty = contacts.specialty;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2009/12/30/transitioning-mysql-field-to-foreign-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing GMT in MySQL, Timezone Conversion in PHP</title>
		<link>http://www.botsko.net/blog/2009/08/13/storing-gmt-in-mysql-timezone-conversion-in-php/</link>
		<comments>http://www.botsko.net/blog/2009/08/13/storing-gmt-in-mysql-timezone-conversion-in-php/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 19:00:22 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/?p=353</guid>
		<description><![CDATA[At Trellis Development we&#8217;re working on several applications which need to frequently convert dates between various time zones. By default, both PHP and MySQL operate using a single timezone which is identified during installation and is quite often the same as the host server. Both tools allow you to specify a different timezone on the [...]]]></description>
			<content:encoded><![CDATA[<p>At Trellis Development we&#8217;re working on several applications which need to frequently convert dates between various time zones.</p>
<p>By default, both PHP and MySQL operate using a single timezone which is identified during installation and is quite often the same as the host server. Both tools allow you to specify a different timezone on the fly, and all following date operations work based off of that locale.</p>
<p>The primary goal is to preserve a base standard time so that we can easily convert between different timezones, without loosing the ability to convert again. Two choices emerged &#8211; storing a Unix Time stamp or a date string stored using GMT/UTC time.<br />
<span id="more-353"></span></p>
<p>The Unix Time stamp is a count of the number of seconds since January 1 1970 00:00:00 GMT. </p>
<p>The only difference between the two is that the time stamp is an integer so it makes no sense when read directly from the database. People don&#8217;t immediately understand that the date <em>1248988280</em> refers to 21:11:20 30-Jul-09 GMT. It makes more sense to me to store the dates in a format that&#8217;s easy to read, so we decided on storing GMT.</p>
<p>In the following demonstration, I show how to get the local time first and then convert that to GMT.</p>
<pre class="brush: php;">
$base_time = time();

// print my local time
print date(&quot;H:i:s d-M-Y T&quot;, $base_time); // 14:49:49 30-Jul-2009 PDT

// Convert my local time to GMT
$gmt = gmdate(&quot;H:i:s d-M-Y T&quot;, $base_time);
print $gmt; // 21:49:49 30-Jul-2009 GMT
</pre>
<p>However, when developing the application it makes most sense to just generate the GMT date first. PHP has a great function for handling that called gmdate:</p>
<pre class="brush: php;">
gmdate(&quot;H:i:s d-M-Y T&quot;);
</pre>
<p>At this point you can store that GMT format to your database. In MySQL, the supported date string format is the standard YYYY-MM-DD HH:MM:SS. The important thing is that you remember that the times are in GMT and will <em>always</em> need to be converted to local time for display.</p>
<p>Once you pull a record with the GMT date, you can then convert it to a specific timezone. The following example requires PHP 5.2 or above.</p>
<pre class="brush: php;">
$cnv_date = new DateTime($gmt);
$cnv_date-&gt;setTimeZone(new DateTimeZone('America/New_York'));
print $cnv_date-&gt;format('H:i:s d-M-Y T'); // 17:49:49 30-Jul-2009 EDT
</pre>
<p>Now, we have a standard date string in GMT stored in the database, and we can easily convert it to another timezone without changing the entire server timezone.</p>
<p>I would recommend that you wrap this functionality into a basic object within your application. Although it&#8217;s not much work for each conversion, it would further reduce the amount of code and give you a central point of management.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2009/08/13/storing-gmt-in-mysql-timezone-conversion-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Missing mysql.sock on Mac OS X</title>
		<link>http://www.botsko.net/blog/2009/03/16/missing-mysqlsock-on-mac-os-x/</link>
		<comments>http://www.botsko.net/blog/2009/03/16/missing-mysqlsock-on-mac-os-x/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 17:39:48 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/?p=282</guid>
		<description><![CDATA[Recently I was doing some local development on my Mac OS X Leopard machine. When running apache2 with php I was able to connect to the database just fine, and from the command line I was able to run mysql commands. However, when running a php script from the command line I was getting an [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was doing some local development on my Mac OS X Leopard machine. When running apache2 with php I was able to connect to the database just fine, and from the command line I was able to run mysql commands.</p>
<p>However, when running a php script from the command line I was getting an error:</p>
<p><code>Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2)</code></p>
<p>After spending some time thinking it was the code, I discovered that the mysql.sock file was missing.</p>
<p>Others on the net found the file elsewhere, like <code>/private/tmp/mysql.sock</code> or <code>/tmp/mysql.sock</code> but I had absolutely no .sock file anywhere. </p>
<p>Turns out, it did exist, but was named differently. According to the php.ini, the file was located at</p>
<p><code>/usr/local/mysql/run/mysql_socket </code></p>
<p>So, the solution was to create a symlink in /var/mysql to the proper file.</p>
<p><code>sudo ln -s /usr/local/mysql/run/mysql_socket /var/mysql/mysql.sock</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2009/03/16/missing-mysqlsock-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Fulltext Min Word Length</title>
		<link>http://www.botsko.net/blog/2008/10/03/mysql-fulltext-min-word-length/</link>
		<comments>http://www.botsko.net/blog/2008/10/03/mysql-fulltext-min-word-length/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 22:32:08 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/?p=240</guid>
		<description><![CDATA[Could have sworn that I&#8217;ve posted about this before, but it seems to be missing. To change the minimum word length that MySQL uses for a fulltext index search, just set a configuration and restart. $ vi /etc/my.cnf Then add in: ft_min_word_len=3 Or whatever minimum word length you&#8217;re looking for. Then, restart: /etc/init.d/mysqld restart Then [...]]]></description>
			<content:encoded><![CDATA[<p>Could have sworn that I&#8217;ve posted about this before, but it seems to be missing. To change the minimum word length that MySQL uses for a fulltext index search, just set a configuration and restart.</p>
<p><code><br />
$ vi /etc/my.cnf<br />
</code></p>
<p>Then add in:</p>
<p><code><br />
ft_min_word_len=3<br />
</code></p>
<p>Or whatever minimum word length you&#8217;re looking for. Then, restart:</p>
<p><code><br />
/etc/init.d/mysqld restart<br />
</code></p>
<p>Then rebuild your fulltext index.</p>
<p><code><br />
ALTER TABLE `mytable` DROP INDEX `myfulltext_index`<br />
ALTER TABLE `mytable` ADD FULLTEXT `fulltext` (<br />
`myfieldname` ,<br />
`anotherfieldname` ,<br />
)<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2008/10/03/mysql-fulltext-min-word-length/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Apache, MySQL, and PHP on OS X (10.5)</title>
		<link>http://www.botsko.net/blog/2007/11/19/install-apache-mysql-and-php-on-os-x-105/</link>
		<comments>http://www.botsko.net/blog/2007/11/19/install-apache-mysql-and-php-on-os-x-105/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 23:53:09 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tech-Tidbit]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/2007/11/19/install-apache-mysql-and-php-on-os-x-105/</guid>
		<description><![CDATA[While I was at zendcon I needed to setup my macbook with a typical apache/php/mysql server, but I was unsatisfied with both the default server that apple included and with the install system provided by XAMPP. I&#8217;ve since setup a server on both my MacBook and my iMac. In order to get support for mysql [...]]]></description>
			<content:encoded><![CDATA[<p>While I was at zendcon I needed to setup my macbook with a typical apache/php/mysql server, but I was unsatisfied with both the default server that apple included and with the install system provided by XAMPP. I&#8217;ve since setup a server on both my MacBook and my iMac.</p>
<p>In order to get support for mysql as well as tools necessary for compiling, you need to install the <a href="http://developer.apple.com/tools/download/">apple developer tools</a>. Check your original mac disc as you may already have a copy.</p>
<p>Anyway, on with our install:</p>
<p><strong>Apache</strong></p>
<p>First, download the latest version of the <a href="http://httpd.apache.org/download.cgi">apache httpd web server</a>.</p>
<p><code>./configure --prefix=/usr/local/apache --enable-module=so --enable-module=rewrite</code></p>
<p>Then, run the standard <code>make, sudo make install.</code></p>
<p><strong>MySQL</strong></p>
<p>Then download the <a href="http://dev.mysql.com/downloads/mysql/5.0.html#source">latest mysql source file</a>. Make sure you download the source &#8220;Compressed GNU TAR archive (tar.gz)&#8221; as we&#8217;ll need to custom compile it.</p>
<p>However, PHP5 no longer includes mysql shared libraries needed during the configure process. To get these, download the source file for the mysql version you just installed. Once downloaded, decompress it and run the following:</p>
<p><code>./configure --prefix=/usr/local/mysql --with-unix-socket-path=/usr/local/mysql/run/mysql_socket  --with-mysqld-user=mysql --with-comment --with-debug</code></p>
<p><code>make</code></p>
<p><code>sudo make install</code></p>
<p><code>sudo /usr/local/mysql/bin/mysql_install_db --force</code></p>
<p><code>sudo mkdir /usr/local/mysql/run</code><br />
<code>sudo mkdir /usr/local/mysql/data</code></p>
<p><code>sudo chgrp -R mysql /usr/local/mysql</code></p>
<p><code>sudo chown -R mysql /usr/local/mysql/run /usr/local/mysql/var /usr/local/mysql/data</code></p>
<p>Then start the server with:</p>
<p><code>sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &#038;</code></p>
<p><strong>zlib</strong></p>
<p>Then, <a href="http://www.zlib.net/">install zlib</a> if it&#8217;s not already installed on your system. Download zlib, and it should be a basic <code>./configure, make, sudo make install</code> process. </p>
<p>Once zlib is installed, make sure you know the path as we&#8217;ll need it during the php configure process.</p>
<p><strong>PHP</strong></p>
<p>Next, download the <a href="http://www.php.net/downloads.php">latest php package</a>. Decompress it, and now we&#8217;re ready to configure.</p>
<p><code>./configure --prefix=/usr/local/apache --enable-cli --with-mysql=/usr/local/mysql --with-zlib-dir=/usr/local --with-apxs2=/usr/local/apache/bin/apxs</code></p>
<p>Then, run <code>make</code> and<code> sudo make install</code> to complete the process.</p>
<p>Restart apache and test everything out. If php didn&#8217;t do it automatically, add in <code>AddType application/x-httpd-php .php</code> to your httpd.conf file &#8211; commonly located in <code>/usr/local/apache/conf/httpd.conf</code>.</p>
<p>Then, start your webserver.</p>
<p><code>sudo /usr/local/apache/bin/apachectl start</code></p>
<p>And finally, once everything is done you&#8217;ll want to setup your account to properly allow access to mysql and apache commands. Just run the following to add their paths to your bash profile:</p>
<p><code>echo 'export PATH=/usr/local/mysql/bin:$PATH' >> ~/.bash_profile<br />
echo 'export PATH=/usr/local/apache/bin:$PATH' >> ~/.bash_profile</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2007/11/19/install-apache-mysql-and-php-on-os-x-105/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL Backup Shell Script</title>
		<link>http://www.botsko.net/blog/2007/10/30/mysql-backup-shell-script/</link>
		<comments>http://www.botsko.net/blog/2007/10/30/mysql-backup-shell-script/#comments</comments>
		<pubDate>Tue, 30 Oct 2007 23:44:25 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Tech-Tidbit]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/2007/10/30/mysql-backup-shell-script/</guid>
		<description><![CDATA[Many of my clients need a quick backup solution before we implement anything on a larger more permanent scale. I wrote this simple script that simply make a copy of a database and then created a timestamped tarball of the file. #!bin/sh date=`date +%Y-%m-%d_%Hh%M` cd /my/path/for/backups/ mysqldump -umy_user -p"mypass" my_database > $date.sql tar -zcvf $date.tgz [...]]]></description>
			<content:encoded><![CDATA[<p>Many of my clients need a quick backup solution before we implement anything on a larger more permanent scale. I wrote this simple script that simply make a copy of a database and then created a timestamped tarball of the file.</p>
<p><code>#!bin/sh</p>
<p></code><code>date=`date +%Y-%m-%d_%Hh%M`</code></p>
<p><code>cd <strong>/my/path/for/backups/</strong></code></p>
<p><code>mysqldump -u<strong>my_user</strong> -p"<strong>mypass</strong>" <strong>my_database</strong> > $date.sql</code></p>
<p><code>tar -zcvf $date.tgz *.sql</code></p>
<p><code># uncomment this line to import the sql into another database, for example as a mirror</code><br />
<code>#mysql -u<strong>my_user</strong> -p"<strong>mypass</strong>" <strong>mirror_database</strong> < $date.sql</code></p>
<p></code><code>rm *.sql</code></p>
<p>Then, just setup a cron job to run this at whichever interval you want this to run.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2007/10/30/mysql-backup-shell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tech-Tidbit: Altering Primary Keys in MySQL</title>
		<link>http://www.botsko.net/blog/2007/03/29/tech-tidbit-altering-primary-keys-in-mysql/</link>
		<comments>http://www.botsko.net/blog/2007/03/29/tech-tidbit-altering-primary-keys-in-mysql/#comments</comments>
		<pubDate>Thu, 29 Mar 2007 16:04:49 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tech-Tidbit]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/?p=181</guid>
		<description><![CDATA[I&#8217;ve come to design every database table with an auto-increment id field that is a primary key. However, sometimes I work on projects where tables have no such column and may have duplicate rows. In order to drop the existing primary key definitions I run: [mysql] ALTER TABLE mytable DROP PRIMARY KEY; [/mysql] Then, I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve come to design every database table with an auto-increment id field that is a primary key. However, sometimes I work on projects where tables have no such column and may have duplicate rows. In order to drop the existing primary key definitions I run:</p>
<p>[mysql]<br />
ALTER TABLE mytable DROP PRIMARY KEY;<br />
[/mysql]</p>
<p>Then, I add a new one:</p>
<p>[mysql]<br />
ALTER TABLE `mytable` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;<br />
[/mysql]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2007/03/29/tech-tidbit-altering-primary-keys-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tech-Tidbit: MySQL Weighting</title>
		<link>http://www.botsko.net/blog/2007/03/10/tech-tidbit-mysql-weighting/</link>
		<comments>http://www.botsko.net/blog/2007/03/10/tech-tidbit-mysql-weighting/#comments</comments>
		<pubDate>Sat, 10 Mar 2007 22:31:18 +0000</pubDate>
		<dc:creator>Michael Botsko</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tech-Tidbit]]></category>

		<guid isPermaLink="false">http://www.botsko.net/blog/?p=169</guid>
		<description><![CDATA[I&#8217;m still rather disappointed with my options for weighting random results in MySQL, but I&#8217;ve done the best I can do with it. It&#8217;s more about figuring out what numbers you use for the weighting rather than the calculation itself. The query itself ends with: [mysql] ORDER BY RAND() * weight_fieldname [/mysql] Now, my possible [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still rather disappointed with my options for weighting random results in MySQL, but I&#8217;ve done the best I can do with it. It&#8217;s more about figuring out what numbers you use for the weighting rather than the calculation itself. The query itself ends with:</p>
<p>[mysql]<br />
ORDER BY RAND() * weight_fieldname<br />
[/mysql]</p>
<p>Now, my possible weights from <code>weight_fieldname</code> are 5, 20, 100. 5 is the default weight for every record &#8211; 20 increases the chances of the record a bit and 100 pretty much sets the record as an exclusive result. Now, if you only have a single weight applied then setting it to 0 will reduce the weight of an item, however this is very difficult when you start having multiple weights.</p>
<p>One additional problem is that if you have a serious amount of records, using ORDER BY RAND() is going to be a performance hit. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.botsko.net/blog/2007/03/10/tech-tidbit-mysql-weighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
