<?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>Welcome to XP Link Co., Ltd. &#187; SQL</title>
	<atom:link href="http://www.xp-link.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xp-link.com</link>
	<description>XP Link Co., Ltd is a software consultant company specializes in Java Technology from Thailand.</description>
	<lastBuildDate>Tue, 10 Jan 2012 09:11:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>เพิ่มความสามารถในการ Cache ให้ Hibernate ด้วย EhCache</title>
		<link>http://www.xp-link.com/2011/08/30/hibernate-caching-with-ehcache/</link>
		<comments>http://www.xp-link.com/2011/08/30/hibernate-caching-with-ehcache/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 11:06:29 +0000</pubDate>
		<dc:creator>nuttarut</dc:creator>
				<category><![CDATA[Spring Framework]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.xp-link.com/?p=511</guid>
		<description><![CDATA[ในการใช้งานฐานข้อมูล (Database) โดยทั่วไปนั้นจะมีข้อมูลบางส่วนที่มีการเปลี่ยนแปลงไม่บ่อยนัก หรือไม่มีการเปลี่ยนแปลงเลย แต่ฐานข้อมูลก็ต้องมีการทำงานทุกครั้งที่มีการเข้าถึงข้อมูลเหล่านี้ ทำให้อาจเสียเวลาในการประมวลผลไปโดยเปล่าประโยชน์ หรือในกรณีของระบบที่มีการทำงานหนักมากก็อาจจะทำให้ระบบขัดข้องเนื่องจากฐานข้อมูลไม่สามารถตอบสนองต่อการเข้าถึงข้อมูลได้ทันเวลา ปัญหานี้สามารถแก้ไขได้โดยวิธีที่เรียกว่า Cache ซึ่งนำข้อมูลที่ไม่ค่อยมีการเปลี่ยนแปลงเหล่านี้มาเก็บไว้ในหน่อยความจำ เพื่อที่เมื่อมีการเข้าถึงข้อมูลชุดเดิมก็ไม่ต้องไปค้นหาในฐานข้อมูลอีกครั้ง แต่มาดึงไปใช้จากหน่วยความจำแทน ซึ่งใช้เวลาน้อยกว่าและไม่เพิ่มภาระให้ฐานข้อมูลโดยไม่จำเป็น ข้างต้นคือแนวคิดในภาพรวม ส่วนรายละเอียดและขั้นตอนในการใช้ Cache นั้นก็ขึ้นกับเทคโนโลยีในระบบที่เราเลือกใช้ สำหรับในบทความนี้เราจะมาดูว่าถ้าต้องการใช้ Cache ในระบบที่พัฒนาด้วย Java และใช้เฟรมเวิร์คอย่าง Spring และ Hibernate นั้นควรจะต้องพิจรณาอะไรบ้าง ควรใช้อะไรในการทำ Cache? สำหรับ Hibernate ซึ่งเป็นเฟรมเวิร์คที่ช่วยจัดการในเรื่องฐานข้อมูลนั้นก็มีตัวเลือกในการใช้ Cache มากมาย แต่ตัวเลือกที่น่าจะเหมาะสมที่สุดก็คงไม่พ้น EhCache เนื่องจากสามารถใช้งานร่วมกับทั้ง Hibernate แล้ว Spring ได้อย่างไม่มีปัญหา (หรือมีน้อยมาก) และทีมพัฒนาของทั้ง Hibernate และ EhCache ก็ยังทำงานร่วมกันเพื่อรับประกันว่าเฟรมเวิร์คทั้งสองจะสามารถใช้งานร่วมกันได้อย่างราบรื่น ไม่เกิดปัญหาความเข้ากันไม่ได้ ข้อมูลส่วนใดบ้างที่ควรทำ Cache ก่อนอื่นต้องทำความเข้าใจก่อนว่า Hibernate นั้นจะมี Cache อยู่ 2 [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.xp-link.com/website/wp-content/uploads/2011/08/graph7.jpg" alt="" width="300" height="231" class="aligncenter size-full wp-image-544" /><br />
<br />
ในการใช้งานฐานข้อมูล (Database) โดยทั่วไปนั้นจะมีข้อมูลบางส่วนที่มีการเปลี่ยนแปลงไม่บ่อยนัก หรือไม่มีการเปลี่ยนแปลงเลย แต่ฐานข้อมูลก็ต้องมีการทำงานทุกครั้งที่มีการเข้าถึงข้อมูลเหล่านี้ ทำให้อาจเสียเวลาในการประมวลผลไปโดยเปล่าประโยชน์ หรือในกรณีของระบบที่มีการทำงานหนักมากก็อาจจะทำให้ระบบขัดข้องเนื่องจากฐานข้อมูลไม่สามารถตอบสนองต่อการเข้าถึงข้อมูลได้ทันเวลา<br />
<span id="more-511"></span><br />
<br />
ปัญหานี้สามารถแก้ไขได้โดยวิธีที่เรียกว่า Cache ซึ่งนำข้อมูลที่ไม่ค่อยมีการเปลี่ยนแปลงเหล่านี้มาเก็บไว้ในหน่อยความจำ เพื่อที่เมื่อมีการเข้าถึงข้อมูลชุดเดิมก็ไม่ต้องไปค้นหาในฐานข้อมูลอีกครั้ง แต่มาดึงไปใช้จากหน่วยความจำแทน ซึ่งใช้เวลาน้อยกว่าและไม่เพิ่มภาระให้ฐานข้อมูลโดยไม่จำเป็น<br />
<br />
ข้างต้นคือแนวคิดในภาพรวม ส่วนรายละเอียดและขั้นตอนในการใช้ Cache นั้นก็ขึ้นกับเทคโนโลยีในระบบที่เราเลือกใช้ สำหรับในบทความนี้เราจะมาดูว่าถ้าต้องการใช้ Cache ในระบบที่พัฒนาด้วย Java และใช้เฟรมเวิร์คอย่าง Spring และ Hibernate นั้นควรจะต้องพิจรณาอะไรบ้าง<br />
<br />
<strong>ควรใช้อะไรในการทำ Cache?</strong><br />
<br />
สำหรับ Hibernate ซึ่งเป็นเฟรมเวิร์คที่ช่วยจัดการในเรื่องฐานข้อมูลนั้นก็มีตัวเลือกในการใช้ Cache มากมาย แต่ตัวเลือกที่น่าจะเหมาะสมที่สุดก็คงไม่พ้น EhCache เนื่องจากสามารถใช้งานร่วมกับทั้ง Hibernate แล้ว Spring ได้อย่างไม่มีปัญหา (หรือมีน้อยมาก) และทีมพัฒนาของทั้ง Hibernate และ EhCache ก็ยังทำงานร่วมกันเพื่อรับประกันว่าเฟรมเวิร์คทั้งสองจะสามารถใช้งานร่วมกันได้อย่างราบรื่น ไม่เกิดปัญหาความเข้ากันไม่ได้<br />
<br />
<strong>ข้อมูลส่วนใดบ้างที่ควรทำ Cache</strong><br />
<br />
ก่อนอื่นต้องทำความเข้าใจก่อนว่า Hibernate นั้นจะมี Cache อยู่ 2 ประเภทด้วยกันคือ</p>
<ol>
<li>Second Level Cache (L2 Cache) &#8211; ใช้เก็บ Entity โดยใช้ id เป็นคีย์หลักในการตรวจสอบข้อมูล</li>
<li>Query Cache &#8211; ใช้เก็บผลที่ได้จาก Query โดยจะใช้ SQL Statement เป็นคีย์หลัก</li>
</ol>
<p>โดย Second Level Cache (หรือ L2 Cache) นั้นยังแบ่งประเภทของข้อมูลเป็นอีก 3 ประเภทคือ</p>
<ol>
<li>READ_ONLY &#8211; ข้อมูลที่ไม่มีการเปลี่ยนแปลง</li>
<li>NONSTRICT_READ_WRITE &#8211; ข้อมูลที่การเปลี่ยนแปลงไม่บ่อยมาก</li>
<li>READ_WRITE &#8211; ข้อมูลที่มีการเปลี่ยนแปลงตลอดเวลา</li>
</ol>
<p>จะเห็นได้ว่านอกจากข้อมูลที่ไม่ค่อยมีการเปลี่ยนแปลงแล้ว เราก็สามารถใช้ Cache กับข้อมูลที่มีการเปลี่ยนแปลงบ่อยได้ด้วย แต่เนื่องจากเมื่อข้อมูลมีการเปลี่ยนแปลงแล้วจะต้องมีการปรับปรุงข้อมูลที่อยู่ใน Cache ด้วย ทำให้มีการทำงานเพิ่มมากขึ้น ฉะนั้นข้อมูลที่มีอัตราส่วนการเขียนมากกว่าการอ่าน หรือข้อมูลประเภท Transaction จึงไม่เหมาะสมที่จะใช้การ Cache<br />
<br />
ส่วน Query Cache นั้นจะเก็บผลลัพธ์ที่ได้จาก SQL Statement เอาไว้ เมื่อมีการเรียกเรียกใช้ SQL Statement เดิมก็จะนำผลลัพธ์ที่เก็บไว้มาใช้งาน จึงเหมาะกับ Query ที่ตรงไปตรงมาหรือมีเงื่อนไขไม่มากนัก เช่น ข้อมูลประเทศ หรือหมวดหมู่สินค้า ซึ่งมีโอกาสที่จะถูกเรียกซ้ำบ่อย สำหรับ Query ประเภทที่เงื่อนไขเยอะ เช่น การค้นหาแบบละเอียด อาจจะไม่เหมาะที่จะใช้ Cache เท่าไหร เพราะมีโอกาสถูกเรียกใช้ซ้ำน้อย ทำให้เปลืองพื้นที่ในหน่วยความจำ<br />
<br />
ข้อมูลเพิ่มเติม &#8211; <a href="http://www.ehcache.org/documentation/user-guide/hibernate" target="_blank">ehcache.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.xp-link.com/2011/08/30/hibernate-caching-with-ehcache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HSQL (HypersonicSQL)</title>
		<link>http://www.xp-link.com/2009/02/23/hsql-hypersonicsql/</link>
		<comments>http://www.xp-link.com/2009/02/23/hsql-hypersonicsql/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 09:18:54 +0000</pubDate>
		<dc:creator>Thipbodee</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.xp-link.com/2009/02/23/hsql-hypersonicsql/</guid>
		<description><![CDATA[ HSQL เป็น Database Server เป็นอีกตัวเลือกหนึ่งของ Java Developer เหมาะสำหรับการทำงานเกี่ยวกับการ testing และ de- ployment of database applications ด้วยขนาดที่เล็กและทำงานได้ รวดเร็ว ประโยชน์ HSQL มีขนาดเล็ก HSQLใช้ทรัพยากรบนเครื่องน้อย HSQL เหมาะสำหรับผู้ที่ต้องการทดลองการทำงานที่ใช้งาน Database HSQLสามารถเลือกmodeในการใช้งาน Database เช่นการเก็บข้อมูล ใน Memory ,การเก็บข้อมูลลงบนFile HSQL เป็น Open Source ที่ให้ใช้ฟรีสามารถใช้การค้าได้ HSQL สามารถใช้งานร่วมกันกับ Hibernate ข้อกำหนด HSQLสามารถใช้งานQueryได้ตามปกติแต่ผลลัพธ์ที่สามารถแสดงออกมานั้นจะถูกกำหนดโดยขนาดMemoryของเครื่องที่มีอยู่ HSQL สามารถสร้าง Tableได้อย่างไม่จำกัดจำนวนแต่จำกัดที่จำนวนMemoryของเครื่อง HSQL ทำงานบนJRE 1.4 or later HSQL ใช้ Syntax เดี่ยวกับภาษาSQL Standard HSQL สามารถ [...] ]]></description>
			<content:encoded><![CDATA[<p> HSQL เป็น Database Server เป็นอีกตัวเลือกหนึ่งของ Java<br />
Developer เหมาะสำหรับการทำงานเกี่ยวกับการ  testing และ de-<br />
ployment of database applications  ด้วยขนาดที่เล็กและทำงานได้<br />
รวดเร็ว</p>
<h4>ประโยชน์</h4>
<ul>
<li> HSQL มีขนาดเล็ก</li>
<li> HSQLใช้ทรัพยากรบนเครื่องน้อย</li>
<li> HSQL เหมาะสำหรับผู้ที่ต้องการทดลองการทำงานที่ใช้งาน Database</li>
<li> HSQLสามารถเลือกmodeในการใช้งาน Database เช่นการเก็บข้อมูล ใน Memory ,การเก็บข้อมูลลงบนFile</li>
<li> HSQL เป็น Open Source ที่ให้ใช้ฟรีสามารถใช้การค้าได้</li>
<li> HSQL สามารถใช้งานร่วมกันกับ Hibernate</li>
</ul>
<p><span id="more-39"></span></p>
<h4>ข้อกำหนด</h4>
<ul>
<li>HSQLสามารถใช้งานQueryได้ตามปกติแต่ผลลัพธ์ที่สามารถแสดงออกมานั้นจะถูกกำหนดโดยขนาดMemoryของเครื่องที่มีอยู่</li>
<li> HSQL  สามารถสร้าง Tableได้อย่างไม่จำกัดจำนวนแต่จำกัดที่จำนวนMemoryของเครื่อง</li>
<li>HSQL ทำงานบนJRE 1.4 or later</li>
<li> HSQL ใช้ Syntax เดี่ยวกับภาษาSQL Standard</li>
<li>HSQL  สามารถ Join Table ได้ 41Table ในการเก็บข้อมูลแบบ Self Test 4000000 row ใช้ เนื้อที่ 400MB</li>
</ul>
<h4>Tool ที่มากับHSQL</h4>
<p>ภายในPackage ที่เราได้ทำการดาวโหลดมานั้นมี tool ที่ใช้ในการทำงานมาให้ด้วย เราสามารถใช้คำสัง<br />
java -cp ../lib/hsqldb.jar org.hsqldb.util.DatabaseManager ในการเปิดใช้toolที่ติดมาด้วย</p>
<h4>วิธีการติดตั้ง</h4>
<ul>
<li> ทำการ Download Fileที่ http://hsqldb.org/</li>
<li> ทำการแตกfile zip ออกมาจะได้ folder hsqldb ออกมา</li>
<li>ภายในจะมีFolder hsqlad จะมี folder lib อยู่ ซึ่งข้างใน มี hsqldb.jarนำfile นี้ไปใส่ในโปรเจ็คที่ใช้ เท่านี้ก็สามารถ เรียกการทำงานของHSQLได้แล้ว</li>
<li>การใช้จาวาติดต่อเบื้องต้น
<pre class="code">Class.forName("org.hsqldb.jdbcDriver" );

    Connection c = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "sa", "");
<ul>
<li>	การใช้ Stand Alone Mode
Connection c = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "sa", "");</li>
<li>	การใช้งานบน Memory Mode
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa", "");</li>
</ul>
</pre>
</li>
<p>การใช้งาน HSQL ร่วมกับHibernateและSpring<br />
ในการใช้Hibernate ในการติดต่อHSQLมีขั้นตอนง่ายๆดังนี้</p>
<li>ทำการconfig file   hibernateSpring.xml</li>
<li>ทำการconfig  dialectให้เป็นของHSQL
<pre class="code">		&lt;property name="hibernateProperties"&gt;
			&lt;props&gt;
				&lt;prop key="hibernate.dialect"&gt;
					org.hibernate.dialect.HSQLDialect
				&lt;/prop&gt;
				&lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;
				&lt;prop key="hibernaye.use_sql_comments"&gt;false&lt;/prop&gt;
			&lt;/props&gt;
		&lt;/property&gt;</pre>
</li>
<li> ทำการconfig serverปลายทาง
<pre class="code">	&lt;bean id="dataSource"
	class="org.springframework.jdbc.datasource.SingleConnectionDataSource"&gt;
		&lt;property name="driverClassName"&gt;
			&lt;value&gt;org.hsqldb.jdbcDriver&lt;/value&gt;
		true&lt;/value&gt;
		&lt;/property&gt;
	&lt;/bean&gt;</pre>
</li>
<li> hsqldb.jarมาวางไว้ในlibของโปเจค<br />
เท่านั้นเราก็จะสามารถใช้Hibernate ในการติดต่อไปยังDatabase<br />
HSQLที่เราใช้ในการทดสอบได้แล้ว</li>
</ul>
<h4>ข้อควรระวังในการเปลี่ยนจาก Oracle Databaseมาเป็นHSQL</h4>
<ul>
<li>ชนิดของData Typeที่ไม่ตรงกันเมื่อทำการexport sql commandออกมา<br />
จะไม่สามารถใช้command ที่ออกมานั้นได้เลยต้องทำการแก้ไขก่อน</li>
</ul>
<table class="xpress" border="0">
<tbody>
<tr>
<th>คำสั่งที่ใช้งาน</th>
<th>Oracle</th>
<th>HSQL</th>
</tr>
<tr>
<td>Data type</td>
<td>Number</td>
<td>Integer</td>
</tr>
<tr>
<td>Data type</td>
<td>Varchar2</td>
<td>Varchar</td>
</tr>
<tr>
<td>Add Date</td>
<td>to_date(&#8217;2007-05-08&#8242;,&#8217;RRRR-MM-DD&#8217;)</td>
<td>&#8217;2008-05-08&#8242;</td>
</tr>
<tr>
<td>not-null</td>
<td>NOT NULL ENABLE</td>
<td>NOT NULL</td>
</tr>
<tr>
<td>การComment</td>
<td>REM&#8230;</td>
<td>#&#8230;&#8230;</td>
</tr>
<tr>
<td>การทำsequence</td>
<td>Sequence</td>
<td>AUTO_INCREMENT</td>
</tr>
</tbody>
</table>
<ul>
<li> SQL Syntaxอื่นๆ</li>
<p>ดูตัวอย่างได้ที่ http://hsqldb.org/doc/guide/ch09.html</p>
<li> หากมีข้อส่งสัยหรือต้องการข้อมูลเพิ่มเติมสามารถหาดูได้ที่
<p>http://www.hsqldb.org/</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.xp-link.com/2009/02/23/hsql-hypersonicsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to SQL #2</title>
		<link>http://www.xp-link.com/2007/07/19/introduction-to-sql-2/</link>
		<comments>http://www.xp-link.com/2007/07/19/introduction-to-sql-2/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 05:22:22 +0000</pubDate>
		<dc:creator>nuntaporn</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.xp-link.com/2007/07/19/introduction-to-sql-2/</guid>
		<description><![CDATA[ Manipulating Data Command Description INSERT Adds a new row to the table UPDATE Modifies existing rows from the table DELETE Removes existing rows from the table COMMIT Makes all pending changes permanent ROLLBACK Discards all pending data changes Insert Inserting new rows with NULL values INSER INTO s_dept VALUES ( 100,'Developer', NULL); Inserting special values [...] ]]></description>
			<content:encoded><![CDATA[<h2>Manipulating Data</h2>
<table class="xpress">
<tr>
<th >Command</th>
<th >Description</th>
</tr>
<tr>
<td >INSERT</td>
<td>Adds a new row to the table</td>
</tr>
<tr>
<td >UPDATE</td>
<td>Modifies existing rows from the table</td>
</tr>
<tr>
<td >DELETE</td>
<td>Removes existing rows from the table</td>
</tr>
<tr>
<td >COMMIT</td>
<td>Makes all pending changes permanent</td>
</tr>
<tr>
<td >ROLLBACK</td>
<td>Discards all pending data changes</td>
</tr>
</table>
<h3>Insert</h3>
<p><span id="more-28"></span></p>
<ul>
<li>Inserting new rows with NULL values 
<pre class="code">
INSER INTO s_dept
VALUES ( 100,'Developer', NULL);
</pre>
</li>
<li>Inserting special values 
<pre class="code">
INSER INTO s_dept(id,first_name, user_id, start_date)
VALUES ( 13,'Somchai', USER, SYSDATE);
</pre>
</li>
<li>
<ul>
<li>The USER function records the current user name.</li>
<li>The SYSDATE function records the current date and time.</li>
</ul>
</li>
<li>Inserting specific date and time values</li>
<li>
<ul>
<li>TO_DATE function
<pre class="code">
	INSER INTO s_dept(id,first_name, user_id, start_date)
	VALUES ( 13,'Somchai', USER, TO_DATE('01/02/2007', 'DD/MM/YYYY'));
	</pre>
</li>
<li>Default century is the current century.</li>
<li>Default time is midnight.</li>
</ul>
</li>
<li>Copying rows from another table
<pre class="code">
INSER INTO history(id,first_name, salary, start_date)
SELECT  id,first_name, salary, start_date
FROM s_dept
WHERE salary &lt; 100000;
</pre>
</li>
</ul>
<h3>Update</h3>
<pre class="code">
UPDATE s_dept
SET dept_id = 31, salary= 20000
where id = 1;
</pre>
<h3>Delete</h3>
<pre class="code">
DELETE s_dept
where salary &lt; 2000;
</pre>
<h2>Data Modeling and Database Design</h2>
<h4>Entity Relationship Model</h4>
<h3>Entity</h3>
<ul>
<li>A thing of significance about which information needs to be known</li>
<li>Examples : Employees, customers</li>
</ul>
<h3>Attribute</h3>
<ul>
<li>Something that describes or qualifies an entity</li>
<li>Examples : name, id</li>
</ul>
<h3>Relationship</h3>
<ul>
<li>An association between two entities</li>
<li>Examples : customer and product.</li>
</ul>
<h4>Relationship Types</h4>
<h3>One-to-one</h3>
<ul>
<li>Example : Husband and wife</li>
</ul>
<h3>Many-to-one</h3>
<ul>
<li>Example : Passenger and plane.</li>
</ul>
<h3>Many-to-many</h3>
<ul>
<li>Example : employees and skill.</li>
</ul>
<h4>Primary Key</h4>
<ul>
<li>No duplicate value and cannot be NULL.</li>
<li>Each row is uniquely identified by a column or set of columns(composite  primary key).</li>
</ul>
<h4>Foreign Key</h4>
<ul>
<li>A column or combination of columns in one table that refers to a PK or unique key (UK) in the same table or in another table.</li>
<li>The value must either match the value in the related column or be NULL.</li>
<li>If an FK is part of a PK, then it cannot be NULL.</li>
</ul>
<h4>Indexes</h4>
<h3>What is an index?</h3>
<ul>
<li>An index is a database structure that provides quick lookup of data in a column or columns of a table.</li>
</ul>
<h3>When is an Index Used?</h3>
<ul>
<li>To speed up access to rows in a table.</li>
</ul>
<h3>Guidelines to create an index</h3>
<ul>
<li>The column is used frequently in the WHERE clause or in a join condition.</li>
<li>The column contains a wide range of values.</li>
<li>The column contains a large number of null values.</li>
<li>Two or more columns are frequently used together in a WHERE clause or a join condition.</li>
<li>The table is large and most queries are expected to retrieve less than 2-4% of the rows.</li>
<li>More indexes do not always speed up queries.</li>
<li>Do not create an index if </li>
<li>
<ul>
<li>The table is small.</li>
<li>The columns are not often used as a condition in the query.</li>
<li>Most queries are expected retrieve more than 2-4% of the rows.</li>
<li>The table is update frequently.</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.xp-link.com/2007/07/19/introduction-to-sql-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to SQL</title>
		<link>http://www.xp-link.com/2007/07/17/introduction-to-sql/</link>
		<comments>http://www.xp-link.com/2007/07/17/introduction-to-sql/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 07:56:51 +0000</pubDate>
		<dc:creator>nuntaporn</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.xp-link.com/2007/07/17/introduction-to-sql/</guid>
		<description><![CDATA[ Introduction SQL:Structured Query Language SQL is a standard computer language for accessing and manipulating databases. SQL commands Data retrieval : SELECT Data manipulation language(DML) : INSERT, UPDATE, DELETE Data definition language(DDL) : CREATE, ALTER, DROP, RENAME, TRUNCATE Transaction control : COMMIT, ROLLBACK, SAVEPOINT Data control language(DCL) : GRANT, REVOKE Data retrieval Selecting Rows The basic [...] ]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<h3>SQL:Structured Query Language</h3>
<p>SQL is a standard computer language for accessing and manipulating databases.</p>
<h3>SQL commands</h3>
<ul>
<li>Data retrieval : SELECT </li>
<li>Data manipulation language(DML) : INSERT, UPDATE, DELETE</li>
<li>Data definition language(DDL) : CREATE, ALTER, DROP, RENAME, TRUNCATE</li>
<li>Transaction control : COMMIT, ROLLBACK, SAVEPOINT</li>
<li>Data control language(DCL) : GRANT, REVOKE</li>
</ul>
<h2>Data retrieval</h2>
<h3>Selecting Rows</h3>
<p><span id="more-27"></span></p>
<ul>
<li>The basic query block
<pre class="code">
SELECT [DISTINCT] {*,column [alias], ...}
FROM  table;
</pre>
</li>
<li>Selecting all columns, all rows
<pre class="code">
SELECT *
FROM  table_exam;
</pre>
</li>
<li>Selecting specific columns
<pre class="code">
SELECT col1, col2
FROM  table_exam;
</pre>
</li>
<li>Create expressions on NUMBER and DATE datatypes by using operators +, -, *, /
<pre class="code">
SELECT salary * 12 , start_date +7
FROM  table_salary;
</pre>
</li>
<li>NVL Function</li>
<li>
<ul>
<li>Convert NULL to an actual value with NVL.</li>
<li>Datatypes must match.</li>
</ul>
<pre class="code">
SELECT salary * NVL(commission , 0)/100 ?Comm?
FROM  table_salary;
</pre>
</li>
<li>DISTINCT</li>
<li>
<ul>
<li>Eliminate duplicate rows by using DISTINCT in the SELECT</li>
</ul>
<pre class="code">
SELECT DISTINCT name
FROM  table_salary;
</pre>
</li>
<li>Limiting Selected Rows</li>
<li>
<ul>
<li>Sort row output using the ORDER BY clause.</li>
<li>Enter search criteria using the WHERE clause.</li>
</ul>
</li>
<li>The ORDER BY clause</li>
<li>
<ul>
<li>ASC ? ascending order, default.</li>
<li>DESC ? descending order.
<pre class="code">
	SELECT Name , start_date
	FROM  table_salary
	ORDER BY start_date DESC;
	</pre>
</li>
<li>ORDER BY clause is the last in SELECT command.</li>
<li>Null values are displayed
<ul>
<li>Last for ascending sequences.</li>
<li>First for descending sequences.</li>
</ul>
</li>
<li>You can order by position to save time.<br/>
<pre class="code">
	SELECT Name , start_date
	FROM  table_salary
	ORDER BY 2;
	</pre>
</li>
<li>You can sort by a column that is not in the SELECT list.<br/>
<pre class="code">
	SELECT Name , start_date
	FROM  table_salary
	ORDER BY salary DESC;
	</pre>
</li>
</ul>
</li>
<li>Comparison and Logical Operators
<ul>
<li>Logical comparison operators : = , &gt; , &gt;= , &lt; ,  &lt;= </li>
<li>SQL comparison operator
<ul>
<li>BETWEEN &#8230; AND&#8230;</li>
<li>IN(list)</li>
<li>LIKE</li>
<li>IS NULL</li>
</ul>
</li>
<li>Logical operators
<ul>
<li>AND</li>
<li>OR</li>
<li>NOT</li>
</ul>
</li>
</ul>
</li>
<li>Negative Expressions
<ul>
<li>Logical Operators : != , &lt;&gt; , ^=</li>
<li>SQL Operators
<ul>
<li>NOT BETWEEN</li>
<li>NOT IN</li>
<li>NOT LIKE</li>
<li>IS NOT NULL</li>
</ul>
</li>
</ul>
</li>
<li>Character Functions
<ul>
<li>LOWER : Converts to lowercase</li>
<li>UPPER : Converts to uppercase</li>
<li>INTCAP : Converts to Initial capitalization</li>
<li>CONCAT : Concatenates values</li>
<li>SUBSRT : Returns substring</li>
<li>LENGTH : Returns number of characters</li>
<li>NVL	 : Converts a null value</li>
</ul>
</li>
<li>Number Functions
<ul>
<li>ROUND : 	Rounds value to specified decimal</li>
<li>TRUNC	 : Truncates value to specified decimal</li>
<li>MOD : Returns remainder of division</li>
</ul>
</li>
<li>Date Functions
<ul>
<li>MONTHS_BETWEEN : Number of months between two dates</li>
<li>ADD_MONTHS : Add calendar months to date</li>
<li>NEXT_DAY : Next day of the date specified</li>
<li>LAST_DAY : Last day of the month</li>
<li>ROUND	 : Round to date at midnight</li>
<li>TRUNC	: Remove time portion from date</li>
</ul>
</li>
<li>Join
<ul>
<li>Equijoin
<pre class="code">
	SELECT e.Name , s.salary
	FROM  table_salary s , table_employee e
	WHERE s.id = e.emp_id;
	</pre>
</li>
<li>Non-Equijoins
<pre class="code">
	SELECT e.name , s.salary
	FROM  table_salary s , table_employee e
	WHERE e.salary BETWEEN s.max_salary AND s.min_salary;
	</pre>
</li>
<li>Outer Joins
<pre class="code">
	SELECT e.id, e.name , c.name
	FROM  table_customer c , table_employee e
	WHERE e.id(+) = c.sale_id
	ORDER BY e.id;
	</pre>
<p>Place the operator on the side of the join where there is no value to join to.</p>
</li>
<li>Self Joins
<pre class="code">
	SELECT worker.last_name||' works for '||manager.last_name
	FROM  s_emp worker, s_emp manager
	WHERE worker.manager_id  = manager.id;
	</pre>
</li>
</ul>
</li>
<li>Group Functions
<ul>
<li>COUNT Function
<p>Count(*) returns the number of rows in a table.</p>
<pre class="code">
	SELECT COUNT(*)
	FROM  s_emp
	WHERE dept_id = 31;
	</pre>
<p>Count(expr) returns the number of non-null rows</p>
<pre class="code">
	SELECT COUNT(commisstion_pct)
	FROM  s_emp
	WHERE dept_id = 31;
	</pre>
</li>
<li>Group by clause
<pre class="code">
	SELECT credit_rating, COUNT(*) ?#Cust?
	FROM  s_customer
	GROUP BY credit_rating ;
	</pre>
<pre class="code">
	SELECT title, SUM(salary) PAYROLL
	FROM  s_emp
	WHERE title NOT LIKE 'VP%'
	GROUP BY title
	ORDER BY SUM(salary);
	</pre>
</li>
<li>The HAVING Clause
<pre class="code">
	SELECT title, SUM(salary) PAYROLL
	FROM  s_emp
	WHERE title NOT LIKE 'VP%'
	GROUP BY title
	HAVING SUM(salary) > 5000
	ORDER BY SUM(salary);
	</pre>
</li>
</ul>
</li>
<li>Subquery
<ul>
<li>Single Row subqueries
<pre class="code">
	SELECT last_name, title
	FROM  s_emp
	WHERE title =
	( SELECT title
	FROM s_emp
	WHERE last_name = 'Smith');
	</pre>
</li>
<li>Multiple Row Subqueries
<pre class="code">
	SELECT last_name, first_name, title
	FROM  s_emp
	WHERE dept_id IN
	   (SELECT ID
		FROM s_dept
		WHERE name = 'Finance'
		OR region_id = 2 );
	</pre>
</li>
<li>Having Clause with subaueries
<pre class="code">
	SELECT dept_id, AVG(salary)
	FROM  s_emp
	GROUP dept_id
	HAVING AVG(salary) >
	  (SELECT AVG(salary)
		FROM s_emp
		WHERE dept_id = 32);
	</pre>
</li>
<li>Exists condition
<pre class="code">
	SELECT *
	FROM s_dept d
	WHERE EXISTS
	  (select 'T'
		from s_order o
		where d.dept_id = r.dept_id);
	</pre>
<pre class="code">
	SELECT *
	FROM s_dept d
	WHERE NOT EXISTS
	  (select 'T'
		from s_order o
		where d.dept_id = r.dept_id);
	</pre>
</li>
</ul>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.xp-link.com/2007/07/17/introduction-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

