วันอังคารที่ 20 มกราคม พ.ศ. 2558
วันจันทร์ที่ 1 ธันวาคม พ.ศ. 2557
รู้จักกับภาษา SQL ? SQL หรือ Structured Query Language
รู้จักกับภาษา SQL ? SQL หรือ Structured Query Language
คือภาษาที่ใช้ในการเขียนโปรแกรม เพื่อจัดการกับฐานข้อมูลโดยเฉพาะ เป็นภาษามาตราฐานบนระบบฐานข้อมูลเชิงสัมพันธ์และเป็นระบบเปิด (open system) หมายถึงเราสามารถใช้คำสั่ง sql กับฐานข้อมูลชนิดใดก็ได้ และ คำสั่งงานเดียวกันเมื่อสั่งงานผ่าน ระบบฐานข้อมูลที่แตกต่างกันจะได้ ผลลัพธ์เหมือนกัน ทำให้เราสามารถเลือกใช้ฐานข้อมูล ชนิดใดก็ได้โดยไม่ติดยึดกับฐานข้อมูลใดฐานข้อมูลหนึ่ง นอกจากนี้แล้ว SQL ยังเป็นชื่อโปรแกรมฐานข้อมูล ซึ่งโปรแกรม SQL เป็นโปรแกรมฐานข้อมูลที่มีโครงสร้างของภาษาที่เข้าใจง่าย ไม่ซับซ้อน มีประสิทธิภาพการทำงานสูง สามารถทำงานที่ซับซ้อนได้โดยใช้คำสั่งเพียงไม่กี่คำสั่ง โปรแกรม SQL จึงเหมาะที่จะใช้กับระบบฐานข้อมูลเชิงสัมพันธ์ และเป็นภาษาหนึ่ง ซึ่งแบ่งการทำงานได้เป็น 4 ประเภท ดังนี้
1. Select query ใช้สำหรับดึงข้อมูลที่ต้องการ
2. Update query ใช้สำหรับแก้ไขข้อมูล
3. Insert query ใช้สำหรับการเพิ่มข้อมูล
4. Delete query ใช้สำหรับลบข้อมูลออกไป
เป็นภาษาที่ใช้ในการติดต่อกับฐานข้อมูลหรือพูดอีกอย่างก็คือ
เป็นภาษาที่ใช้ในการสั่งให้ฐานฐานข้อมูลกระทำการใด ๆ ตามคำสั่งที่เราสั่ง
ซึ่งในการติดต่อฐานข้อมูลนั้น ไม่ว่าจะเป็น SQL Server
, Microsoft Access , MySQL ,DB2 หรือแม้แต่ Oracle ก็จะต้องใช้คำสั่งภาษา SQL ในการควบคุมทั้งสิ้น
และเราจะมาเรียนรู้ถึงคำสั่งพื้นฐาน ของ SQL ที่จำเป็นกัน
โดยส่วนใหญ่แล้วการใช้คำสั่ง SQL เพื่อติดต่อฐานข้อมูลนั้น
จะใช้โดยหลักคือ 3 กรณี คือ
1. การเรียกดู
2. การแก้ไข ลบ, เพิ่ม, เปลี่ยนแปลง
3. การสร้างขึ้นใหม่
รูปแบบคำสั่ง SQL และการใช้งานภาษา SQL ในรูปแบบต่าง ๆ
SQL SELECT
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Syntax
SELECT Column1, Column2, Column3,... FROM
[Table-Name]
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
|
.......................................................................................................
Table : customer
Sample1 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า ee อยู่
Output
Table : customer
Sample1 การเลือกข้อมูลผลรวมของ Budget
Output
SQL LIKE
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT
Column1,Column2,Column3,... FROM [Table-Name] WHERE [Filed] LIKE '%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 การเลือกข้อมูลตารางที่ฟิวด์ Name มีคำว่า ee อยู่
SELECT * FROM customer
WHERE Name LIKE '%ee%'
Output
CustomerID
|
Name
|
Email
|
CountryCode
|
Budget
|
Used
|
C001
|
Win Weerachai
|
win.weerachai@thaicreate.com
|
TH
|
1000000
|
600000
|
C004
|
Chalee Angel
|
chalee.angel@thaicreate.com
|
US
|
4000000
|
100000
|
.......................................................................................................
SQL SUM
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าผลรวมของฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าผลรวมของฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT SUM(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 SUM(Budget)
AS SumBudget FROM customer
Output
SumBudget
|
10000000
|
.......................................................................................................
SQL LENGTH
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับข้อความในตำแหน่งที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับข้อความในตำแหน่งที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT LENGTH(Name)
As MyLength FROM customer
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 การเลือกข้อมูลโดยนับความยาวในฟิวด์ Name
SELECT LENGTH(Name)
AS MyLength FROM customer
Output
MyLength
|
13
|
11
|
9
|
12
|
.......................................................................................................
SQL MID
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดคำใน Colomn หรือ Field ที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดคำใน Colomn หรือ Field ที่ต้องการ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT
MID(column_name,start[,length]) 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 เลือกข้อมูล Column ชือ่ Name ออกมา 4 ตัวอักษร
SELECT MID(Name,1,4) As
Name FROM customer
Output
Name
|
Win
|
John
|
Jame
|
Chal
|
.......................................................................................................
SQL LEN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวนตัวอักษร
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการนับจำนวนตัวอักษร
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT LEN(ColumnName)
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 เลือกข้อมูล Column ชื่อ Name โดยทำการนับจำนวนตัวอักษร
SELECT Name, LEN(Name)
As LenName FROM customer
Output
Name
|
LenName
|
win weerachai
|
13
|
john smith
|
10
|
jame born
|
9
|
chalee angel
|
12
|
.......................................................................................................
SQL COPY TABLE (CREATE
TABLE... SELECT...)
เป็นคำสั่งที่ใช้สำหรับสร้างตารางใหม่ โดยทำการ COPY/CREATE TABLE และข้อมูลจากตารางที่มีอยู่แล้ว
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับสร้างตารางใหม่ โดยทำการ COPY/CREATE TABLE และข้อมูลจากตารางที่มีอยู่แล้ว
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
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
|
.......................................................................................................
SQL MIN
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าต่ำสุดในฟิวด์
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
MinBudget
|
1000000
|
.......................................................................................................
Table : customer1
Sample1 การรวมค่า Used แล้วสร้างชื่อ Column ใหม่ชื่อว่า Total
Output
Table : customer
Sample1 การเลือกข้อมูลแบบด้วย FIRST ในตาราง customer
Output
SQL AS
เป็นคำสั่งที่ใช้สำหรับการเลือกข้อมูลโดยทำการสร้าง Alias Name (Column) ขึ้นมาใหม่
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการเลือกข้อมูลโดยทำการสร้าง Alias Name (Column) ขึ้นมาใหม่
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT Column As
NewColumn FROM Table1
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
|
Sample1 การรวมค่า Used แล้วสร้างชื่อ Column ใหม่ชื่อว่า Total
SELECT SUM(Used) As
Total FROM customer
Output
Total
|
2100000
|
.......................................................................................................
SQL FIRST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวแรกของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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
Name
|
Weerachai Nukitram
|
สมัครสมาชิก:
บทความ (Atom)