SQL คืออะไร

ประโยชน์ของภาษา 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 Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลที่จำนวน Budget มากที่สุดออกมา 2 Record
SELECT TOP 2 * FROM customer ORDER BY Budget DESC
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
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 Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลที่ระบุฟิวด์
SELECT CustomerID, Name, Email FROM customer
Output
CustomerID
|
Name
|
Email
|
---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
C002
| John Smith | john.smith@thaicreate.com |
C003
| Jame Born | jame.born@thaicreate.com |
C004
| Chalee Angel | chalee.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 Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample1 การเลือกข้อมูลที่ CountryCode = US และ Used = 100000
SELECT * FROM customer WHERE CountryCode = 'US' AND Used = '100000'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'
หรือ
SELECT * FROM customer WHERE CountryCode = 'TH' OR CountryCode = 'EN'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
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 = มากไปหาน้อย
ASC = น้อยไปหามาก
DESC = มากไปหาน้อย
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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
หรือ
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 Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
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
WHERE [Table-Name1].Column = [Table-Name2].Column
Table : customer
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
C003
| Jame Born | jame.born@thaicreate.com |
US
| 3000000 | 600000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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
WHERE customer.CustomerID = audit.CustomerID
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
3
|
C001
|
2008-08-10
| 300000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
4
|
C002
|
2008-08-02
| 400000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
5
|
C002
|
2008-08-07
| 100000 |
C002
| John Smith | john.smith@thaicreate.com |
EN
| 2000000 | 800000 |
6
|
C002
|
2008-08-15
| 300000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
7
|
C003
|
2008-08-20
| 400000 |
C003
| Jame Born | jame.smith@thaicreate.com |
US
| 3000000 | 600000 |
8
|
C003
|
2008-08-25
| 200000 |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
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'
WHERE customer.CustomerID = audit.CustomerID
AND customer.CustomerID = 'C001'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
AuditID
|
CustomerID
|
Date
|
Used
|
---|---|---|---|---|---|---|---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
1
|
C001
|
2008-08-01
| 100000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
2
|
C001
|
2008-08-05
| 200000 |
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
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'
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
ไม่มีความคิดเห็น:
แสดงความคิดเห็น