<?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>Pressing the Red Button &#187; mySQL</title>
	<atom:link href="http://www.pressthered.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pressthered.com</link>
	<description>Adventures in Software Development</description>
	<lastBuildDate>Sun, 05 Sep 2010 16:55:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>GUID Primary Keys in SQL</title>
		<link>http://www.pressthered.com/guid_primary_keys_in_sql/</link>
		<comments>http://www.pressthered.com/guid_primary_keys_in_sql/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 05:41:22 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[microsoft sql server]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.pressthered.com/?p=258</guid>
		<description><![CDATA[Creating a GUID in Microsoft SQL Server:

select newid();

Creating a GUID in MYSQL:

select uuid();

Why use a GUID as a primary key?
The main reason is if you want to guarantee uniqueness between databases and servers. 
I have never been a fan of GUIDs. Especially when having to write sql statements for reports. They&#8217;re difficult to scan and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Creating a GUID in Microsoft SQL Server:</strong></p>
<pre name="code" class="sql">
select newid();
</pre>
<p><strong>Creating a GUID in MYSQL:</strong></p>
<pre name="code" class="sql">
select uuid();
</pre>
<p><strong>Why use a GUID as a primary key?</strong><br />
The main reason is if you want to guarantee uniqueness between databases and servers. </p>
<p>I have never been a fan of GUIDs. Especially when having to write sql statements for reports. They&#8217;re difficult to scan and impossible to sort. It used to be they were non existent and then they gained popularity when trying to store objects in relational databases became a fad. Try writing reports against an object hierarchy that is stored in a relational database. Every other column is a GUID. It&#8217;s not fun. </p>
<p>Anyway I had one recent experience that reinforced the need of a GUID in a database. Some production data was unintentionally lost in one table(due to a roll back). This particular table was using GUID&#8217;s as a primary key. The table could not be restored since it was already out of date. Instead the table was restored to a copy of the database. This allowed a quick comparison of the two tables and a painless insert via sql. What the GUID did in this case, was guarantee sameness (instead of uniqueness). </p>
<p>I am still going to stick to integers as primary keys but I am now going to add an auto generated GUID column for the sake of backup/restores/import/exports/etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/guid_primary_keys_in_sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Hacks: Sequentially Numbering Records</title>
		<link>http://www.pressthered.com/sql_hacks_sequentially_numbering_records/</link>
		<comments>http://www.pressthered.com/sql_hacks_sequentially_numbering_records/#comments</comments>
		<pubDate>Mon, 11 Dec 2006 19:45:34 +0000</pubDate>
		<dc:creator>jim.richmond</dc:creator>
				<category><![CDATA[Tech Tips]]></category>
		<category><![CDATA[mySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.pressingtheredbutton.com/sql_hacks_sequentially_numbering_records/</guid>
		<description><![CDATA[It is a simple concept, I want each record of a SQL result set to be numbered 1 through X. Well SQL wasn't really designed to do this so here come some the ugliest hacks you'll every see to "just get it done".
Here is some more background. I got contracted to write some fixes for [...]]]></description>
			<content:encoded><![CDATA[<p>It is a simple concept, I want each record of a SQL result set to be numbered 1 through X. Well SQL wasn't really designed to do this so here come some the ugliest hacks you'll every see to "just get it done".</p>
<p>Here is some more background. I got contracted to write some fixes for an existing ecommerce web site written in PHP/MySQL.  One of those fixes was a bug that occurred when trying to sort the store's products in the admin interface. Each product had a sort order that could be manually adjusted using these up and down arrows. Common approach, but it wasn't implemented well. I'm not going to go into any more details, but the simple fix was to create a function that could reset the sort when needed.</p>
<p>I wanted a quick way to regenerate the sort order numbers based on the SQL result set. I did some googling and found this link pertaining to Microsoft SQL Server.:<br />
<a href="http://www.databasejournal.com/features/mssql/article.php/10894_2244821_1">Database Journal Article</a></p>
<p>These methods could be modified to work with MySQL but I needed something more elegant. I found my solution in the least likely of places, <a href="http://dev.mysql.com/doc/refman/4.1/en/update.html">the manual</a>.</p>
<p><strong>Displaying Sequential Numbers in MySQL:</strong></p>
<div class="igBar"><span id="lsql-4"><a href="#" onclick="javascript:showPlainTxt('sql-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-4">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SET</span> @rnk=<span style="color: #cc66cc;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> id, @rnk:=@rnk+<span style="color: #cc66cc;color:#800000;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> product_order</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">FROM</span> products <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> product_name; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now for the let down. It won't work in PHP 4/MySQL. This is typical when working in LAMP. You develop code locally, go to move it to the client's machine and they have minor versions differences and your code doesn't work. The solution above requires two SQL statements and the PHP MySQL library does not allow the running of multiple queries at the same time (to prevent SQL injection attacks). Supposedly it will work in PHP 5/MySQL with the mysqli_multi_query but I didn't have time to test.</p>
<p>Since excuses don't pay the bills, I had to resort to an ugly hack (as opposed to elegant hack). I didn't really like the methods in the Database Journal link above so I made my own:</p>
<p><strong>Displaying Sequential numbers in MySQL/PHP4:</strong><br />
The basic idea behind this one is to use a seperate table to update the sort order.<br />
1. Create a Sort Table</p>
<div class="igBar"><span id="lsql-5"><a href="#" onclick="javascript:showPlainTxt('sql-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-5">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`sort_order`</span> <span style="color:#006600; font-weight:bold;">&#40;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff0000;">`id`</span> int<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">11</span><span style="color:#006600; font-weight:bold;">&#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;">'0'</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff0000;">`order`</span> int<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;color:#800000;">11</span><span style="color:#006600; font-weight:bold;">&#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;">AUTO_INCREMENT</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>&nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">`order`</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>2. Run the SQL that will update the sort</p>
<div class="igBar"><span id="lsql-6"><a href="#" onclick="javascript:showPlainTxt('sql-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">SQL:</span>
<div id="sql-6">
<div class="sql">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#first clear the table</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">TRUNCATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> sort_order;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#reset the auto increment</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> sort_order <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>=<span style="color: #cc66cc;color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#insert the id's of the records sorted the way you want</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> sort_order <span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> products <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> product_order;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">#update the sort order field</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">UPDATE</span> products p</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sort_order so <span style="color: #993333; font-weight: bold;">ON</span> p.id = so.id</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333; font-weight: bold;">SET</span> p.product_order = so.<span style="color: #993333; font-weight: bold;">ORDER</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This was a quick and dirty hack but it worked for the situation. For multi-user use, I would put the statements in a transaction.</p>
<p>Let me know if you have any questions or come up with anything else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pressthered.com/sql_hacks_sequentially_numbering_records/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
