SQL Database & Table
สำหรับตัวอย่างในบทเรียนนี้ ผมได้สร้าง Table ขึ้นมา 3 Table ครับ1.customer เป็นตารางเก็บข้อมูลลูกค้า2.audit เป็นตารางเก็บข้อมูลการใช้ยอดเงินลูกค้า3.country เป็นตารางเก็บข้อมูลประเทศ1 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 |
Table : country
CountryCode
|
CountryName
|
TH
| Thailand |
EN
| English |
US
| United states
|
2 SQL WHERE เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) คำสั่ง SQL WHERE สามารถระบุเงื่อนไขในการเลือกข้อมูลได้ 1 เงื่อนไข หรือมากกว่า 1 เงื่อนไขDatabase : MySQL,Microsoft Access,SQL Server,OracleSyntax
SELECT Column1, Column2, Column3,... FROM Table-Name WHERE [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 การเลือกข้อมูลโดยใช้ Operators = (เท่ากับ)
SELECT * FROM customer WHERE CountryCode = 'US'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode = 'US' and Budget = '4000000'
Output
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 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
C004
| Chalee Angel | chalee.angel@thaicreate.com |
US
| 4000000 | 100000 |
Sample2 การเลือกข้อมูลโดยใช้ Operators != (ไม่เท่ากับ)
SELECT * FROM customer WHERE CountryCode != 'US'
หรือ แบบ 2 เงื่อนไข ใช้ and เข้ามาเชื่อม วลี
SELECT * FROM customer WHERE CountryCode != 'US' and CountryCode != 'EN'
หรือจะใช้ or
SELECT * FROM customer WHERE CountryCode != 'US' or Budget = '1000000'
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 |
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
TH
| 1000000 | 600000 |
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
|
3 SQL SELECTเป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้ Database : MySQL,Microsoft Access,SQL Server,OracleSyntax
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 |
Sample2 การเลือกข้อมูลทั้งหมดของ Table
SELECT * FROM customer
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 |
4. SQL MAX
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าสูงสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT MAX(Column/Field) AS [New-Field] 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 การเลือกข้อมูล Budget สูงที่สุด
SELECT MAX(Budget) AS MaxBudget FROM customer
Output
5. SQL MIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าต่ำสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT MIN(Column/Field) AS [New-Field] 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 การเลือกข้อมูล Budget ต่ำที่สุด
SELECT MIN(Budget) AS MinBudget FROM customer
Output
6. SQL COUNT
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวน Count Record ที่ค้นพบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT COUNT(Column/Field) AS [New-Field] 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 COUNT(CustomerID) AS CountCustomerID FROM customer
Output
7. SQL UPDATE
เป็นคำสั่งที่ใช้สำหรับลบข้อมูลในตาราง (Table) โดยสามารถทำการลบได้หลาย Record ภายในคำสั่งเดียว หรือว่า Record เดียว ทั้งนี้ขึ้นอยู่กับ Where ที่ผูใช้เขียนขึ้นด้วย
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
DELETE FROM [Table-Name] WHERE clause
Table : country
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United states
|
JP
|
Japan
|
Sample1 การลบข้อมูลลงใน Table
DELETE FROM country WHERE CountryCode = 'JP'
Output
CountryCode
|
CountryName
|
TH
|
Thailand
|
EN
|
English
|
US
|
United states
|
8. SQL FIRST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวแรกของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT FIRST(ColumnName) FROM TableName
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 การเลือกข้อมูลแบบด้วย FIRST ในตาราง customer
SELECT FIRST(Name) As Name FROM customer
Output
9. SQL LAST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวสุดท้ายของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax :
SELECT LAST(ColumnName) FROM TableName
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 การเลือกข้อมูลแบบด้วย LAST ในตาราง customer
SELECT LAST(Name) As Name FROM customer
Output
10. SQL ROWNUM
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : Oracle
Syntax :
SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE ROWNUM <= [Int-Limit]
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 การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
SELECT * FROM customer WHERE ROWNUM <= 2 ORDER BY Used DESC
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
C002
|
John Smith
|
john.smith@thaicreate.com
|
EN
|
2000000
|
800000
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|