<?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>Open Script Solution&#187; Database</title>
	<atom:link href="http://www.openscriptsolution.com/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.openscriptsolution.com</link>
	<description>... where solutions for script programming are found ...</description>
	<lastBuildDate>Tue, 31 Jan 2012 03:22:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Create Breadcrumb Using the Nested Set Data Model in SQL Server</title>
		<link>http://www.openscriptsolution.com/2012/01/31/how-to-create-breadcrumb-using-the-nested-set-data-model-in-sql-server/</link>
		<comments>http://www.openscriptsolution.com/2012/01/31/how-to-create-breadcrumb-using-the-nested-set-data-model-in-sql-server/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 03:19:46 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[basis data]]></category>
		<category><![CDATA[breadcrumb]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[konversi]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Stored Procedure]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=1501</guid>
		<description><![CDATA[Still remember the similar article I wrote regarding the title of this article? Well, if you want to implement breadcrumb using the SQL Server database, then here is the related Stored Procedures in SQL Server format. In other words, this following article will show you the same Stored Procedures in SQL Server instead of using [...]]]></description>
			<content:encoded><![CDATA[<p>Still remember the <a href="http://www.openscriptsolution.com/2011/12/05/how-to-create-breadcrumb-using-the-nested-set-data-model-in-mysql/" title="How to Create Breadcrumb Using the Nested Set Data Model in MySQL" target="_blank">similar article</a> I wrote regarding the title of this article? Well, if you want to implement breadcrumb using the SQL Server database, then here is the related Stored Procedures in SQL Server format. In other words, this following article will show you the same Stored Procedures in SQL Server instead of using MySQL database. This is very useful if you want to switch your database from MySQL to SQL Server for your web application, so you don&#8217;t have to waste your time to convert the Stored Procedures in MySQL to SQL Server format. Simply use the following three Stored Procedures that I has successfully converted it to SQL Server. I have had tested those three SPs in SQL Server, and made sure that everything went smoothly.</p>
<ol>
<li>
<h2>Inserting New Breadcrumb</h2>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>addnewbreadcrumb<span style="color: #66cc66;">&#93;</span>
  @PageTitleParent <span style="color: #993333; font-weight: bold;">AS</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: #66cc66;">,</span>
	@PageTitle <span style="color: #993333; font-weight: bold;">AS</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: #66cc66;">,</span>
	@PageURL <span style="color: #993333; font-weight: bold;">AS</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;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
<span style="color: #993333; font-weight: bold;">DECLARE</span> @ParentLevel <span style="color: #993333; font-weight: bold;">INTEGER</span>;
<span style="color: #993333; font-weight: bold;">DECLARE</span> @RecCount <span style="color: #993333; font-weight: bold;">INTEGER</span>;
<span style="color: #993333; font-weight: bold;">DECLARE</span> @CheckRecCount <span style="color: #993333; font-weight: bold;">INTEGER</span>;
<span style="color: #993333; font-weight: bold;">DECLARE</span> @MyPageTitle <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: #808080; font-style: italic;">-- routine body goes here, e.g.</span>
<span style="color: #993333; font-weight: bold;">SET</span> @ParentLevel <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Rgt <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> 
Page_Title <span style="color: #66cc66;">=</span> @PageTitleParent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @CheckRecCount <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> RecordCount <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">WHERE</span> 
Page_Title <span style="color: #66cc66;">=</span> @PageTitle<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">IF</span> @CheckRecCount <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span>
  <span style="color: #993333; font-weight: bold;">BEGIN</span> 
		<span style="color: #993333; font-weight: bold;">SET</span> @MyPageTitle <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'The following Page_Title is already exists in database: '</span> <span style="color: #66cc66;">+</span>  @PageTitle;
		<span style="color: #993333; font-weight: bold;">SELECT</span> @MyPageTitle;
		<span style="color: #993333; font-weight: bold;">GOTO</span> GoodBye;
  <span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span> 
   <span style="color: #993333; font-weight: bold;">SET</span> Lft <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> Lft <span style="color: #66cc66;">&gt;</span> @ParentLevel <span style="color: #993333; font-weight: bold;">THEN</span>
      Lft <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span>
    <span style="color: #993333; font-weight: bold;">ELSE</span>
      Lft <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span>
   Rgt <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> Rgt <span style="color: #66cc66;">&gt;=</span> @ParentLevel <span style="color: #993333; font-weight: bold;">THEN</span>
      Rgt <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span>
   <span style="color: #993333; font-weight: bold;">ELSE</span>
      Rgt <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">0</span>
   <span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #993333; font-weight: bold;">WHERE</span>  Rgt <span style="color: #66cc66;">&gt;=</span> @ParentLevel;
&nbsp;
<span style="color: #993333; font-weight: bold;">SET</span> @RecCount <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #993333; font-weight: bold;">IF</span> @RecCount <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
    <span style="color: #993333; font-weight: bold;">BEGIN</span>
		<span style="color: #808080; font-style: italic;">-- this is for handling the first record</span>
		<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>Page_Title<span style="color: #66cc66;">,</span> Page_URL<span style="color: #66cc66;">,</span> Lft<span style="color: #66cc66;">,</span> Rgt<span style="color: #66cc66;">&#41;</span>
					<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>@PageTitle<span style="color: #66cc66;">,</span> @PageURL<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #993333; font-weight: bold;">END</span>
	<span style="color: #993333; font-weight: bold;">ELSE</span>
    <span style="color: #993333; font-weight: bold;">BEGIN</span>
		<span style="color: #808080; font-style: italic;">-- whereas the following is for the second record, and so forth!</span>
		<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>Page_Title<span style="color: #66cc66;">,</span> Page_URL<span style="color: #66cc66;">,</span> Lft<span style="color: #66cc66;">,</span> Rgt<span style="color: #66cc66;">&#41;</span>
					<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span>@PageTitle<span style="color: #66cc66;">,</span> @PageURL<span style="color: #66cc66;">,</span> @ParentLevel<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>@ParentLevel <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	  <span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
  GoodBye:
&nbsp;
<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #993333; font-weight: bold;">GO</span></pre></div></div>

</li>
<li>
<h2>Removing or Deleting Breadcrumb</h2>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>deletebreadcrumbbasedonpagetitle<span style="color: #66cc66;">&#93;</span>
  @PageTitle <span style="color: #993333; font-weight: bold;">AS</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;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DECLARE</span> @DeletedPageTitle <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;">DECLARE</span> @DeletedLft <span style="color: #993333; font-weight: bold;">INTEGER</span>;
<span style="color: #993333; font-weight: bold;">DECLARE</span> @DeletedRgt <span style="color: #993333; font-weight: bold;">INTEGER</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> @DeletedPageTitle<span style="color: #66cc66;">=</span>Page_Title<span style="color: #66cc66;">,</span> @DeletedLft<span style="color: #66cc66;">=</span>Lft<span style="color: #66cc66;">,</span> @DeletedRgt<span style="color: #66cc66;">=</span>Rgt
<span style="color: #993333; font-weight: bold;">FROM</span>   <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span> 
<span style="color: #993333; font-weight: bold;">WHERE</span>  Page_Title <span style="color: #66cc66;">=</span> @PageTitle;
&nbsp;
<span style="color: #993333; font-weight: bold;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">WHERE</span> Lft <span style="color: #993333; font-weight: bold;">BETWEEN</span> @DeletedLft <span style="color: #993333; font-weight: bold;">AND</span> @DeletedRgt;
&nbsp;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>breadcrumblinks<span style="color: #66cc66;">&#93;</span>
   <span style="color: #993333; font-weight: bold;">SET</span> Lft <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> Lft <span style="color: #66cc66;">&gt;</span> @DeletedLft <span style="color: #993333; font-weight: bold;">THEN</span>
             Lft <span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#40;</span>@DeletedRgt <span style="color: #66cc66;">-</span> @DeletedLft <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #993333; font-weight: bold;">ELSE</span>
             Lft
          <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span>
       Rgt <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> Rgt <span style="color: #66cc66;">&gt;</span> @DeletedLft <span style="color: #993333; font-weight: bold;">THEN</span>
             Rgt <span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#40;</span>@DeletedRgt <span style="color: #66cc66;">-</span> @DeletedLft <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #993333; font-weight: bold;">ELSE</span>
             Rgt
          <span style="color: #993333; font-weight: bold;">END</span>
   <span style="color: #993333; font-weight: bold;">WHERE</span> Lft <span style="color: #66cc66;">&gt;</span> @DeletedLft
      <span style="color: #993333; font-weight: bold;">OR</span> Rgt <span style="color: #66cc66;">&gt;</span> @DeletedLft;
<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #993333; font-weight: bold;">GO</span></pre></div></div>

</li>
<li>
<h2>Moving Breadcrumb to Another Parent</h2>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>movebreadcrumb<span style="color: #66cc66;">&#93;</span>
  @CurrentRoot <span style="color: #993333; font-weight: bold;">AS</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: #66cc66;">,</span>
	@NewParent <span style="color: #993333; font-weight: bold;">AS</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;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
  <span style="color: #808080; font-style: italic;">-- routine body goes here, e.g.</span>
  <span style="color: #993333; font-weight: bold;">DECLARE</span> @Origin_Lft <span style="color: #993333; font-weight: bold;">INTEGER</span>;
	<span style="color: #993333; font-weight: bold;">DECLARE</span> @Origin_Rgt <span style="color: #993333; font-weight: bold;">INTEGER</span>;
	<span style="color: #993333; font-weight: bold;">DECLARE</span> @NewParent_Rgt <span style="color: #993333; font-weight: bold;">INTEGER</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> @Origin_Lft<span style="color: #66cc66;">=</span>Lft<span style="color: #66cc66;">,</span> @Origin_Rgt<span style="color: #66cc66;">=</span>Rgt
	<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>breadcrumblinks 
	<span style="color: #993333; font-weight: bold;">WHERE</span> Page_Title <span style="color: #66cc66;">=</span> @CurrentRoot;
<span style="color: #993333; font-weight: bold;">SET</span> @NewParent_Rgt <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> Rgt <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>breadcrumblinks
	<span style="color: #993333; font-weight: bold;">WHERE</span> Page_Title <span style="color: #66cc66;">=</span> @NewParent<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span>breadcrumblinks 
	<span style="color: #993333; font-weight: bold;">SET</span> Lft <span style="color: #66cc66;">=</span> Lft <span style="color: #66cc66;">+</span> 
	<span style="color: #993333; font-weight: bold;">CASE</span>
		<span style="color: #993333; font-weight: bold;">WHEN</span> @NewParent_Rgt <span style="color: #66cc66;">&lt;</span> @Origin_Lft
			<span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #993333; font-weight: bold;">CASE</span>
				<span style="color: #993333; font-weight: bold;">WHEN</span> Lft <span style="color: #993333; font-weight: bold;">BETWEEN</span> @Origin_Lft <span style="color: #993333; font-weight: bold;">AND</span> @Origin_Rgt
					<span style="color: #993333; font-weight: bold;">THEN</span> @NewParent_Rgt <span style="color: #66cc66;">-</span> @Origin_Lft
				<span style="color: #993333; font-weight: bold;">WHEN</span> Lft <span style="color: #993333; font-weight: bold;">BETWEEN</span> @NewParent_Rgt	<span style="color: #993333; font-weight: bold;">AND</span> @Origin_Lft <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
					<span style="color: #993333; font-weight: bold;">THEN</span> @Origin_Rgt <span style="color: #66cc66;">-</span> @Origin_Lft <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>
				<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span>
		<span style="color: #993333; font-weight: bold;">WHEN</span> @NewParent_Rgt <span style="color: #66cc66;">&gt;</span> @Origin_Rgt
			<span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #993333; font-weight: bold;">CASE</span>
				<span style="color: #993333; font-weight: bold;">WHEN</span> Lft <span style="color: #993333; font-weight: bold;">BETWEEN</span> @Origin_Lft	<span style="color: #993333; font-weight: bold;">AND</span> @Origin_Rgt
					<span style="color: #993333; font-weight: bold;">THEN</span> @NewParent_Rgt <span style="color: #66cc66;">-</span> @Origin_Rgt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
				<span style="color: #993333; font-weight: bold;">WHEN</span> Lft <span style="color: #993333; font-weight: bold;">BETWEEN</span> @Origin_Rgt <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AND</span> @NewParent_Rgt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
					<span style="color: #993333; font-weight: bold;">THEN</span> @Origin_Lft <span style="color: #66cc66;">-</span> @Origin_Rgt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
				<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span>
			<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span>
	Rgt <span style="color: #66cc66;">=</span> Rgt <span style="color: #66cc66;">+</span> 
	<span style="color: #993333; font-weight: bold;">CASE</span>
		<span style="color: #993333; font-weight: bold;">WHEN</span> @NewParent_Rgt <span style="color: #66cc66;">&lt;</span> @Origin_Lft
			<span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #993333; font-weight: bold;">CASE</span>
		<span style="color: #993333; font-weight: bold;">WHEN</span> Rgt <span style="color: #993333; font-weight: bold;">BETWEEN</span> @Origin_Lft <span style="color: #993333; font-weight: bold;">AND</span> @Origin_Rgt
			<span style="color: #993333; font-weight: bold;">THEN</span> @NewParent_Rgt <span style="color: #66cc66;">-</span> @Origin_Lft
		<span style="color: #993333; font-weight: bold;">WHEN</span> Rgt <span style="color: #993333; font-weight: bold;">BETWEEN</span> @NewParent_Rgt <span style="color: #993333; font-weight: bold;">AND</span> @Origin_Lft <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
			<span style="color: #993333; font-weight: bold;">THEN</span> @Origin_Rgt <span style="color: #66cc66;">-</span> @Origin_Lft <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span>
		<span style="color: #993333; font-weight: bold;">WHEN</span> @NewParent_Rgt <span style="color: #66cc66;">&gt;</span> @Origin_Rgt
			<span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #993333; font-weight: bold;">CASE</span>
				<span style="color: #993333; font-weight: bold;">WHEN</span> Rgt <span style="color: #993333; font-weight: bold;">BETWEEN</span> @Origin_Lft <span style="color: #993333; font-weight: bold;">AND</span> @Origin_Rgt
					<span style="color: #993333; font-weight: bold;">THEN</span> @NewParent_Rgt <span style="color: #66cc66;">-</span> @Origin_Rgt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
				<span style="color: #993333; font-weight: bold;">WHEN</span> Rgt <span style="color: #993333; font-weight: bold;">BETWEEN</span> @Origin_Rgt <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>	<span style="color: #993333; font-weight: bold;">AND</span> @NewParent_Rgt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
					<span style="color: #993333; font-weight: bold;">THEN</span> @Origin_Lft <span style="color: #66cc66;">-</span> @Origin_Rgt <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
				<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span>
			<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #993333; font-weight: bold;">GO</span></pre></div></div>

</li>
</ol>
<p>Hope it helps!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2012%2F01%2F31%2Fhow-to-create-breadcrumb-using-the-nested-set-data-model-in-sql-server%2F&amp;title=How%20to%20Create%20Breadcrumb%20Using%20the%20Nested%20Set%20Data%20Model%20in%20SQL%20Server" id="wpa2a_2"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2012/01/31/how-to-create-breadcrumb-using-the-nested-set-data-model-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create Breadcrumb Using the Nested Set Data Model in MySQL</title>
		<link>http://www.openscriptsolution.com/2011/12/05/how-to-create-breadcrumb-using-the-nested-set-data-model-in-mysql/</link>
		<comments>http://www.openscriptsolution.com/2011/12/05/how-to-create-breadcrumb-using-the-nested-set-data-model-in-mysql/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 06:32:59 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Adjacency List Model]]></category>
		<category><![CDATA[Hierarchical Data Model]]></category>
		<category><![CDATA[Model Data Hirarki]]></category>
		<category><![CDATA[Nested Set Data Model]]></category>
		<category><![CDATA[Stored Procedure]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=1480</guid>
		<description><![CDATA[There are so many articles in the Internet have explained us that the Nested Set Data Model has many advantages (although it is more complex among the others) for handling the Hierarchical Data Model. Some of the advantages of this Nested Set Data Model are: You can create unlimited levels and it only needs one [...]]]></description>
			<content:encoded><![CDATA[<p>There are so many articles in the Internet have explained us that the Nested Set Data Model has many advantages (although it is more complex among the others) for handling the Hierarchical Data Model. </p>
<p>Some of the advantages of this Nested Set Data Model are: </p>
<ol>
<li>You can create unlimited levels and it only needs one query to get all the related path.</li>
<li>Since it only need one query, then it is faster to get and display the full path.</li>
<li>It is easy to manage the records (inserting, deleting, and moving record). We will focus on this item in this article using MySQL database.</li>
</ol>
<p>The only one disadvantage of this model is it is more complex than the others. You have to be careful to manage all of the records (inserting, deleting, and moving), otherwise you will break all of the breadcrumb records!</p>
<p>I came across the following article: <a href="http://www.developer.com/db/article.php/3517366/Using-the-Nested-Set-Data-Model-for-Breadcrumb-Links.htm" title="Using the Nested Set Data Model for Breadcrumb Links" target="_blank">Using the Nested Set Data Model for Breadcrumb Links</a> and I think this is the smartest way how you can create the independent breadcrumb for any websites which have not implemented it. Since that article only explained how to add and delete a row (breadcrumb), but had not explained more detail how can you move the certain breadcrumb from the current position to the new parent, then in this article I wrote, I will try to explain you step by step, how you can create the Stored Procedures in MySQL for inserting new breadcrumb, deleting breadcrumbs based on their parent, and also: <strong>Moving the Breadcrumb</strong> from the current location to the new parent. This will be helpful to manage your breadcrumb easily and quickly especially if the current location has many breadcrumb childs. If you do not handle this carefully then you will destroy all of your breadcrumb records in your table. </p>
<ol>
<li>
<h2>Creating Table for Breadcrumb</h2>
<p>First of all, let&#8217;s create a table which we will use to store all the breadcrumb records using the following SQL query:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #FF00FF;">&#40;</span>
  <span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>URL`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`Lft`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">4</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #008000;">`Rgt`</span> <span style="color: #999900; font-weight: bold;">int</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">4</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
  <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span><span style="color: #CC0099;">=</span>MyISAM <span style="color: #990099; font-weight: bold;">DEFAULT</span> <span style="color: #FF9900; font-weight: bold;">CHARSET</span><span style="color: #CC0099;">=</span>utf8<span style="color: #000033;">;</span></pre></div></div>

</li>
<li>
<h2>Inserting New Breadcrumb</h2>
<p>Now let&#8217;s create a Stored Procedure for adding a new record to your breadcrumblinks table above:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`addnewbreadcrumb`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> PageTitleParent <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> 
                                    PageTitle <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> 
                                    PageURL <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
GoodBye: <span style="color: #990099; font-weight: bold;">BEGIN</span>
<span style="color: #808080; font-style: italic;">-- Need three parameters (PageTitleParent, PageTitle, and PageURL), </span>
<span style="color: #808080; font-style: italic;">-- look at this line --&gt; `Page_Title` = PageTitleParent);</span>
<span style="color: #808080; font-style: italic;">-- look at this line --&gt; VALUES (PageTitle, PageURL, ParentLevel, (ParentLevel + 1));</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> ParentLevel <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> RecCount <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> CheckRecCount <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> MyPageTitle <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SET</span> ParentLevel <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> Rgt <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #990099; font-weight: bold;">WHERE</span> 
<span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> PageTitleParent<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SET</span> CheckRecCount <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> RecCount <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #990099; font-weight: bold;">WHERE</span> 
<span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> PageTitle<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">IF</span> CheckRecCount <span style="color: #CC0099;">&gt;</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
		<span style="color: #990099; font-weight: bold;">SET</span> MyPageTitle <span style="color: #CC0099;">=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;The following Page<span style="color: #008080; font-weight: bold;">_</span>Title is already exists in database: &quot;</span><span style="color: #000033;">,</span> PageTitle<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> MyPageTitle<span style="color: #000033;">;</span>
		LEAVE GoodBye<span style="color: #000033;">;</span>
  <span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`breadcrumblinks`</span>
   <span style="color: #990099; font-weight: bold;">SET</span> Lft <span style="color: #CC0099;">=</span> <span style="color: #009900;">CASE</span> <span style="color: #009900;">WHEN</span> Lft <span style="color: #CC0099;">&gt;</span> ParentLevel <span style="color: #009900;">THEN</span>
      Lft <span style="color: #CC0099;">+</span> <span style="color: #008080;">2</span>
    <span style="color: #009900;">ELSE</span>
      Lft <span style="color: #CC0099;">+</span> <span style="color: #008080;">0</span>
    <span style="color: #009900;">END</span><span style="color: #000033;">,</span>
   Rgt <span style="color: #CC0099;">=</span> <span style="color: #009900;">CASE</span> <span style="color: #009900;">WHEN</span> Rgt <span style="color: #CC0099;">&gt;=</span> ParentLevel <span style="color: #009900;">THEN</span>
      Rgt <span style="color: #CC0099;">+</span> <span style="color: #008080;">2</span>
   <span style="color: #009900;">ELSE</span>
      Rgt <span style="color: #CC0099;">+</span> <span style="color: #008080;">0</span>
   <span style="color: #009900;">END</span>
<span style="color: #990099; font-weight: bold;">WHERE</span>  Rgt <span style="color: #CC0099;">&gt;=</span> ParentLevel<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SET</span> RecCount <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">IF</span> RecCount <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
		<span style="color: #808080; font-style: italic;">-- this is for handling the first record</span>
		<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #FF00FF;">&#40;</span>Page_Title<span style="color: #000033;">,</span> Page_URL<span style="color: #000033;">,</span> Lft<span style="color: #000033;">,</span> Rgt<span style="color: #FF00FF;">&#41;</span>
					<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span>PageTitle<span style="color: #000033;">,</span> PageURL<span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">ELSE</span>
		<span style="color: #808080; font-style: italic;">-- whereas the following is for the second record, and so forth!</span>
		<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #FF00FF;">&#40;</span>Page_Title<span style="color: #000033;">,</span> Page_URL<span style="color: #000033;">,</span> Lft<span style="color: #000033;">,</span> Rgt<span style="color: #FF00FF;">&#41;</span>
					<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span>PageTitle<span style="color: #000033;">,</span> PageURL<span style="color: #000033;">,</span> ParentLevel<span style="color: #000033;">,</span> <span style="color: #FF00FF;">&#40;</span>ParentLevel <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #009900;">END</span></pre></div></div>

<p>Now let&#8217;s discuss that Stored Procedure (SP) above:<br />
Look at this following line:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`addnewbreadcrumb`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> PageTitleParent <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> 
                                    PageTitle <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> 
                                    PageURL <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span></pre></div></div>

<p>We see that this SP has a name: <strong>addnewbreadcrumb</strong>, and it needs three parameters.<br />
They are:<br />
- <strong>PageTitleParent</strong>: This parameter is the page title parent of your new breadcrumb,<br />
- <strong>PageTitle</strong>: This parameter is the page title of your new breadcrumb,<br />
- <strong>PageURL</strong>: This parameter is the page URL of your new breadcrumb.</p>
<p>Here is how we can keep the <strong>ParentLevel</strong> value based on the <strong>PageTitleParent</strong> parameter using this following SQL:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> ParentLevel <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> Rgt <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #990099; font-weight: bold;">WHERE</span> 
<span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> PageTitleParent<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></div></div>

<p>Before we add a new breadcrumb record, let&#8217;s check to the table whether the new record that we will insert has already exists or not using this following code (this is important in order to avoid the summing up the value of Lft and Rgt of the breadcrumb):</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> CheckRecCount <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> RecCount <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcru.mblinks`</span> <span style="color: #990099; font-weight: bold;">WHERE</span> 
<span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> PageTitle<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">IF</span> CheckRecCount <span style="color: #CC0099;">&gt;</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
		<span style="color: #990099; font-weight: bold;">SET</span> MyPageTitle <span style="color: #CC0099;">=</span> <span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;The following Page<span style="color: #008080; font-weight: bold;">_</span>Title is already exists in database: &quot;</span><span style="color: #000033;">,</span> PageTitle<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
		<span style="color: #990099; font-weight: bold;">SELECT</span> MyPageTitle<span style="color: #000033;">;</span>
		LEAVE GoodBye<span style="color: #000033;">;</span>
  <span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span></pre></div></div>

<p>That code will tell us, if the record already exists, then display the custom message: &#8220;The following Page_Title is already exists in database: {ThePageTitle}&#8221;, and then exit from that SP by jumping to GoodBye label before the BEGIN statement above.</p>
<p>The rest of that code is for adding the new breadcrumb. Important to know, that I added the special handling for adding the first record if the table is still empty, and also for adding the second record and so forth. Please look at the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SET</span> RecCount <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">COUNT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #CC0099;">*</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">IF</span> RecCount <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> <span style="color: #009900;">THEN</span>
		<span style="color: #808080; font-style: italic;">-- this is for handling the first record</span>
		<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #FF00FF;">&#40;</span>Page_Title<span style="color: #000033;">,</span> Page_URL<span style="color: #000033;">,</span> Lft<span style="color: #000033;">,</span> Rgt<span style="color: #FF00FF;">&#41;</span>
					<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span>PageTitle<span style="color: #000033;">,</span> PageURL<span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #000033;">,</span> <span style="color: #008080;">2</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">ELSE</span>
		<span style="color: #808080; font-style: italic;">-- whereas the following is for the second record, and so forth!</span>
		<span style="color: #990099; font-weight: bold;">INSERT</span> <span style="color: #990099; font-weight: bold;">INTO</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #FF00FF;">&#40;</span>Page_Title<span style="color: #000033;">,</span> Page_URL<span style="color: #000033;">,</span> Lft<span style="color: #000033;">,</span> Rgt<span style="color: #FF00FF;">&#41;</span>
					<span style="color: #990099; font-weight: bold;">VALUES</span> <span style="color: #FF00FF;">&#40;</span>PageTitle<span style="color: #000033;">,</span> PageURL<span style="color: #000033;">,</span> ParentLevel<span style="color: #000033;">,</span> <span style="color: #FF00FF;">&#40;</span>ParentLevel <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
	<span style="color: #009900;">END</span> <span style="color: #009900;">IF</span><span style="color: #000033;">;</span></pre></div></div>

<p>So, if you want to add your new breadcrumb record, simply execute this <strong>addnewbreadcrumb</strong> SP.
</li>
<li>
<h2>Removing or Deleting Breadcrumb</h2>
<p>Now let&#8217;s create another SP for deleting the certain breadcrumb based on the given <strong>PageTitle</strong> parameter. This SP only needs one parameter (PageTitle):</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`deletebreadcrumbbasedonpagetitle`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> <span style="color: #008000;">`PageTitle`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
<span style="color: #808080; font-style: italic;">-- Need one parameter (PageTitle), look at the line: WHERE  Page_Title = PageTitle;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> DeletedPageTitle <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> DeletedLft <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> DeletedRgt <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`Lft`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`Rgt`</span>
<span style="color: #990099; font-weight: bold;">INTO</span>   DeletedPageTitle<span style="color: #000033;">,</span> DeletedLft<span style="color: #000033;">,</span> DeletedRgt
<span style="color: #990099; font-weight: bold;">FROM</span>   <span style="color: #008000;">`breadcrumblinks`</span>
<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> PageTitle<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">DELETE</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span>
<span style="color: #990099; font-weight: bold;">WHERE</span> Lft <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> DeletedLft <span style="color: #CC0099; font-weight: bold;">AND</span> DeletedRgt<span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`breadcrumblinks`</span>
   <span style="color: #990099; font-weight: bold;">SET</span> Lft <span style="color: #CC0099;">=</span> <span style="color: #009900;">CASE</span> <span style="color: #009900;">WHEN</span> Lft <span style="color: #CC0099;">&gt;</span> DeletedLft <span style="color: #009900;">THEN</span>
             Lft <span style="color: #CC0099;">-</span> <span style="color: #FF00FF;">&#40;</span>DeletedRgt <span style="color: #CC0099;">-</span> DeletedLft <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span>
          <span style="color: #009900;">ELSE</span>
             Lft
          <span style="color: #009900;">END</span><span style="color: #000033;">,</span>
       Rgt <span style="color: #CC0099;">=</span> <span style="color: #009900;">CASE</span> <span style="color: #009900;">WHEN</span> Rgt <span style="color: #CC0099;">&gt;</span> DeletedLft <span style="color: #009900;">THEN</span>
             Rgt <span style="color: #CC0099;">-</span> <span style="color: #FF00FF;">&#40;</span>DeletedRgt <span style="color: #CC0099;">-</span> DeletedLft <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span>
          <span style="color: #009900;">ELSE</span>
             Rgt
          <span style="color: #009900;">END</span>
   <span style="color: #990099; font-weight: bold;">WHERE</span> Lft <span style="color: #CC0099;">&gt;</span> DeletedLft
      <span style="color: #CC0099; font-weight: bold;">OR</span> Rgt <span style="color: #CC0099;">&gt;</span> DeletedLft<span style="color: #000033;">;</span>
<span style="color: #009900;">END</span></pre></div></div>

<p>If you want to remove or delete the certain breadcrumb record, simply execute this <strong>deletebreadcrumbbasedonpagetitle</strong> SP.
</li>
<li>
<h2>Moving Breadcrumb to Another Parent</h2>
<p>So far we have already had the 2 SPs for inserting and deleting breadcrumb records. How about moving breadcrumb? Good question! Now the most important thing that we have to handle is how we can move the certain breadcrumb from the current position to the new parent. This is very important, especially if you want to move the certain breadcrumb which have so many childs beneath it. So we will create the third SP as following:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`movebreadcrumb`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> CurrentRoot <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> NewParent <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">BEGIN</span>
<span style="color: #808080; font-style: italic;">-- Need two parameters: (1) CurrentRoot, and (2) NewParent.</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> Origin_Lft <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> Origin_Rgt <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">DECLARE</span> NewParent_Rgt <span style="color: #999900; font-weight: bold;">INTEGER</span><span style="color: #000033;">;</span>
&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">`Lft`</span><span style="color: #000033;">,</span> <span style="color: #008000;">`Rgt`</span>
	<span style="color: #990099; font-weight: bold;">INTO</span> Origin_Lft<span style="color: #000033;">,</span> Origin_Rgt
	<span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span>
	<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> CurrentRoot<span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">SET</span> NewParent_Rgt <span style="color: #CC0099;">=</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">`Rgt`</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span>
	<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`Page<span style="color: #008080; font-weight: bold;">_</span>Title`</span> <span style="color: #CC0099;">=</span> NewParent<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span>
<span style="color: #990099; font-weight: bold;">UPDATE</span> <span style="color: #008000;">`breadcrumblinks`</span> 
	<span style="color: #990099; font-weight: bold;">SET</span> <span style="color: #008000;">`Lft`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">`Lft`</span> <span style="color: #CC0099;">+</span> 
	<span style="color: #009900;">CASE</span>
		<span style="color: #009900;">WHEN</span> NewParent_Rgt <span style="color: #CC0099;">&lt;</span> Origin_Lft
			<span style="color: #009900;">THEN</span> <span style="color: #009900;">CASE</span>
				<span style="color: #009900;">WHEN</span> Lft <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> Origin_Lft <span style="color: #CC0099; font-weight: bold;">AND</span> Origin_Rgt
					<span style="color: #009900;">THEN</span> NewParent_Rgt <span style="color: #CC0099;">-</span> Origin_Lft
				<span style="color: #009900;">WHEN</span> Lft <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> NewParent_Rgt	<span style="color: #CC0099; font-weight: bold;">AND</span> Origin_Lft <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
					<span style="color: #009900;">THEN</span> Origin_Rgt <span style="color: #CC0099;">-</span> Origin_Lft <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span>
				<span style="color: #009900;">ELSE</span> <span style="color: #008080;">0</span> <span style="color: #009900;">END</span>
		<span style="color: #009900;">WHEN</span> NewParent_Rgt <span style="color: #CC0099;">&gt;</span> Origin_Rgt
			<span style="color: #009900;">THEN</span> <span style="color: #009900;">CASE</span>
				<span style="color: #009900;">WHEN</span> Lft <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> Origin_Lft	<span style="color: #CC0099; font-weight: bold;">AND</span> Origin_Rgt
					<span style="color: #009900;">THEN</span> NewParent_Rgt <span style="color: #CC0099;">-</span> Origin_Rgt <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
				<span style="color: #009900;">WHEN</span> Lft <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> Origin_Rgt <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span> <span style="color: #CC0099; font-weight: bold;">AND</span> NewParent_Rgt <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
					<span style="color: #009900;">THEN</span> Origin_Lft <span style="color: #CC0099;">-</span> Origin_Rgt <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
				<span style="color: #009900;">ELSE</span> <span style="color: #008080;">0</span> <span style="color: #009900;">END</span>
			<span style="color: #009900;">ELSE</span> <span style="color: #008080;">0</span> <span style="color: #009900;">END</span><span style="color: #000033;">,</span>
	Rgt <span style="color: #CC0099;">=</span> Rgt <span style="color: #CC0099;">+</span> 
	<span style="color: #009900;">CASE</span>
		<span style="color: #009900;">WHEN</span> NewParent_Rgt <span style="color: #CC0099;">&lt;</span> Origin_Lft
			<span style="color: #009900;">THEN</span> <span style="color: #009900;">CASE</span>
		<span style="color: #009900;">WHEN</span> Rgt <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> Origin_Lft <span style="color: #CC0099; font-weight: bold;">AND</span> Origin_Rgt
			<span style="color: #009900;">THEN</span> NewParent_Rgt <span style="color: #CC0099;">-</span> Origin_Lft
		<span style="color: #009900;">WHEN</span> Rgt <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> NewParent_Rgt <span style="color: #CC0099; font-weight: bold;">AND</span> Origin_Lft <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
			<span style="color: #009900;">THEN</span> Origin_Rgt <span style="color: #CC0099;">-</span> Origin_Lft <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span>
		<span style="color: #009900;">ELSE</span> <span style="color: #008080;">0</span> <span style="color: #009900;">END</span>
		<span style="color: #009900;">WHEN</span> NewParent_Rgt <span style="color: #CC0099;">&gt;</span> Origin_Rgt
			<span style="color: #009900;">THEN</span> <span style="color: #009900;">CASE</span>
				<span style="color: #009900;">WHEN</span> Rgt <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> Origin_Lft <span style="color: #CC0099; font-weight: bold;">AND</span> Origin_Rgt
					<span style="color: #009900;">THEN</span> NewParent_Rgt <span style="color: #CC0099;">-</span> Origin_Rgt <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
				<span style="color: #009900;">WHEN</span> Rgt <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> Origin_Rgt <span style="color: #CC0099;">+</span> <span style="color: #008080;">1</span>	<span style="color: #CC0099; font-weight: bold;">AND</span> NewParent_Rgt <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
					<span style="color: #009900;">THEN</span> Origin_Lft <span style="color: #CC0099;">-</span> Origin_Rgt <span style="color: #CC0099;">-</span> <span style="color: #008080;">1</span>
				<span style="color: #009900;">ELSE</span> <span style="color: #008080;">0</span> <span style="color: #009900;">END</span>
			<span style="color: #009900;">ELSE</span> <span style="color: #008080;">0</span> <span style="color: #009900;">END</span><span style="color: #000033;">;</span>
<span style="color: #009900;">END</span></pre></div></div>

<p>You will see that this SP needs two parameter as you can see from this following line:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">PROCEDURE</span> <span style="color: #008000;">`movebreadcrumb`</span><span style="color: #FF00FF;">&#40;</span><span style="color: #990099; font-weight: bold;">IN</span> CurrentRoot <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span> <span style="color: #990099; font-weight: bold;">IN</span> NewParent <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span></pre></div></div>

<p>They are:<br />
- <strong>CurrentRoot</strong>: This is the name of the current page title that you want to move.<br />
- <strong>NewParent</strong>: This is the name of the page title where the CurrentRoot will be moved to. In other words, this is the new parent page title where the CurrentRoot will be located after the moving process is successfully done.</p>
<p>If you want to move the certain breadcrumb record, simply execute this <strong>movebreadcrumb</strong> SP.
</li>
<li>
<h2>Retrieving Breadcrumb</h2>
<p>As I mentioned above, you only need one query to get the full path of the certain given page.<br />
Here is one of the example of the code how you can retrieve the certain breadcrumb of the (e.g) &#8216;Statistik per Jam&#8217; page:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> C.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #990099; font-weight: bold;">AS</span> B<span style="color: #000033;">,</span> <span style="color: #008000;">`breadcrumblinks`</span> <span style="color: #990099; font-weight: bold;">AS</span> C
<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #FF00FF;">&#40;</span>B.Lft <span style="color: #CC0099; font-weight: bold;">BETWEEN</span> C.Lft <span style="color: #CC0099; font-weight: bold;">AND</span> C.Rgt<span style="color: #FF00FF;">&#41;</span>
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#40;</span>B.Page_Title <span style="color: #CC0099;">=</span> <span style="color: #008000;">'Statistik per Jam'</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #990099; font-weight: bold;">ORDER BY</span> C.Lft<span style="color: #000033;">;</span></pre></div></div>

</li>
</ol>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2011%2F12%2F05%2Fhow-to-create-breadcrumb-using-the-nested-set-data-model-in-mysql%2F&amp;title=How%20to%20Create%20Breadcrumb%20Using%20the%20Nested%20Set%20Data%20Model%20in%20MySQL" id="wpa2a_4"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2011/12/05/how-to-create-breadcrumb-using-the-nested-set-data-model-in-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Handle Duplicate Records by Deleting and Leaving Only One Record from Each Duplicate Records in MySQL Database</title>
		<link>http://www.openscriptsolution.com/2011/07/15/how-to-handle-duplicate-records-by-deleting-and-leaving-only-one-record-from-each-duplicate-records-in-mysql-database/</link>
		<comments>http://www.openscriptsolution.com/2011/07/15/how-to-handle-duplicate-records-by-deleting-and-leaving-only-one-record-from-each-duplicate-records-in-mysql-database/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 15:56:45 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[DISTINCT]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[duplikat]]></category>
		<category><![CDATA[hapus]]></category>
		<category><![CDATA[record]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=1350</guid>
		<description><![CDATA[Have you ever got difficulty when handling the duplicate records in a table in MySQL database, where you want to delete the records and pretty leaves one record from each duplicate records? This usually happens in the table who did not have the primary key, so that it can cause to store the duplicate records. [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever got difficulty when handling the duplicate records in a table in MySQL database, where you want to delete the records and pretty leaves one record from each duplicate records? This usually happens in the table who did not have the primary key, so that it can cause to store the duplicate records. The duplicate records are usually obtained from the data import process that was done repeatedly. Well, the following trick you can use to handle such cases. Quite easy and a little bit outsmart. The first step, you must copy the table structure that stores the duplicate records to another table name (copy table structure only, not including records). The second step, simply copy only one or distinct record from each duplicate records using a single MySQL statement to the copied/destination table. Lastly, the third step, check the results in the destination table. If everything goes smoothly, then delete the original table, and rename back the copied/destination table to the original table name. Curious? <span id="more-1350"></span></p>
<p>For example, you have a table named <strong>table1</strong> which has the structure like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mysql&gt; DESCRIBE `table1`;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| code        | char(2)     | YES  |     |         |       |
| description | varchar(50) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+</pre></div></div>

<p>and it has the duplicate records as following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mysql&gt; select * from table1;
+------+-------------+
| code | description |
+------+-------------+
| 1    | One         |
| 1    | One         |
| 2    | Two         |
| 2    | Two         |
| 3    | Three       |
| 3    | Three       |
| 3    | Three       |
| 4    | Four        |
| 4    | Four        |
| 4    | Four        |
| 4    | Four        |
| 5    | Five        |
| 5    | Five        |
| 5    | Five        |
| 5    | Five        |
| 5    | Five        |
+------+-------------+</pre></div></div>

<p>Let&#8217;s say, you want to delete and leave only one record from each the duplicate records. So the results become like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">+------+-------------+
| code | description |
+------+-------------+
| 1    | One         |
| 2    | Two         |
| 3    | Three       |
| 4    | Four        |
| 5    | Five        |
+------+-------------+</pre></div></div>

<p>So, in order to get that results, copy only the table structure of <strong>table1</strong> to (for example) <strong>table1_copy</strong> using the following SQL statement:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mysql&gt; CREATE TABLE `table1_copy` LIKE `table1`;</pre></div></div>

<p>Afterwards, copy or insert only one record of the duplicate records from <strong>table1</strong> into the <strong>table1_copy</strong> using the following SQL statement:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">INSERT INTO table1_copy
SELECT DISTINCT * FROM table1;</pre></div></div>

<p>Voila&#8230; here is the result:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mysql&gt; SELECT * FROM table1_copy;
+------+-------------+
| code | description |
+------+-------------+
| 1    | One         |
| 2    | Two         |
| 3    | Three       |
| 4    | Four        |
| 5    | Five        |
+------+-------------+</pre></div></div>

<p>Ups, don&#8217;t forget to remove the <strong>table1</strong> table (assuming all the steps above goes smoothly):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">DROP TABLE IF EXISTS `table1`;</pre></div></div>

<p>and then rename the destination table to the original one:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">RENAME TABLE `table1_copy` TO `table1`;</pre></div></div>

<p>It&#8217;s very easy, isn&#8217;t? <img src='http://www.openscriptsolution.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2011%2F07%2F15%2Fhow-to-handle-duplicate-records-by-deleting-and-leaving-only-one-record-from-each-duplicate-records-in-mysql-database%2F&amp;title=How%20to%20Handle%20Duplicate%20Records%20by%20Deleting%20and%20Leaving%20Only%20One%20Record%20from%20Each%20Duplicate%20Records%20in%20MySQL%20Database" id="wpa2a_6"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2011/07/15/how-to-handle-duplicate-records-by-deleting-and-leaving-only-one-record-from-each-duplicate-records-in-mysql-database/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Replace the Certain Character in a MySQL Table</title>
		<link>http://www.openscriptsolution.com/2010/08/09/how-to-replace-the-certain-character-in-a-mysql-table/</link>
		<comments>http://www.openscriptsolution.com/2010/08/09/how-to-replace-the-certain-character-in-a-mysql-table/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 07:08:08 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[hapus]]></category>
		<category><![CDATA[REPLACE]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[update]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=1255</guid>
		<description><![CDATA[Have you ever encountered the strange behavior while you were deleting the certain records from a table in MySQL database using an application you build, which those records cannot be deleted? Well, I have! Today, I encountered this problem. This is so weird, since I have never faced this situation before. Well, after doing some [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever encountered the strange behavior while you were deleting the certain records from a table in MySQL database using an application you build, which those records cannot be deleted? Well, I have! Today, I encountered this problem. This is so weird, since I have never faced this situation before. Well, after doing some investigations, this problem obviously caused by one of the primary key fields contains the comma (,) character. Why I am sure about this, because when I replace the comma character for one record, then I re-try to delete that record from that application, voila&#8230;. that record gets successfully deleted. But the main problem is not stop until that. The next question is: How then can I replace all comma characters from the rest of the other records immediately? Just for your information, there are thousand records that contain the comma character. It is impossible to update all the records one by one, even as a matter of fact, you can create a little program/tool in order to implement it using loop method throught the recordset? Well, here is the quick-solution to overcome it. <span id="more-1255"></span></p>
<p>MySQL has provided the great UPDATE SQL syntax for this. You can use this SQL (for instance, I want to replace the comma character inside the <strong>tablename</strong> table with the minus character that located in a field named <strong>thefield</strong>):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">UPDATE tablename SET `thefield` = REPLACE(`thefield`, ',', '-');</pre></div></div>

<p>Isn&#8217;t easy, huh? <img src='http://www.openscriptsolution.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2010%2F08%2F09%2Fhow-to-replace-the-certain-character-in-a-mysql-table%2F&amp;title=How%20to%20Replace%20the%20Certain%20Character%20in%20a%20MySQL%20Table" id="wpa2a_8"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2010/08/09/how-to-replace-the-certain-character-in-a-mysql-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Change Default Storage Engine When Creating New Table in Wampserver</title>
		<link>http://www.openscriptsolution.com/2010/04/28/how-to-change-default-storage-engine-when-creating-new-table-in-wampserver/</link>
		<comments>http://www.openscriptsolution.com/2010/04/28/how-to-change-default-storage-engine-when-creating-new-table-in-wampserver/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 14:34:19 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Apache MySQL PHP]]></category>
		<category><![CDATA[default]]></category>
		<category><![CDATA[engine type]]></category>
		<category><![CDATA[InnoDB]]></category>
		<category><![CDATA[my.ini]]></category>
		<category><![CDATA[MyISAM]]></category>
		<category><![CDATA[storage engine]]></category>
		<category><![CDATA[Wampserver]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=1161</guid>
		<description><![CDATA[Wampserver is one of the popular PHP-Apache-MySQL server for Windows operating system. If you want to create your own web server, especially in your local computer, then you should use this software. It is a free software. After using this software for a few years, I have just realized that when we create a new [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.wampserver.com" target="_blank">Wampserver</a> is one of the popular PHP-Apache-MySQL server for Windows operating system. If you want to create your own web server, especially in your local computer, then you should use this software. It is a free software. After using this software for a few years, I have just realized that when we create a new table, then the default storage engine is INNODB instead of MYISAM engine type. So here is the trick how you can change this value become the MYISAM engine type. <span id="more-1161"></span></p>
<ol>
<li>Make sure your Wampserver is already running.</li>
<li>Click your mouse&#8217;s left-button on the Wampserver icon in your systray bar, then choose <strong>MySQL</strong> -> <strong>my.ini</strong>.</li>
<li>Find this code in the my.ini file:

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;"># The default storage engine that will be used when create new tables when
default-storage-engine=INNODB</pre></div></div>

<p>then replace with this code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;"># The default storage engine that will be used when create new tables when
default-storage-engine=MYISAM</pre></div></div>

</li>
<li>Save your <strong>my.ini</strong> file.</li>
<li>Restart all services of your Wampserver.</li>
<li>Try to create a new table without define the engine type explicitely by using SQL syntax, then its engine type should be now the MyISAM.</li>
</ol>
<p><strong>Edited on September 19, 2011:</strong> If you are running Wampserver which has the MySQL version 5.5.8+ in it, you will not find that directive. So the trick for it is simply add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">default-storage-engine=MYISAM</pre></div></div>

<p>after this following line in your <strong>my.ini</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">[wampmysqld]</pre></div></div>

<p>Hope it helps! <img src='http://www.openscriptsolution.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2010%2F04%2F28%2Fhow-to-change-default-storage-engine-when-creating-new-table-in-wampserver%2F&amp;title=How%20to%20Change%20Default%20Storage%20Engine%20When%20Creating%20New%20Table%20in%20Wampserver" id="wpa2a_10"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2010/04/28/how-to-change-default-storage-engine-when-creating-new-table-in-wampserver/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Know The Duplicate Records Based on The Same Value in a Field in MySQL Database</title>
		<link>http://www.openscriptsolution.com/2009/11/28/how-to-know-the-duplicate-records-based-on-the-same-value-in-a-field-in-mysql-database/</link>
		<comments>http://www.openscriptsolution.com/2009/11/28/how-to-know-the-duplicate-records-based-on-the-same-value-in-a-field-in-mysql-database/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 08:10:38 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[record]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=921</guid>
		<description><![CDATA[Have you ever wanted to know which record has duplicate content based on the same content in a field in MySQL database? This information will be get after you select the records and display them which one have the same content by groupping them by that field. Well, here is the script how you can [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to know which record has duplicate content based on the same content in a field in MySQL database? This information will be get after you select the records and display them which one have the same content by groupping them by that field. Well, here is the script how you can do that. <span id="more-921"></span></p>
<p>Suppose we have a table named <strong>mytable</strong> which has these following records inside:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">+----+-------+
| id | name  |
+----+-------+
|  1 | One   |
|  2 | One   |
|  3 | Two   |
|  4 | Two   |
|  5 | Two   |
|  6 | Three |
|  7 | Three |
|  8 | Three |
|  9 | Three |
| 10 | Four  |
| 11 | Five  |
+----+-------+
11 rows in set (0.00 sec)</pre></div></div>

<p>and you want to display which one has the duplicate content in the <strong>name</strong> filed become like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">+-------+---------+
| name  | howmany |
+-------+---------+
| Three |       4 |
| Two   |       3 |
| One   |       2 |
+-------+---------+
3 rows in set (0.00 sec)</pre></div></div>

<p>In the other words, we will display the value in <strong>name</strong> field which have more than one records, whereas, the records which do not the duplicate content, will be avoided (in this case the record which have the value in field <strong>name</strong>-nya: <strong>Four</strong> and <strong>Five</strong>).</p>
<p>Well, here is the SQL script to do make it came true:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">SELECT name, COUNT(name) AS howmany 
FROM `my_table`
GROUP BY name 
HAVING howmany &gt; 1
ORDER BY howmany DESC;</pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2009%2F11%2F28%2Fhow-to-know-the-duplicate-records-based-on-the-same-value-in-a-field-in-mysql-database%2F&amp;title=How%20to%20Know%20The%20Duplicate%20Records%20Based%20on%20The%20Same%20Value%20in%20a%20Field%20in%20MySQL%20Database" id="wpa2a_12"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2009/11/28/how-to-know-the-duplicate-records-based-on-the-same-value-in-a-field-in-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Select Distinct And Get the Certain Characters From a Field in MySQL Database</title>
		<link>http://www.openscriptsolution.com/2009/11/27/how-to-select-distinct-and-get-the-certain-characters-from-a-field-in-mysql-database/</link>
		<comments>http://www.openscriptsolution.com/2009/11/27/how-to-select-distinct-and-get-the-certain-characters-from-a-field-in-mysql-database/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 14:09:00 +0000</pubDate>
		<dc:creator>Masino Sinaga</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[basis data]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[DISTINCT]]></category>
		<category><![CDATA[LENGTH]]></category>
		<category><![CDATA[SELECT]]></category>
		<category><![CDATA[SUBSTR]]></category>

		<guid isPermaLink="false">http://www.openscriptsolution.com/?p=917</guid>
		<description><![CDATA[Today I faced an interesting situation while getting the certain characters from a field and wanted to display them by using SELECT DISTINCT statement in MySQL database. Suppose we had thousand records in a table and we wanted to get the certain characters from the first until the certain position counted 6 characters from the [...]]]></description>
			<content:encoded><![CDATA[<p>Today I faced an interesting situation while getting the certain characters from a field and wanted to display them by using <strong>SELECT DISTINCT</strong> statement in MySQL database. Suppose we had thousand records in a table and we wanted to get the certain characters from the first until the certain position counted 6 characters from the right of all particular characters, and display them by using the SELECT DISTINCT statement. So, what would you do? Well, here is the solution I made in MySQL database. <span id="more-917"></span></p>
<p>Let&#8217;s take a following example. We have had already about six thousand records in a table named <strong>mytable</strong>. Let&#8217;s say that in one of the fields named <strong>myfield</strong> has the multiple diverse value.<br />
For example:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">JAKARTA 10120
JAKARTA 10120
JAKARTA 10246
JAKARTA 10710
JAKARTA 11721
JAKARTA 11842
JAKARTA 12210
JAKARTA 12714
JAKARTA 13220
JAKARTA 13562
JAKARTA 13587
JAKARTA 14210
BOGOR 16125
BOGOR 16219
BOGOR 16242
BOGOR 16255
TANGERANG 15124
TANGERANG 15125
TANGERANG 15233
BEKASI 17121
BEKASI 17131
BEKASI 17142
BEKASI 17145
BEKASI 17233
...</pre></div></div>

<p>then, I want to display only the unique city value become like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">JAKARTA
BOGOR
TANGERANG
BEKASI</pre></div></div>

<p>In the other words, we want to avoid the 5 digits of postcode of the city, and get the unique value of the city. So here is the SQL script how you can get that unique city value:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">SELECT DISTINCT(SUBSTR(myfield, 1, LENGTH(myfield)-6)) FROM `mytable`;</pre></div></div>

<p>This SQL tells the MySQL:<br />
&#8220;Get the unique value from the <strong>myfield</strong> field started from the first character until the sixth position counted from upon the right of the field value&#8221;.</p>
<p>You can see that we have included three functions in that SQL:</p>
<ol>
<li>
<strong>DISTINCT</strong>, this keyword has the purpose to get the unique value from the set of records which has the possibility contains of the same value.
</li>
<li>
<strong>SUBSTR</strong>, this keyword has the purpose to get the certain characters from the certain field based on from the certain started position of character until the last position that we can specify.
</li>
<li>
<strong>LENGTH</strong>, this keyword has the purpose to get how many characters that contained in a field.
</li>
</ol>
<p>That SQL script worked like a charm and had been tested in MySQL version 5.0.51b which included in the WAMPSERVER version 2.0 installation.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.openscriptsolution.com%2F2009%2F11%2F27%2Fhow-to-select-distinct-and-get-the-certain-characters-from-a-field-in-mysql-database%2F&amp;title=How%20to%20Select%20Distinct%20And%20Get%20the%20Certain%20Characters%20From%20a%20Field%20in%20MySQL%20Database" id="wpa2a_14"><img src="http://www.openscriptsolution.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.openscriptsolution.com/2009/11/27/how-to-select-distinct-and-get-the-certain-characters-from-a-field-in-mysql-database/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
