วันจันทร์ที่ 1 ธันวาคม พ.ศ. 2557

SQL



SQL คืออะไร




       SQL ย่อมาจาก structured query language คือภาษาที่ใช้ในการเขียนโปรแกรม เพื่อจัดการกับฐานข้อมูลโดยเฉพาะ เป็นภาษามาตราฐานบนระบบฐานข้อมูลเชิงสัมพันธ์และเป็นระบบเปิด (open system) หมายถึงเราสามารถใช้คำสั่ง sql กับฐานข้อมูลชนิดใดก็ได้ และ คำสั่งงานเดียวกันเมื่อสั่งงานผ่าน  ระบบฐานข้อมูลที่แตกต่างกันจะได้ ผลลัพธ์เหมือนกัน ทำให้เราสามารถเลือกใช้ฐานข้อมูล ชนิดใดก็ได้โดยไม่ติดยึดกับฐานข้อมูลใดฐานข้อมูลหนึ่ง นอกจากนี้แล้ว SQL ยังเป็นชื่อโปรแกรมฐานข้อมูล ซึ่งโปรแกรม SQL เป็นโปรแกรมฐานข้อมูลที่มีโครงสร้างของภาษาที่เข้าใจง่าย ไม่ซับซ้อน มีประสิทธิภาพการทำงานสูง สามารถทำงานที่ซับซ้อนได้โดยใช้คำสั่งเพียงไม่กี่คำสั่ง โปรแกรม SQL จึงเหมาะที่จะใช้กับระบบฐานข้อมูลเชิงสัมพันธ์ และเป็นภาษาหนึ่ง ซึ่งแบ่งการทำงานได้เป็น 4 ประเภท ดังนี้1. Select query ใช้สำหรับดึงข้อมูลที่ต้องการ2. Update query ใช้สำหรับแก้ไขข้อมูล3. Insert query ใช้สำหรับการเพิ่มข้อมูล4. Delete query ใช้สำหรับลบข้อมูลออกไป     ปัจจุบันมีซอฟต์แวร์ระบบจัดการฐานข้อมูล (DBMS ) ที่สนับสนุนการใช้คำสั่ง SQL เช่น  Oracle , DB2, MS-SQL, MS-Access นอกจากนี้ภาษา SQL ถูกนำมาใช้เขียนร่วมกับโปรแกรมภาษาต่างๆ เช่น ภาษา c/C++ , VisualBasic และ Java 
ประโยชน์ของภาษา SQL1. สร้างฐานข้อมูลและ ตาราง    2. สนับสนุนการจัดการฐานข้อมูล ซึ่งประกอบด้วย การเพิ่ม การปรับปรุง และการลบข้อมูล3. สนับสนุนการเรียกใช้หรือ ค้นหาข้อมูล        ประเภทของคำสั่งภาษา SQL1. ภาษานิยามข้อมูล(Data Definition Language : DDL) เป็นคำสั่งที่ใช้ในการสร้างฐานข้อมูล กำหนดโครงสร้างข้อมูลว่ามี  Attribute ใด ชนิดของข้อมูล รวมทั้งการเปลี่ยนแปลงตาราง และการสร้างดัชนี คำสั่ง : CREATE,DROP,ALTER2. ภาษาจัดการข้อมูล (Data Manipulation Language :DML) เป็นคำสั่งที่ใช้ในการเรียกใช้ เพิ่ม ลบ และเปลี่ยนแปลงข้อมูลในตาราง    คำสั่ง : SELECT,INSERT,UPDATE,DELETE3. ภาษาควบคุมข้อมูล (Data Control Language : DCL) เป็นคำสั่งที่ใช้ในการกำหนดสิทธิการอนุญาติ หรือ ยกเลิก การเข้าถึงฐานข้อมูล เพื่อป้องกันความปลอดภัยของฐานข้อมูล คำสั่ง : GRANT,REVOKE


ตัวอย่าง คำสั่ง SQL

1. SQL TOP 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้

Database : Microsoft Access,SQL Server

Syntax

SELECT TOP [Integer] Column1, Column2, Column3,... FROM [Table-Name] ORDER BY [Field] [ASC/DESC]



Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample1 การเลือกข้อมูลที่จำนวน Budget มากที่สุดออกมา 2 Record 

SELECT TOP 2 * FROM customer ORDER BY Budget DESC

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000

2. SQL SELECT

เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้ 

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1, Column2, Column3,... FROM [Table-Name]


Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample1 การเลือกข้อมูลที่ระบุฟิวด์

SELECT CustomerID, Name, Email FROM customer

Output 

CustomerID
Name
Email
C001
Win Weerachaiwin.weerachai@thaicreate.com
C002
John Smithjohn.smith@thaicreate.com
C003
Jame Bornjame.born@thaicreate.com
C004
Chalee Angelchalee.angel@thaicreate.com

3. SQL OR AND 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) การเชื่อมวลีสำหรับเงื่อนไขต่าง ๆ 

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] = 'Value' [AND/OR] [Field] = 'Value'


Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000 

SELECT * FROM customer WHERE CountryCode = 'US' AND Used = '100000'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000

CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000


4. SQL ORDER BY 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยจัดเรียงข้อมูลตามต้องการ

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Culumn1,Culumn2,Culumn3,... FROM [Table-Name] ORDER BY [Field] [ASC/DESC],[Field] [ASC/DESC],...
ASC = น้อยไปหามาก
DESC = มากไปหาน้อย


Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample1 การเลือกข้อมูลโดยทำการจัดเรียงจาก CustomerID น้อยไปหามาก หรือ มากไปหาน้อย 

SELECT * FROM customer ORDER BY CustomerID ASC
หรือ
SELECT * FROM customer ORDER BY CustomerID DESC
หรือ
SELECT * FROM customer ORDER BY CountryCode DESC,CustomerID ASC

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000

CustomerID
Name
Email
CountryCode
Budget
Used
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000

CustomerID
Name
Email
CountryCode
Budget
Used
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000

5. SQL JOIN 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเงื่อนไขการ JOIN จะกระทำเมื่อมีข้อมูลตั้งแต่ 2 Table ขึ้นไป โดยข้อมูลเหล่านั้นเป็นข้อมูลที่มีความสัมพันธ์และเชื่อมโยงกับข้อมูลหลัก

Database : MySQL,Microsoft Access,SQL Server,Oracle 

Syntax

SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1],[Table-Name2]
WHERE [Table-Name1].Column = [Table-Name2].Column


Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000

Table : audit
AuditID
CustomerID
Date
Used
1
C001
2008-07-01
100000
2
C001
2008-07-05
200000
3
C001
2008-07-10
300000
4
C002
2008-07-02
400000
5
C002
2008-07-07
100000
6
C002
2008-07-15
300000
7
C003
2008-07-20
400000
8
C003
2008-07-25
200000
9
C004
2008-07-04
100000


Sample1 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit 

SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
3
C001
2008-08-10
300000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
4
C002
2008-08-02
400000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
5
C002
2008-08-07
100000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
6
C002
2008-08-15
300000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
7
C003
2008-08-20
400000
C003
Jame Bornjame.smith@thaicreate.com
US
3000000600000
8
C003
2008-08-25
200000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000
9
C004
2008-07-04
100000


Sample2 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 

SELECT customer.*,audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'

Output 

CustomerID
Name
Email
CountryCode
Budget
Used
AuditID
CustomerID
Date
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
1
C001
2008-08-01
100000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
2
C001
2008-08-05
200000
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
3
C001
2008-08-10
300000


Sample3 การเลือกข้อมูลแบบเชื่อมตาราง customer และ audit และ CustomerID = C001 และแสดงผลเฉพาะตาราง audit 

SELECT audit.* FROM customer,audit
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'

Output 

AuditID
CustomerID
Date
Used
1
C001
2008-08-01
100000
2
C001
2008-08-05
200000
3
C001
2008-08-10
300000
ที่มา : http://www.thaicreate.com/tutorial/sql.html



ไม่มีความคิดเห็น:

แสดงความคิดเห็น