วันเสาร์ที่ 28 กุมภาพันธ์ พ.ศ. 2558

MySQL

MySQL (มายเอสคิวแอล)         เป็นระบบจัดการฐานข้อมูลเชิงสัมพันธ์ (Relational Database Management System) โดยใช้ภาษา SQL แม้ว่า MySQL เป็นซอฟต์แวร์โอเพนซอร์ส แต่แตกต่างจากซอฟต์แวร์โอเพนซอร์สทั่วไป โดยมีการพัฒนาภายใต้บริษัท MySQL AB ในประเทศสวีเดน โดยจัดการMySQL ทั้งในแบบที่ให้ใช้ฟรี และแบบที่ใช้ในเชิงธุรกิจ
MySQL สร้างขึ้นโดยชาวสวีเดน 2 คน และชาวฟินแลนด์ ชื่อ David Axmark, Allan Larsson และMichael "Monty" Widenius.ปัจจุบันบริษัทซันไมโครซิสเต็มส์ (Sun Microsystems, Inc.) เข้าซื้อกิจการของMySQL AB เรียบร้อยแล้ว ฉะนั้นผลิตภัณฑ์ภายใต้ MySQL AB ทั้งหมดจะตกเป็นของซัน

ชื่อ "MySQL" อ่านออกเสียงว่า "มายเอสคิวเอล" (maɪˌɛskjuːˈɛl)  หรือ "มายเอสคิวแอล" (ในการอ่านอักษร ในภาษาไทย) ซึ่งทางซอฟต์แวร์ไม่ได้อ่าน มายซีเควล หรือ มายซีควล เหมือนกับซอฟต์แวร์จัดการฐานข้อมูลตัวอื่น

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

คำสั่ง SQL

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


Sample การเลือกข้อมูลที่ระบุฟิวด์
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




SQL ALIAS 

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

Database : MySQL

Syntax

SELECT Column1 AS Alias1,Column2 AS Alias2,Column3 AS Alias3,... FROM [Table-Name1] Table Alias


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


Sample การเลือกข้อมูลตาราง customer โดยทำการ Alias เปลี่ยนชื่อฟิวด์ขึ้นมาใหม่
SELECT CustomerID AS CusID,Name AS CusName,Email AS CusEmail FROM customer

Output
CusID
CusName
CusEmail
C001
Win Weerachai
win.weerachai@thaicreate.com
C002
John Smith
john.smith@thaicreate.com
C003
Jame Born
jame.smith@thaicreate.com
C004
Chalee Angel
chalee.angel@thaicreate.com



  

SQL OR AND 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (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 [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


Sample การเลือกข้อมูลที่ 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 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





SQL BETWEEN 

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] BETWEEN [Value-Start] AND [Value-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


Sample การเลือกข้อมูลที่ Budget ที่มีค่าตั้งแต่ 1000000 - 3000000
SELECT * FROM customer WHERE Budget BETWEEN '1000000' AND '3000000'

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





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


Sample การเลือกข้อมูลที่จำนวน 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





SQL DISTINCT

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT DISTINCT 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


Sample การเลือกข้อมูล CountryCode ที่ไม่ซ้ำกัน
SELECT DISTINCT CountryCode FROM customer

Output
CountryCode
TH
EN
US





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


Sample การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 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





SQL NOT LIKE 

เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Tableโดยทำการค้นหาข้อความที่ระบุภายในฟิวด์ที่กำหนด และไม่แสดง Record ที่ค้นพบ ซึ่งทำหน้าที่ตรงข้ามกับ LIKE

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Filed] NOT 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


Sample การเลือกข้อมูลตารางที่ฟิวด์ Name มีไม่มีคำว่า ee อยู่
SELECT * FROM customer WHERE Name NOT LIKE '%ee%'

Output
CustomerID
Name
Email
CountryCode
Budget
Used
C002
John Smith
john.smith@thaicreate.com
EN
2000000
800000
C003
Jame Born
jame.smith@thaicreate.com
US
3000000
600000





SQL INSERT 

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

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

INSERT INTO [Table-Name] (Column1,Column2,Column3,...) VALUES ('Value1','Value2','Value3',...)


Table : country
CountryCode
CountryName
TH
Thailand
EN
English
US
United states


Sample การเพิ่มข้อมูลลงใน Table
INSERT INTO country VALUES ('CH','Chaina')
หรือ
INSERT INTO country (CountryCode,CountryName) VALUES ('CH','Chaina')

Output
CountryCode
CountryName
TH
Thailand
EN
English
US
United states
CH
Chaina





SQL LIMIT 

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

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


Sample การเลือกข้อมูลที่มีการใช้ยอดเงินมากที่สุดจำนวน 2 Record
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



ที่มา : http://www.thaicreate.com/tutorial/sql.html