ภาษา
SQL (สามารถอ่านออกเสียงได้ 2 แบบ คือ “เอสคิวแอล” (SQL)
หรือ “ซีเควล” (Sequel) ย่อมมาจาก Structured
Query Language หรือภาษาในการสอบถามข้อมูล เป็นภาษาทางด้านฐานข้อมูล
ที่สมารถสร้างและปฏิบัติการกับฐานข้อมูลแบบสัมพันธ์ (Relational Database) โดยเฉพาะ และเป็นภาษาที่มีลักษณะคลายกับภาษาอังกฤษ ภาษา SQL ถูกพัฒนาขึ้นจากแนวคิดของ Relational Calculus และ Relational
Algebra เป็นหลัก ภาษา SQL เริ่มพัฒนาครั้งแรกโดย
Almaden Research Center ของบริษัท IBM โดยมีชื่อเริ่มแรกว่า “ซีเควล” (Sequel) ต่อมาได้เปลี่ยนชื่อเป็น
“เอสคิวแอล” (SQL)
หลังจากนั้นภาษาSQL ได้ถูกนำมาพัฒนาโดยผู้ผลิตซอฟต์แวร์ด้านระบบจัดการฐานข้อมูลเชิงสัมพันธ์จนเป็นที่นิยมกันอย่างแพร่หลายในปัจจุบัน
โดยผู้ผลิตแต่ละรายก็พยายามที่จะพัฒนาระบบจัดการฐานข้อมูลของตนให้มีลักษณะเด่นเฉพาะขึ้นมา
ทำให้รูปแบบการใช้คำสั่ง SQL มีรูปแบบที่แตกต่างกันไปบ้าง เช่น
Oracle Access SQL Base ของ Sybase Ingres หรือ SQL Server ของ Microsoft เป็นต้น ดังนั้นในปี ค.ศ. 1986 ทางด้าน American
National Standards Institute (ANSI) จึงได้กำหนดมาตรฐานของ SQL
ขึ้น อย่างไรก็ดี โปรแกรมฐานข้อมูลที่ขายในท้องตลาด ได้ขยาย SQL
ออกไปจนเกินข้อกำหนดของ ANSI โดยเพิ่มคุณสมบัติอื่น
ๆ ที่คิดว่าเป็นประโยชน์เข้าไปอีก แต่โดยหลักทั่วไปแล้วก็ยังปฏิบัติตามมาตรฐานของ ANSI
ในการอธิบายคำสั่งต่าง ๆ ของภาษาSQL
วันเสาร์ที่ 13 ธันวาคม พ.ศ. 2557
1. SQL SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง
(Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง
หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name]
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
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
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
|
2. SQL LIMIT
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง
(Table) ที่สามารถกำหนดจำนวน Record ที่แสดงผลออกมาได้
Database : MySQL
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] ORDER BY [Fields] [ASC/DESC] LIMIT [Int-Start] , [Int-End]
Database : MySQL
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] ORDER BY [Fields] [ASC/DESC] LIMIT [Int-Start] , [Int-End]
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 ORDER BY Used DESC LIMIT 0,2
SELECT * FROM customer ORDER BY Used DESC LIMIT 0,2
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
|
3. SQL INSERT
เป็นคำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในตาราง
(Table) โดยสามารถเพิ่มได้ทั้งแถวหรือว่าเพิ่มในส่วนของแต่ละฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
INSERT INTO [Table-Name] (Column1,Column2,Column3,...) VALUES ('Value1','Value2','Value3',...)
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
INSERT INTO [Table-Name] (Column1,Column2,Column3,...) VALUES ('Value1','Value2','Value3',...)
Table : country
|
CountryCode
|
CountryName
|
|
TH
|
|
|
EN
|
English
|
|
US
|
|
Sample1 การเพิ่มข้อมูลลงใน Table
INSERT INTO country VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')
INSERT INTO country VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')
Output
|
CountryCode
|
CountryName
|
|
TH
|
|
|
EN
|
English
|
|
US
|
|
|
CH
|
Chaina
|
4. SQL GROUP BY
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง
(Table) โดยใช้หาผลรวมของคอลัมน์จากแถวใน Column ที่ระบุและทำการรวม
Group ภายใต้ Column ที่อยู่หลัง GROUP
BY
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column,SUM(Column) FROM [Table-Name] GROUP BY Column
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column,SUM(Column) FROM [Table-Name] GROUP BY 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
|
Sample1 การเลือกข้อมูลผลรวมของ Budget โดยแบ่ง
Group ตาม CountryCode
SELECT CountryCode,SUM(Budget) AS SumBudget FROM customer GROUP BY CountryCode
SELECT CountryCode,SUM(Budget) AS SumBudget FROM customer GROUP BY CountryCode
Output
|
CountryCode
|
SumBudget
|
|
EN
|
2000000
|
|
TH
|
1000000
|
|
US
|
7000000
|
5. SQL MAX
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง
(Table) โดยหาค่าสูงสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT MAX(Column/Field) AS [New-Field] FROM [Table-Name]
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
SELECT MAX(Budget) AS MaxBudget FROM customer
Output
|
MaxBudget
|
|
4000000
|
6. SQL COPY TABLE (CREATE TABLE... SELECT...)
เป็นคำสั่งที่ใช้สำหรับสร้างตารางใหม่
โดยทำการ COPY/CREATE TABLE และข้อมูลจากตารางที่มีอยู่แล้ว
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
CREATE TABLE [Table-Name] SELECT * FROM [Table-Name] WHERE ....
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
CREATE TABLE [Table-Name] SELECT * FROM [Table-Name] WHERE ....
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 การเพิ่มข้อมูลลงใน Table customer2 โดยการ SELECT จาก customer
CREATE TABLE customer2 SELECT * FROM customer
CREATE TABLE customer2 SELECT * FROM customer
Output (Table : customer2)
|
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
|
7. SQL FIRST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง
(Table) โดยเลือกข้อมูล แถวแรกของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT FIRST(ColumnName) FROM TableName
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
SELECT FIRST(Name) As Name FROM customer
Output
|
Name
|
|
Weerachai
Nukitram
|
8. SQL EXISTS
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขโดยทำการตรวจสอบ
ข้อมูลจากอีกตารางหนึ่งว่ามีข้อมูล หรือว่าไม่มีข้อมูลที่ต้องการเปรียบเทียบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT * FROM TableName1 WHERE [NOT] EXISTS (SELECT * FROM TableName2)
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT * FROM TableName1 WHERE [NOT] EXISTS (SELECT * FROM TableName2)
Table :
customer1
|
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 : customer2
|
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
|
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
Sample1 การเลือกข้อมูลจากตาราง customer1 โดยข้อมูลจะเปรียบเทียบในตาราง customer2 ว่ามีข้อมูลเหมือนกันหรือไม่
SELECT * FROM customer1 WHERE EXISTS (SELECT * FROM customer2)
SELECT * FROM customer1 WHERE EXISTS (SELECT * FROM customer2)
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
|
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
ส่วนการใช้ NOT EXISTS จะเป็นตรงกันข้าม กันเงื่อนไขนี้
สมัครสมาชิก:
บทความ (Atom)