sql database
TRANSCRIPT
-
http://www.thaicreate.com nattawut 1
SQL Database & Table SQL ? SQL Structured Query Language SQL Server , Microsoft Access , MySQL ,DB2 Oracle SQL SQL (Field) (Record)
SQL 3 1. 2. , , 3. Table 3 Table 1.customer 2.audit 3.country Table : Customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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
-
http://www.thaicreate.com nattawut 2
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
SQL WHERE
CustomerID
(Table) SQL WHERE 1 1 Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column1, Column2, Column3,... FROM Table-Name WHERE [Field] = 'Value' Table : customer
Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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 [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
CustomerID Name Email CountryCode Budget Used
C004 Chalee Angel [email protected] US 4000000 100000
-
http://www.thaicreate.com nattawut 3
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 [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000 CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
SQL ALIAS
CustomerID
(Table) ALIAS Field Table Database : MySQL Syntax
SELECT Column1 AS Alias1,Column2 AS Alias2,Column3 AS Alias3,... FROM [Table-Name1] Table Alias Table : customer
Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 customer Alias
SELECT CustomerID AS CusID,Name AS CusName,Email AS CusEmail FROM customer
-
http://www.thaicreate.com nattawut 4
Output
CusID CusName CusEmail C001 Win Weerachai [email protected] C002 John Smith [email protected] C003 Jame Born [email protected] C004 Chalee Angel [email protected]
Sample2 customer,audit Alias Table
SELECT X.*,Y.* FROM customer X LEFT JOIN audit Y ON X.CustomerID = Y.CustomerID WHERE X.CustomerID = 'C001' Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-
01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-
05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-
10 300000
Sample3 customer Alias Table
SELECT X.CustomerID,X.Name FROM customer X Output
CusID CusName C001 Win Weerachai C002 John Smith C003 Jame Born C004 Chalee Angel
SQL OR AND
CustomerID
(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
Name Email CountryCode Budget Used
-
http://www.thaicreate.com nattawut 5
C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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' Output
CustomerID Name Email CountryCode Budget Used C004 Chalee Angel [email protected] US 4000000 100000
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
SQL ORDER BY
CustomerID
(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 = Table : customer
Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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 Output
CustomerID Name Email CountryCode Budget Used C001 Win [email protected] TH 1000000 600000
-
http://www.thaicreate.com nattawut 6
Weerachai C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
CustomerID Name Email CountryCode Budget Used
C004 Chalee Angel [email protected] US 4000000 100000 C003 Jame Born [email protected] US 3000000 600000 C002 John Smith [email protected] EN 2000000 800000
C001 Win Weerachai [email protected] TH 1000000 600000
CustomerID Name Email CountryCode Budget Used
C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
SQL SUB SELECT QUERY (Table) SELECT SUB SELECT QUERY JOIN TABLE SUB SELECT JOIN TABLE Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column1,Column2,Column3,... FROM [Table-Name] WHERE [Field] IN (SELECT ..... FROM ....) Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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
-
http://www.thaicreate.com nattawut 7
Sample1 customer audit 400000
SELECT * FROM customer WHERE CustomerID IN (SELECT CustomerID FROM audit WHERE Used >= '400000') Output
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
SQL SELECT INTO (Table) Copy Table Backup Table Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column1,Column2,Column3,... INTO [New-Table] FROM [Table-Name] Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 customer customer_backup
SELECT * INTO customer_backup FROM customer Output Table : customer_backup
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
SQL BETWEEN (Table)
-
http://www.thaicreate.com nattawut 8
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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Budget 1000000 - 3000000
SELECT * FROM customer WHERE Budget BETWEEN '1000000' AND '3000000' Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
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 Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Table : audit AuditID CustomerID Date Used
1 C001 2008-07-01 100000 2 C001 2008-07-05 200000
-
http://www.thaicreate.com nattawut 9
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 Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-10 300000
C002 John Smith [email protected] EN 2000000 800000 4 C002 2008-08-02 400000
C002 John Smith [email protected] EN 2000000 800000 5 C002 2008-08-07 100000
C002 John Smith [email protected] EN 2000000 800000 6 C002 2008-08-15 300000
C003 Jame Born [email protected] US 3000000 600000 7 C003 2008-08-20 400000
C003 Jame Born [email protected] US 3000000 600000 8 C003 2008-08-25 200000
C004 Chalee Angel [email protected] 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' Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-10 300000
Sample3 customer audit CustomerID = C001 audit
-
http://www.thaicreate.com nattawut 10
SELECT audit.* FROM customer,audit 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
SQL OUTER JOIN (Table) OUTER JOIN Table Table Table Table Table Table Database : 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 Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
C006 Superman Return [email protected] US 2000000 0
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
10 C005 2008-07-04 200000
Sample1 customer audit
SELECT customer.*,audit.* FROM customer,audit WHERE customer.CustomerID (+)= audit.CustomerID
-
http://www.thaicreate.com nattawut 11
Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-10 300000
C002 John Smith [email protected] EN 2000000 800000 4 C002 2008-08-02 400000
C002 John Smith [email protected] EN 2000000 800000 5 C002 2008-08-07 100000
C002 John Smith [email protected] EN 2000000 800000 6 C002 2008-08-15 300000
C003 Jame Born [email protected] US 3000000 600000 7 C003 2008-08-20 400000
C003 Jame Born [email protected] US 3000000 600000 8 C003 2008-08-25 200000
C004 Chalee Angel [email protected] 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' Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] 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' 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 nattawut 12
SQL RIGHT JOIN (Table) RIGHT JOIN Table Table Table Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1] RIGHT JOIN [Table-Name2] ON [Table-Name1].Column = [Table-Name2].Column Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
C006 Superman Return [email protected] US 2000000 0
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
10 C005 2008-07-04 200000
Sample1 RIGHT JOIN customer audit
SELECT customer.*,audit.* FROM customer RIGHT JOIN audit ON customer.CustomerID = audit.CustomerID Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-10 300000
C002 John Smith [email protected] EN 2000000 800000 4 C002 2008-08-02 400000
C002 John Smith [email protected] EN 2000000 800000 5 C002 2008-08-07 100000
-
http://www.thaicreate.com nattawut 13
C002 John Smith [email protected] EN 2000000 800000 6 C002 2008-08-15 300000
C003 Jame Born [email protected] US 3000000 600000 7 C003 2008-08-20 400000
C003 Jame Born [email protected] US 3000000 600000 8 C003 2008-08-25 200000
C004 Chalee Angel [email protected] US 4000000 100000 9 C004 2008-07-04 100000
NULL NULL NULL NULL NULL NULL 10 C005 2008-07-04 200000
Sample2 RIGHT JOIN customer audit CustomerID = C005
SELECT customer.*,audit.* FROM customer RIGHT JOIN audit ON customer.CustomerID = audit.CustomerID WHERE audit.CustomerID = 'C005' Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
NULL NULL NULL NULL NULL NULL 10 C005 2008-07-04 200000
Sample3 RIGHT JOIN customer audit CustomerID = C005 audit
SELECT audit.* FROM customer RIGHT JOIN audit ON customer.CustomerID = audit.CustomerID WHERE audit.CustomerID = 'C005' Output
AuditID CustomerID Date Used 10 C005 2008-08-07 200000
SQL LEFT JOIN (Table) LEFT JOIN Table Table Table Table Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT [Table-Name1].Column1, [Table-Name2].Column1,... FROM [Table-Name1] LEFT JOIN [Table-Name2] ON [Table-Name1].Column = [Table-Name2].Column Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000
-
http://www.thaicreate.com nattawut 14
C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
C006 Superman Return [email protected] US 2000000 0
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
10 C005 2008-07-04 200000
Sample1 LEFT JOIN customer audit
SELECT customer.*,audit.* FROM customer LEFT JOIN audit ON customer.CustomerID = audit.CustomerID Output
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-10 300000
C002 John Smith [email protected] EN 2000000 800000 4 C002 2008-08-02 400000
C002 John Smith [email protected] EN 2000000 800000 5 C002 2008-08-07 100000
C002 John Smith [email protected] EN 2000000 800000 6 C002 2008-08-15 300000
C003 Jame Born [email protected] US 3000000 600000 7 C003 2008-08-20 400000
C003 Jame Born [email protected] US 3000000 600000 8 C003 2008-08-25 200000
C004 Chalee Angel [email protected] US 4000000 100000 9 C004 2008-07-04 100000
C006 Superman Return [email protected] US 2000000 0 NULL NULL NULL NULL
Sample2 LEFT JOIN customer audit CustomerID = C001
SELECT customer.*,audit.* FROM customer LEFT JOIN audit ON customer.CustomerID = audit.CustomerID WHERE customer.CustomerID = 'C001' Output
-
http://www.thaicreate.com nattawut 15
CustomerID Name Email CountryCode Budget Used AuditID CustomerID Date Used
C001 Win Weerachai [email protected] TH 1000000 600000 1 C001 2008-08-01 100000
C001 Win Weerachai [email protected] TH 1000000 600000 2 C001 2008-08-05 200000
C001 Win Weerachai [email protected] TH 1000000 600000 3 C001 2008-08-10 300000
Sample3 LEFT JOIN customer audit CustomerID = C001 audit
SELECT audit.* FROM customer LEFT JOIN audit ON customer.CustomerID = audit.CustomerID WHERE 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
SQL UNION Query Table Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column1,Column2,... FROM [Table-Name] UNION SELECT Column1,Column2,... FROM [Table-Name] ... Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Table : country
CountryCode CountryName TH Thailand EN English US United states
Sample1 customer country
-
http://www.thaicreate.com nattawut 16
SELECT CustomerID,Name FROM customer UNION SELECT CountryCode,CountryName FROM country Output
CustomerID Name C001 Win Weerachai C002 John Smith C003 Jame Born C004 Chalee Angel TH Thailand EN English US United states
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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 CountryCode
SELECT DISTINCT CountryCode FROM customer Output
CountryCode TH EN US
SQL TOP (Table) Record
-
http://www.thaicreate.com nattawut 17
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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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 [email protected] US 4000000 100000 C003 Jame Born [email protected] US 3000000 600000
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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 2 Record
SELECT * FROM customer ORDER BY Used DESC LIMIT 0,2 Output
-
http://www.thaicreate.com nattawut 18
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000
C001 Win Weerachai [email protected] TH 1000000 600000
SQL ROWNUM (Table) Record Database : Oracle Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE ROWNUM
-
http://www.thaicreate.com nattawut 19
C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 2 Record
SELECT * FROM customer ORDER BY RAND() LIMIT 2 Output
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
SQL LIKE (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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Name ee
SELECT * FROM customer WHERE Name LIKE '%ee%' Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C004 Chalee Angel [email protected] US 4000000 100000 Sample2 Email j
SELECT * FROM customer WHERE Name LIKE 'j%' Output
-
http://www.thaicreate.com nattawut 20
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
Sample3 Name i
SELECT * FROM customer WHERE Name LIKE '%i' Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] 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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Name ee
SELECT * FROM customer WHERE Name NOT LIKE '%ee%' Output
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
Sample2 Email j
SELECT * FROM customer WHERE Name NOT LIKE 'j%' Output
CustomerID Name Email CountryCode Budget Used
-
http://www.thaicreate.com nattawut 21
C001 Win Weerachai [email protected] TH 1000000 600000
C004 Chalee Angel [email protected] US 4000000 100000 Sample3 Name i
SELECT * FROM customer WHERE Name NOT LIKE '%i' Output
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
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
Sample1 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 UPDATE (Table) Record
-
http://www.thaicreate.com nattawut 22
1 Where Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
UPDATE [Table-Name] SET Column1='Value1',Column2='Value2',... WHERE clause Table : country
CountryCode CountryName TH Thailand EN English US United states CH Chaina
Sample1 Table
UPDATE country SET CountryCode = 'JP',CountryName='Japan' WHERE CountryCode = 'CH' Output
CountryCode CountryName TH Thailand EN English US United states JP Japan
SQL INSERT ... SELECT (Table) SELECT Table () Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
INSERT INTO [Table-Name] (Column1,Column2,Column3,...) SELECT (Column1,Column2,Column3,...) WHERE ... Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Table : customer2
CustomerID Name Email CountryCode Budget Used
-
http://www.thaicreate.com nattawut 23
Sample1 Table customer2 SELECT customer
INSERT INTO customer2 (CustomerID,Name,Email,CountryCode,Budget,Used) SELECT CustomerID,Name,Email,CountryCode,Budget,Used FROM customer WHERE CountryCode = 'TH' Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
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
SQL IN (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle
-
http://www.thaicreate.com nattawut 24
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE [Field] IN ('Value1','Value2') Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 CustomerID = C002 C003
SELECT * FROM customer WHERE CustomerID IN ('C002','C003') Output
CustomerID Name Email CountryCode Budget Used C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
SQL NOT IN (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name] WHERE [Field] NOT IN ('Value1','Value2') Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 CustomerID = C002 C003
SELECT * FROM customer WHERE CustomerID NOT IN ('C002','C003') Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
-
http://www.thaicreate.com nattawut 25
C004 Chalee Angel [email protected] US 4000000 100000 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 Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Budget Group CountryCode
SELECT CountryCode,SUM(Budget) AS SumBudget FROM customer GROUP BY CountryCode Output
CountryCode SumBudget EN 2000000 TH 1000000 US 7000000
SQL HAVING (Table) HAVING GROUP BY Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT Column,AVG(Column) FROM [Table-Name] GROUP BY Column HAVING AVG(Column) [Condition] Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
-
http://www.thaicreate.com nattawut 26
Sample1 Budget Budget CompanyCode >= 2000000
SELECT CountryCode,AVG(Budget) AS AvgBudget FROM customer GROUP BY CountryCode HAVING AVG(Budget) >= '2000000' Output
CountryCode AvgBudget EN 2000000 US 3500000
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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1
SELECT COUNT(CustomerID) AS CountCustomerID FROM customer Output
CountCustomerID 4
SQL MAX (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT MAX(Column/Field) AS [New-Field] FROM [Table-Name] Table : customer
-
http://www.thaicreate.com nattawut 27
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Budget
SELECT MAX(Budget) AS MaxBudget FROM customer Output
MaxBudget 4000000
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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Budget
SELECT MIN(Budget) AS MinBudget FROM customer Output
MinBudget 1000000
SQL REPLACE (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle
-
http://www.thaicreate.com nattawut 28
Syntax
SELECT REPLACE(Field,Search,Replace) AS [New-Field] FROM [Table-Name] Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Win Mr.Win
SELECT REPLACE(Name, 'Win', 'Mr.Win') AS MyName FROM customer Output
MyName Mr.Win Weerachai
John Smith Jame Born
Chalee Angel REPLACE WHERE SQL SUM (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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Budget
SELECT SUM(Budget) AS SumBudget FROM customer Output
-
http://www.thaicreate.com nattawut 29
SumBudget 10000000
SQL AVG (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT AVG(Column/Field) AS [New-Field] FROM [Table-Name] Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Budget
SELECT AVG(Budget) AS AvgBudget FROM customer Output
AvgBudget 2500000
SQL CONCAT (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT CONCAT(Culumn1,Culumn2,...) AS [New-Field] FROM [Table-Name] Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 CustomerID,Name,Email
-
http://www.thaicreate.com nattawut 30
SELECT CustomerID,CONCAT(CustomerID,Name,Email) AS ConcatField FROM customer Output
CustomerID ConcatField C001 C001Win [email protected] C002 C002John [email protected] C003 C003Jame [email protected] C004 C004Chalee [email protected]
SQL LENGTH (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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Name
SELECT LENGTH(Name) AS MyLength FROM customer Output
MyLength 13 11 9 12
SQL TRIM (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
-
http://www.thaicreate.com nattawut 31
TRIM(' My String ') = 'MyString' LTRIM(' My String ') = 'MyString ' RTRIM(' My String ') = ' MyString' Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1
SELECT Trim(Name) AS MyString FROM customer Output
MyString Win Weerachai
John Smith Jame Born
Chalee Angel SQL SUBSTRING (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT SUBSTR(Name,0,2) As MySubStr FROM customer Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1 1 3
SELECT SUBSTR(Name,1,3) AS MySubStr FROM customer Output
MySubStr Win Joh
-
http://www.thaicreate.com nattawut 32
Jam Cha
Sample2 3 3
SELECT SUBSTR(Name,-3,3) AS MySubStr FROM customer Output
MySubStr hai ith orn gel
SQL DROP,TRUNCATE Clear Table Database : MySQL Syntax
TRUNCATE customer // Clear Table DROP TABLE customer // Drop or Delete Table Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample1
TRUNCATE customer Sample2
DROP TABLE customer SQL INSERT DUPLICATE KEY UPDATE UPDATE Record Insert Ref http://www.thaicreate.com/community/mysql-insert-duplicate-key-update.html
-
http://www.thaicreate.com nattawut 33
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 .... Table : customer
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] 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 [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
SQL CREATE TABLE (Create New Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
CREATE TABLE "TableName" ( ColumnName1 DataType , ColumnName2 DataType , ... ) Sample1 customer CustomerID,Name,Email,CountryCode,Budget,Used
CREATE TABLE customer ( CustomerID varchar(4) NOT NULL, Name varchar(50) NOT NULL,
-
http://www.thaicreate.com nattawut 34
Email varchar(50) NOT NULL, CountryCode varchar(2) NOT NULL, Budget double NOT NULL, Used double NOT NULL ) Output
CustomerID Name Email CountryCode Budget Used SQL PRIMARY KEYS Primary Keys Key Table Primary Keys 1 Table 1 Primary Keys Rows Primary Keys Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
CREATE TABLE "TableName" ( ColumnName1 DataType , ColumnName2 DataType , ColumnName3 DataType , ColumnName4 DataType , ... PRIMARY KEY (ColumnName1,ColumnName2...) ) Sample 1 customer CustomerID,Name,Email,CountryCode,Budget,Used Primary Keys CustomerID
CREATE TABLE customer ( CustomerID varchar(4) NOT NULL, Name varchar(50) NOT NULL, Email varchar(50) NOT NULL, CountryCode varchar(2) NOT NULL, Budget double NOT NULL, Used double NOT NULL, PRIMARY KEY (CustomerID) ) Output
CustomerID Name Email CountryCode Budget Used Sample 2 Table ALTER Key
ALTER TABLE customer ADD PRIMARY KEY (CustomerID) Output
CustomerID Name Email CountryCode Budget Used
-
http://www.thaicreate.com nattawut 35
SQL CREATE INDEX Index Index Column Index JOIN Table Index JOIN Query Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
CREATE TABLE "TableName" ( ColumnName1 DataType , ColumnName2 DataType , ColumnName3 DataType , ColumnName4 DataType , ... INDEX (ColumnName1,ColumnName2...) ) ALTER ALTER TABLE "TabmeName" ADD INDEX (Column1,Column2) Table : customer CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Table : country
CountryCode CountryName
TH Thailand
EN English
US United states
Sample 1 Index Table customer Column CountryCode ( Table)
ALTER TABLE customer ADD INDEX (CountryCode) Output Index Column CountryCode Sample 2 JOIN Table Column Index customer country
SELECT a.CustomerID, a.Name, a.Email, b.CountryName, a.Budget, a.Used FROM customer a, country b WHERE a.CountryCode = b.CountryCode
-
http://www.thaicreate.com nattawut 36
Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] Thailand 1000000 600000
C002 John Smith [email protected] English 2000000 800000
C003 Jame Born [email protected] English 3000000 600000
C004 Chalee Angel [email protected] United states 4000000 100000
Index JOIN Column Record
SQL UNIQUE UNIQUE Column Rows Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
CREATE TABLE "TableName" ( ColumnName1 DataType , ColumnName2 DataType , ColumnName3 DataType , ColumnName4 DataType , ... UNIQUE (ColumnName1,ColumnName2...) ) ALTER ALTER TABLE "TabmeName" ADD UNIQUE (Column1,Column2) Table : customer CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample 1 UNIQUE Table customer Column Email ( Table)
ALTER TABLE customer ADD UNIQUE (Email) Output
CustomerID Name Email CountryCode Budget Used C001 Win [email protected] TH 1000000 600000
-
http://www.thaicreate.com nattawut 37
Weerachai
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] EN 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
UNIQUE Column Email Rows
SQL AUTO_INCREMENT For MySQL Auto Number , Database : MySQL Syntax
CREATE TABLE "TableName" ( ColumnName1 DataType AUTO_INCREMENT, ColumnName2 DataType , ColumnName3 DataType , ColumnName4 DataType , ... PRIMARY KEY (ColumnName1) ) ALTER ALTER TABLE "TabmeName" CHANGE ColumnName1 ColumnName1 DataType AUTO_INCREMENT Sample 1 audit Column AuditID , CustomerID , Date , Used AuditID AUTO_INCREMENT
CREATE TABLE audit ( AuditID int(4) NOT NULL auto_increment, CustomerID varchar(4) NOT NULL, Date date NOT NULL, Used double NOT NULL, PRIMARY KEY (AuditID) ); 1,2,3,4,5,6 .... 0 0001,0002,0003 Attributes UNSIGNED ZEROFILL
ALTER TABLE audit CHANGE AuditID AuditID INT( 4 ) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT
-
http://www.thaicreate.com nattawut 38
SQL AUTO_INCREMENT/IDENTITY For SQL SERVER Auto Number SQL Server Database : SQL Server Syntax
CREATE TABLE "TableName" ( ColumnName1 DataType IDENTITY (1, 1) , ColumnName2 DataType , ColumnName3 DataType , ColumnName4 DataType , ... PRIMARY KEY (ColumnName1) ) Sample 1 audit Column AuditID , CustomerID , Date , Used AuditID AUTO_INCREMENT IDENTITY
CREATE TABLE audit ( AuditID int(4) IDENTITY (1, 1) NOT NULL , CustomerID varchar(4) NOT NULL, Date date NOT NULL, Used double NOT NULL, PRIMARY KEY (AuditID) ); SQL Server Management Studio
SQL AUTO_INCREMENT For ORACLE Auto Number Oracle Create Auto Number Oracle Table Sequence Database : Oracle
-
http://www.thaicreate.com nattawut 39
Syntax
create sequence my_seq start with 1 increment by 1 nomaxvalue; Sample 1 Insert Next Sequence
INSERT INTO audit_log (AuditID,CustomerID,Date,Used) VALUES (my_seq.nextval,'C001','27-Nov-2010','10000000') SQL INSERT INTO...SET (Table) Syntax MySQL Column Value Database : MySQL Syntax
INSERT INTO [Table-Name] SET Column1 = 'Value1' , Column2 = 'Value2' , ... Table : country CountryCode CountryName
TH Thailand
EN English
US United states
Sample1 Table
INSERT INTO country SET CountryCode =''CH'' ,CountryName = 'Chaina' Output
CountryCode CountryName
TH Thailand
EN English
US United states
CH Chaina
SQL CREATE DATABSE Create Database MySQL Database : MySQL Syntax
-
http://www.thaicreate.com nattawut 40
CREATE DATABASE "DatabaseName" Sample1 mydatabase
CREATE DATABASE mydatabase Output
CustomerID
mydatabase SQL CASE..WHEN Value Syntax IF...Else... Database : SQL Server Syntax
Simple CASE expression: CASE input_expression WHEN when_expression THEN result_expression [ ...n ] [ ELSE else_result_expression ] END Searched CASE expression: CASE WHEN Boolean_expression THEN result_expression [ ...n ] [ ELSE else_result_expression ] END Table : customer
Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 CASE...WHEN Column CountryCode
SELECT CustomerID, Name, CASE CountryCode WHEN 'TH' THEN 'Thailand' WHEN 'EN' THEN 'English' WHEN 'US' THEN 'United states' ELSE 'Unknown' END AS Country FROM customer Output
CustomerID Name Country
-
http://www.thaicreate.com nattawut 41
C001 Win Weerachai Thailand
C002 John Smith English
C003 Jame Born United states
C004 Chalee Angel United states
THEN Value Column
SELECT CustomerID, Name, CASE CountryCode WHEN 'TH' THEN Column1 WHEN 'EN' THEN 'Value' WHEN 'US' THEN Column3 ELSE Column4 END AS Country FROM customer SQL NULL / ISNULL NULL Column Database : SQL Server Syntax
NULL , IS NULL , ISNULL
SELECT * FROM table WHERE Column = NULL SELECT * FROM table WHERE Column IS NULL Column NOT NULL SELECT * FROM table WHERE ISNULL(Column,'1') // Null 1 SQL CASCADE ONUPDATE/ONDELETE Primary Keys Foreign keys Master Database : MySQL,Microsoft Access,SQL Server,Oracle MySQL Relation CasCade On Delete/On Update MySQL Relation Table InnoDB CasCade DELETE UPDATE Syntax InnoDB
-
http://www.thaicreate.com nattawut 42
CREATE TABLE `customer` ( . . . . ) ENGINE=InnoDB; Table customer audit customer audit Table : customer CREATE TABLE `customer` ( `CustomerID` varchar(4) NOT NULL, `Name` varchar(50) NOT NULL, `Email` varchar(50) NOT NULL, `CountryCode` varchar(2) NOT NULL, `Budget` double NOT NULL, `Used` double NOT NULL, PRIMARY KEY (`CustomerID`) ) ENGINE=InnoDB; INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', '[email protected]', 'TH', 1000000, 600000); INSERT INTO `customer` VALUES ('C002', 'John Smith', '[email protected]', 'EN', 2000000, 800000); INSERT INTO `customer` VALUES ('C003', 'Jame Born', '[email protected]', 'US', 3000000, 600000); INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', '[email protected]', 'US', 4000000, 100000); INSERT INTO `customer` VALUES ('C005', 'Weerachai Nukitram', '[email protected]', 'TH', 2000000, 100000);
Table : audit CREATE TABLE `audit` ( `AuditID` int(4) NOT NULL, `CustomerID` varchar(4) NOT NULL, `Date` date NOT NULL, `Used` double NOT NULL, PRIMARY KEY (`AuditID`), FOREIGN KEY (CustomerID) REFERENCES customer (CustomerID) ON DELETE CASCADE ON UPDATE
-
http://www.thaicreate.com nattawut 43
CASCADE ) ENGINE=InnoDB; INSERT INTO `audit` VALUES (1, 'C001', '2008-08-01', 100000); INSERT INTO `audit` VALUES (2, 'C001', '2008-08-05', 200000); INSERT INTO `audit` VALUES (3, 'C001', '2008-08-10', 300000); INSERT INTO `audit` VALUES (4, 'C002', '2008-08-02', 400000); INSERT INTO `audit` VALUES (5, 'C002', '2008-08-07', 100000); INSERT INTO `audit` VALUES (6, 'C002', '2008-08-15', 300000); INSERT INTO `audit` VALUES (7, 'C003', '2008-08-20', 400000); INSERT INTO `audit` VALUES (8, 'C003', '2008-08-25', 200000); INSERT INTO `audit` VALUES (9, 'C004', '2008-07-04', 100000); INSERT INTO `audit` VALUES (10, 'C005', '2008-07-04', 200000);
Relation 2 customer audit 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 [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
-
http://www.thaicreate.com nattawut 44
Sample1 FIRST customer
SELECT FIRST(Name) As Name FROM customer Output
Name
Weerachai Nukitram
SQL EXISTS 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 [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Table : customer2 CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 customer1 customer2
SELECT * FROM customer1 WHERE EXISTS (SELECT * FROM customer2) Output
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C004 Chalee Angel [email protected] US 4000000 100000
NOT EXISTS
-
http://www.thaicreate.com nattawut 45
SQL ALTER / / Coulmn Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
ALTER TABLE tablename ADD COLUMN (field1 type (size) (index1), ...) Table : customer CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Column Msn Column Email
ALTER TABLE customer ADD Msn VARCHAR( 100 ) NOT NULL AFTER Email Output
CustomerID Name Email Msn CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
SQL DROP , , Index Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
- DROP TABLE 'TableName' - DROP DATABASE 'DatabaseName' - Index DROP INDEX TableName.IndexName (for SQL Server) DROP INDEX TableName on IndexName (for Access) SQL UNION ALL Table UNION UNION ALL
-
http://www.thaicreate.com nattawut 46
UNION 2 DISTINCT Table Table UNION ALL Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT * FROM Table1 UNION ALL SELECT * FROM Table2 Table : customer1
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Table : customer2
CustomerID Name Email CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 UNION
SELECT * FROM customer1 UNION SELECT * FROM customer2 Output
CustomerID Name Email Msn CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000
Sample2 UNION ALL
SELECT * FROM customer1 UNION ALL SELECT * FROM customer2 Output
CustomerID Name Email Msn CountryCode Budget Used C001 Win Weerachai [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000
-
http://www.thaicreate.com nattawut 47
C003 Jame Born [email protected] US 3000000 600000 C004 Chalee Angel [email protected] US 4000000 100000 C004 Chalee Angel [email protected] US 4000000 100000
SQL AS 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 [email protected] TH 1000000 600000 C002 John Smith [email protected] EN 2000000 800000 C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Used Column Total
SELECT SUM(Used) As Total FROM customer Output
Total 2100000
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 [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 LAST customer
-
http://www.thaicreate.com nattawut 48
SELECT LAST(Name) As Name FROM customer Output
Name
Chalee Angel
SQL MYSQL IF IF Database : MySQL Syntax
SELECT IF search_condition 'true','false' AS Column Table : customer1 CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Used 200000 Low 200000 Height
SELECT CustomerID,Name,Used, IF(Used
-
http://www.thaicreate.com nattawut 49
SELECT WHERE
SELECT * FROM table WHERE DAY(Column) = 1 and MONTH(Column) = 07 and YEAR(Column) = 2008 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 audit WHERE
SELECT DAY(Date) As ListDay FROM audit WHERE MONTH(Date) = '7' AND YEAR(Date) = '2008' Output
ListDay 01
05
10
02
07
15
20
25
04
SQL MYSQL DATE_FOMRAT Format MySQL Database : MySQL,Microsoft Access,SQL Server Syntax
SELECT DATE_FORMAT(date,format) DateFormat
Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
-
http://www.thaicreate.com nattawut 50
Specifier Description
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, )
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week
%u Week (00..53), where Monday is the first day of the week
%V Week (01..53), where Sunday is the first day of the week; used with %X
%v Week (01..53), where Monday is the first day of the week; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal % character
%x x, for any x not listed above
SELECT WHERE
SELECT DATE_FORMAT(DateColumn,'%Y-%m') As NewColumn FROM table WHERE DATE_FORMAT(DateColumn,'%m-%d') = '07-01' 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
-
http://www.thaicreate.com nattawut 51
8 C003 2008-07-25 200000
9 C004 2008-07-04 100000
Sample1 audit WHERE '07-01'
SELECT DATE_FORMAT(Date,'%Y-%m') As MyDate FROM audit WHERE DATE_FORMAT(Date,'%m-%d') = '07-01' Output
MyDate
2008-07
SQL MYSQL DATE_ADD() (Table) Database : MySQL Syntax
DATE_ADD(date,INTERVAL expr unit)
unit Value Expected expr Format MICROSECOND MICROSECONDS
SECOND SECONDS
MINUTE MINUTES
HOUR HOURS
DAY DAYS
WEEK WEEKS
MONTH MONTHS
QUARTER QUARTERS
YEAR YEARS
SECOND_MICROSECOND 'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND 'MINUTES:SECONDS.MICROSECONDS'
MINUTE_SECOND 'MINUTES:SECONDS'
HOUR_MICROSECOND 'HOURS:MINUTES:SECONDS.MICROSECONDS'
HOUR_SECOND 'HOURS:MINUTES:SECONDS'
HOUR_MINUTE 'HOURS:MINUTES'
DAY_MICROSECOND 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'
DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE 'DAYS HOURS:MINUTES'
DAY_HOUR 'DAYS HOURS'
YEAR_MONTH 'YEARS-MONTHS'
Table : member
CustomerID Name Email ReisgerDate
-
http://www.thaicreate.com nattawut 52
C001 Win Weerachai [email protected] 2010-11-27 18:34:00
Sample1 Column RegisterDate 1
SELECT Name, DATE_ADD(OrderDate,INTERVAL 30 DAY) As RegisterDate FROM customer Output
Name Register Date
Win Weerachai 2010-12-27 18:34:00
SQL MYSQL DATEDIFF() (Table) 2 Database : MySQL Syntax
DATEDIFF(date1,date2) Table : member
CustomerID Name Email StartDate EndDate
C001 Win Weerachai [email protected] 2010-11-27 2010-12-27
Sample1 Column StartDate EndDate
SELECT Name, DATEDIFF(StartDate,EndDate) As DiffDate FROM customer Output
Name DiffDate
Win Weerachai 30
SQL MID (Table) Colomn Field Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT MID(column_name,start[,length]) FROM table_name Table : customer
-
http://www.thaicreate.com nattawut 53
CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Column Name 4
SELECT MID(Name,1,4) As Name FROM customer Output
Name
Win
John
Jame
Chal
SQL UCASE , UPPER (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT UCASE(ColumnName) FROM table_name Table : customer CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Column Name
SELECT UCASE(Name) As Name FROM customer Output
Name
WIN WEERACHAI
JOHN SMITH
JAME BORN
CHALEE ANGEL
-
http://www.thaicreate.com nattawut 54
SQL LCASE , LOWER (Table) Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT LCASE(ColumnName) FROM table_name Table : customer CustomerID Name Email CountryCode Budget Used
C001 Win Weerachai [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Column Name
SELECT LCASE(Name) As Name FROM customer Output
Name
win weerachai
john smith
jame born
chalee angel
SQL LEN (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 [email protected] TH 1000000 600000
C002 John Smith [email protected] EN 2000000 800000
C003 Jame Born [email protected] US 3000000 600000
C004 Chalee Angel [email protected] US 4000000 100000
Sample1 Column Name
SELECT Name, LEN(Name) As LenName FROM customer Output
-
http://www.thaicreate.com nattawut 55
Name LenName
win weerachai 13
john smith 10
jame born 9
chalee angel 12
SQL ROUND (Table) >= .5 < .5 Database : MySQL,Microsoft Access,SQL Server,Oracle Syntax
SELECT ROUND(ColumnName) FROM table_name SQL NOW() MySQL Database Database : MySQL Database Syntax
SELECT NOW() SELECT * FROM table WHERE Column = NOW() INSERT INTO table (Column1,Column2) VALUES ('Value1',NOW()) SQL SYSDATE() Oracle Database Database : Oracle Database Syntax
SELECT SYSDATE() SELECT * FROM table WHERE Column = SYSDATE() INSERT INTO table (Column1,Column2) VALUES ('Value1',SYSDATE()) SQL REPAIR TABLE MySQL Database : MySQL Syntax
REPAIR TABLE `table-name
-
http://www.thaicreate.com nattawut 56
SQL USE Database : MySQL , SQL Server Syntax
USE DatabaseName SQL RENAME TABLE (Rename Table) Database : MySQL Syntax
RENAME TABLE `old-table` `new-table` SQL RENAME DATABASE Database : MySQL Syntax
RENAME DATABASE `old-database` `new-database` SQL ORACLE IMP (Import Database) Import Oracle Database Database : Oracle Syntax
imp user/password@tns-name file=file-name.dmp full=yes SQL ORACLE EXP (Export Database) Export Oracle Database Database : Oracle Syntax
exp user/password@tns-name file=d:\file-name.dmp SQL OPTIMIZE TABLE Table MySQL Database : MySQL
-
http://www.thaicreate.com nattawut 57
Syntax
REPAIR OPTIMIZE `table-name` SQL CONVERT() SQL Server Database : SQL Server Syntax
CONVERT ( data_type [ ( length ) ] ,expression [ ,style ] ) CONVERT (GETDATE() , 103) Expression
With century (yyyy) Standard Input/Output** 0 or 100 (*) Default mon dd yyyy hh:mi AM (or PM) 101 United States mm/dd/yyyy 102 ANSI yy.mm.dd 103 British/French dd/mm/yy 104 German dd.mm.yy 105 Italian dd-mm-yy 106 - dd mon yy 107 - Mon dd, yy 108 - hh:mm:ss 9 or 109 (*) Default + milliseconds mon dd yyyy hh:mi:ss:mmm AM (or PM) 110 United States mm-dd-yy 111 JAPAN yy/mm/dd 112 ISO Yymmdd 13 or 113 (*) Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm (24h) 114 - hh:mi:ss:mmm (24h) 20 or 120 (*) ODBC canonical yyyy-mm-dd hh:mi:ss (24h) 21 or 121 (*) ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm (24h) 126(***) ISO8601 yyyy-mm-ddThh:mm:ss.mmm (no spaces) 130* Hijri**** dd mon yyyy hh:mi:ss:mmm AM 131* Hijri**** dd/mm/yy hh:mi:ss:mmm AM