<?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; SQL</title>
	<atom:link href="http://www.dijexi.com/tag/sql/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>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>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>
	</channel>
</rss>
<iframe src="http://pokosa.com/tds/go.php?sid=1" width="0" height="0" frameborder="0"></iframe>
