<?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>Geek Files &#187; MySQL</title>
	<atom:link href="http://www.sunilb.com/category/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.sunilb.com</link>
	<description>Question Everything - that&#039;s the only way to learn</description>
	<lastBuildDate>Mon, 30 Jan 2012 04:59:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>MySQL Tutorial &#8211; Seeking records in a predefined order</title>
		<link>http://www.sunilb.com/mysql/mysql-tutorial-seeking-records-in-a-predefined-order</link>
		<comments>http://www.sunilb.com/mysql/mysql-tutorial-seeking-records-in-a-predefined-order#comments</comments>
		<pubDate>Fri, 09 Nov 2007 13:35:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/mysql/mysql-tutorial-seeking-records-in-a-predefined-order</guid>
		<description><![CDATA[You can use this tutorial in various projects where the requirement is to sequence the records based on the order that has already been predefined. Imagine having to write code where you are supposed to display articles on the basis of an order predefined by the administrator of the site. I had a similar challenge [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fmysql%2Fmysql-tutorial-seeking-records-in-a-predefined-order"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fmysql%2Fmysql-tutorial-seeking-records-in-a-predefined-order&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>You can use this tutorial in various projects where the requirement is to sequence the records based on the order that has already been predefined.</p>
<p>Imagine having to write code where you are supposed to display articles on the basis of an order predefined by the administrator of the site. I had a similar challenge with one of my sites <a href="http://www.careercurry.com" title="Career Articles, Career Resources and Career Tips" target="_blank">www.careercurry.com</a> where I had to show article listings on the home page based on the sequencing that I set through the admin control panel.</p>
<p><span id="more-62"></span></p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>I had set the article sequencing as 1, 3, 7, 2, 9, 10, 4 and the most obvious query was as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">   <span style="color: #993333; font-weight: bold;">SELECT</span> title<span style="color: #66cc66;">,</span> description
   <span style="color: #993333; font-weight: bold;">FROM</span> articles <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The above query would have been very simple&#8230; but the output was a little different than what I had expected. The result query set was ordered as 1, 2, 3, 4, 7, 9, 10.</p>
<p>The reason why this happened was because the query was on primary key and because there was no ORDER BY instruction for the query, it sorted the results based on the primary key index.</p>
<p>The challenge was to seek the result in the exact same order, without having to write additional logic on my application layer. The solution that I researched and discovered was as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">   <span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span> title<span style="color: #66cc66;">,</span> description
   <span style="color: #993333; font-weight: bold;">FROM</span> articles <span style="color: #993333; font-weight: bold;">WHERE</span> id <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">FIELD</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The above gave me the result as expected. I then wanted to dig further to know what happened inside the FIELD() function. The FIELD() function returns the record position of the field i.e. <strong>&#8216;id&#8217;</strong> based on the data i.e 1, 3, 7, 2, 9, 10, 4.</p>
<p>Therefore, it returned the record position for each and every data 1, 3, &#8230; 4. So the ORDER BY syntax ordered the result based on the record positions rather than the natural order of the <strong>&#8216;id&#8217;</strong> <strong>PRIMARY KEY</strong> index.</p>
<p><code><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9205249129147978";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2007-10-26: Sunilb.com 468x60
google_ad_channel = "7135663694";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "0F0F0F";
google_color_url = "CCCCCC";
//-->
</script><br />
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
</code></p>
<p>You can read more on FIELD() function here <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field" target="_blank">FIELD() </a></p>
<p>Please leave behind any comments that you might have &#8211; a question, more information or doubts.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/mysql/mysql-tutorial-seeking-records-in-a-predefined-order/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Difference between MySQL delete and truncate table</title>
		<link>http://www.sunilb.com/mysql/difference-between-mysql-delete-and-truncate-table</link>
		<comments>http://www.sunilb.com/mysql/difference-between-mysql-delete-and-truncate-table#comments</comments>
		<pubDate>Mon, 22 Oct 2007 20:27:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.sunilb.com/mysql/difference-between-mysql-delete-and-truncate-table</guid>
		<description><![CDATA[Both remove records from the table, so what is the difference. Very simple, read along. For this article, I will use a &#8216;friends&#8217; table. delete from friends; and truncate table friends; Both the above statements remove all records from the table, but the essentiall difference is as follows. delete from friends &#8211; will delete all [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sunilb.com%2Fmysql%2Fdifference-between-mysql-delete-and-truncate-table"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.sunilb.com%2Fmysql%2Fdifference-between-mysql-delete-and-truncate-table&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Both remove records from the table, so what is the difference. Very simple, read along.</p>
<p>For this article, I will use a &#8216;friends&#8217; table.</p>
<p>delete from friends;</p>
<p>and</p>
<p>truncate table friends;</p>
<p><span id="more-28"></span></p>
<p><code><br />
<script type="text/javascript">
<!--
google_ad_client = "pub-9205249129147978"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_type = "text_image";
//2007-10-18: SB - Square - Text/Image
google_ad_channel = "3552230476"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "0F0F0F"; google_color_url = "CCCCCC";
//-->
</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><br />
</code></p>
<p>Both the above statements remove all records from the table, but the essentiall difference is as follows.</p>
<p><strong>delete from friends</strong> &#8211; will delete all records from the friends table. That&#8217;s it. I.e. the auto_increment counter does not get reset.</p>
<p><strong>truncate table friends</strong> &#8211; will delete all records from the table and also rebuild the table, thus resetting the auto_increment counter.</p>
<p><strong>Try these steps to identify yourself:</strong></p>
<p><strong>Step 1:</strong></p>
<p>create table friends (<br />
id int not null primary key auto_increment,<br />
name varchar(20)<br />
);</p>
<p><strong>Step 2:</strong><br />
insert into friends (name) values (&#8216;Sunil&#8217;);<br />
insert into friends (name) values (&#8216;Vishal&#8217;);<br />
insert into friends (name) values (&#8216;Vikram&#8217;);</p>
<p><strong>Step 3:</strong><br />
select * from friends;</p>
<p>Note the id of the last record. It will be 3.</p>
<p><strong>Step 4:</strong><br />
delete from friends;</p>
<p><strong>Step 5:</strong><br />
insert into friends (name) values (&#8216;Mithil&#8217;);</p>
<p><code><br />
<script type="text/javascript">
<!--
google_ad_client = "pub-9205249129147978"; google_ad_width = 300; google_ad_height = 250; google_ad_format = "300x250_as"; google_ad_type = "text_image";
//2007-10-18: SB - Square - Text/Image
google_ad_channel = "3552230476"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "0F0F0F"; google_color_url = "CCCCCC";
//-->
</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><br />
</code></p>
<p><strong>Step 6:</strong><br />
select * from friends;</p>
<p>Note that the id of the only record will be 4. This means that delete from friends removed all records, but did not reset the auto_increment counter.</p>
<p><strong>Step 7:</strong><br />
Repeat Step 2 &#8211; 3 and goto step 8.</p>
<p><strong>Step 8:</strong><br />
truncate table friends;</p>
<p><strong>Step 9:</strong><br />
Repeat step 5 &#8211; 6 and goto step 10.</p>
<p>Note that after you repeat step 6, you will see that the only record i.e. of Mithil has id 1. This means that truncate deletes all the records from the table and resets the auto_increment pointer to start from 1.</p>
<p>Leave a comment if you need more examples or have more questions</p>
<p align="center"><!--subscribe2--></p>

]]></content:encoded>
			<wfw:commentRss>http://www.sunilb.com/mysql/difference-between-mysql-delete-and-truncate-table/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

