Select用途:
從指定表中取出指定的列的數據
語法:
SELECT column_name(s) FROM table_name
解釋:
從數據庫中選取資料列,並允許從一或多個資料表中,選取一或多個資料列或資料行。SELECT 陳述式的完整語法相當復雜,但主要子句可摘要為:
SELECT select_list
[ INTO new_table ]
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]
例:
“Persons” 表中的數據有
LastName
FirstName
Address
City
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
選出字段名” LastName”、” FirstName” 的數據
SELECT LastName,FirstName FROM Persons
返回結果:
LastName
FirstName
Hansen
Ola
Svendson
Tove
Pettersen
Kari
選出所有字段的數據
SELECT * FROM Persons
返回結果:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Where
用途:
被用來規定一種選擇查詢的標准
語法:
SELECT column FROM table WHERE column condition value
注意: 在某些SQL的版本中不等號< >能被寫作為!=
SELECT語句返回WHERE子句中條件為true的數據
例:
SELECT * FROM Persons WHERE City='Sandnes'
"Persons" 表中的數據有:
LastName
FirstName
Address
City
Year
Hansen
Ola
Timoteivn 10
Sandnes
1951
Svendson
Tove
Borgvn 23
Sandnes
1978
Svendson
Stale
Kaivn 18
Sandnes
1980
Pettersen
Kari
Storgt 20
Stavanger
1960
LastName
FirstName
Address
City
Year
Hansen
Ola
Timoteivn 10
Sandnes
1951
Svendson
Tove
Borgvn 23
Sandnes
1978
Svendson
Stale
Kaivn 18
Sandnes
1980
And & Or
用途:
在WHERE子句中AND和OR被用來連接兩個或者更多的條件
解釋:
AND在結合兩個布爾表達式時,只有在兩個表達式都為 TRUE 時才傳回 TRUE
OR在結合兩個布爾表達式時,只要其中一個條件為 TRUE 時,OR便傳回 TRUE
例:
"Persons" 表中的原始數據:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Svendson
Stephen
Kaivn 18
Sandnes
表中
SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
運算子來查找表中FirstNameLastName為” Svendson”的數據
SELECT * FROM Persons
WHERE firstname='Tove'
OR lastname='Svendson'
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
Svendson
Stephen
Kaivn 18
Sandnes
你也能結合AND和OR (使用括號形成復雜的表達式),如:
SELECT * FROM Persons WHERE
(FirstName='Tove' OR FirstName='Stephen')
AND LastName='Svendson'
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
Svendson
Stephen
Kaivn 18
Sandnes
Between…And
用途:
指定需返回數據的范圍
語法:
SELECT column_name FROM table_name
WHERE column_name
BETWEEN value1 AND value2
例:
“Persons”
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Nordmann
Anna
Neset 18
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Svendson
Tove
Borgvn 23
Sandnes
返回LastName為從”Hansen”到”Pettersen”的數據:
SELECT * FROM Persons WHERE LastName
BETWEEN 'Hansen' AND 'Pettersen'
返回結果:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Nordmann
Anna
Neset 18
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
為了顯示指定范圍之外的數據,也可以用NOT操作符:
SELECT * FROM Persons WHERE LastName
NOT BETWEEN 'Hansen' AND 'Pettersen'
返回結果:
LastName
FirstName
Address
City
Svendson
Tove
Borgvn 23
Sandnes
Distinct
用途:
DISTINCT關鍵字被用作返回唯一的值
語法:
SELECT DISTINCT column-name(s) FROM table-name
解釋:
當column-name(s)中存在重復的值時,返回結果僅留下一個
例:
“Orders”表中的原始數據
Company
OrderNumber
Sega
3412
W3Schools
2312
Trio
4678
W3Schools
6798
用DISTINCT關鍵字返回Company字段中唯一的值:
SELECT DISTINCT Company FROM Orders
返回結果:
Company
Sega
W3Schools
Trio
Order by
用途:
指定結果集的排序
語法:
SELECT column-name(s) FROM table-name ORDER BY { order_by_expression [ ASC | DESC ] }
解釋:
指定結果集的排序,可以按照ASC(遞增方式排序,從最低值到最高值)或者DESC(遞減方式排序,從最高值到最低值)的方式進行排序,默認的方式是ASC
例:
“Orders”表中的原始數據:
Company
OrderNumber
Sega
3412
ABC Shop
5678
W3Schools
2312
W3Schools
6798
按照Company字段的升序方式返回結果集:
SELECT Company, OrderNumber FROM Orders
ORDER BY Company
返回結果:
Company
OrderNumber
ABC Shop
5678
Sega
3412
W3Schools
6798
W3Schools
2312
按照Company字段的降序方式返回結果集:
SELECT Company, OrderNumber FROM Orders
ORDER BY Company DESC
返回結果:
Company
OrderNumber
W3Schools
6798
W3Schools
2312
Sega
3412
ABC Shop
5678
Group by
用途:
對結果集進行分組,常與匯總函數一起使用。
語法:
SELECT column,SUM(column) FROM table GROUP BY column
例:
“Sales”表中的原始數據:
Company
Amount
W3Schools
5500
IBM
4500
W3Schools
7100
Amout的合計:
SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company
返回結果:
Company
SUM(Amount)
W3Schools
12600
IBM
4500
Having
用途:
指定群組或匯總的搜尋條件。
語法:
SELECT column,SUM(column) FROM table
GROUP BY column
HAVING SUM(column) condition value
解釋:
HAVING 通常與 GROUP BY 子句同時使用。不使用 GROUP BY 時,HAVING 則與 WHERE 子句功能相似。
例:
“Sales”表中的原始數據:
Company
Amount
W3Schools
5500
IBM
4500
W3Schools
7100
按照Company字段進行分組,求出每個Company的Amout的合計在10000
SELECT Company,SUM(Amount) FROM Sales
GROUP BY Company HAVING SUM(Amount)>10000
返回結果:
Company
SUM(Amount)
W3Schools
12600
Join
用途:
當你要從兩個或者以上的表中選取結果集時,你就會用到JOIN。
例:
“Employees”表中的數據如下,(其中ID
ID
Name
01
Hansen, Ola
02
Svendson, Tove
03
Svendson, Stephen
04
Pettersen, Kari
“Orders”表中的數據如下:
ID
Product
01
Printer
03
Table
03
Chair
用Employees的ID和Orders的ID相關聯選取數據:
SELECT Employees.Name, Orders.Product
FROM Employees, Orders
WHERE Employees.ID = Orders.ID
Name
Product
Hansen, Ola
Printer
Svendson, Stephen
Table
Svendson, Stephen
Chair
或者你也可以用JOIN關鍵字來完成上面的操作:
SELECT Employees.Name, Orders.Product
FROM Employees
INNER JOIN Orders
ON Employees.ID = Orders.ID
INNER JOIN的語法:
SELECT field1, field2, fIEld3
FROM first_table
INNER JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfIEld
解釋:
INNER JOIN返回的結果集是兩個表中所有相匹配的數據。
LEFT JOIN的語法:
SELECT field1, field2, fIEld3
FROM first_table
LEFT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfIEld
用”Employees”表去左外聯結”Orders”表去找出相關數據:
SELECT Employees.Name, Orders.Product
FROM Employees
LEFT JOIN Orders
ON Employees.ID = Orders.ID
返回結果:
Name
Product
Hansen, Ola
Printer
Svendson, Tove
Svendson, Stephen
Table
Svendson, Stephen
Chair
Pettersen, Kari
解釋:
LEFT JOIN返回”first_table”中所有的行盡管在” second_table”中沒有相匹配的數據。
RIGHT JOIN的語法:
SELECT field1, field2, fIEld3
FROM first_table
RIGHT JOIN second_table
ON first_table.keyfield = second_table.foreign_keyfIEld
用”Employees”表去右外聯結”Orders”表去找出相關數據:
SELECT Employees.Name, Orders.Product
FROM Employees
RIGHT JOIN Orders
ON Employees.ID = Orders.ID
返回結果:
Name
Product
Hansen, Ola
Printer
Svendson, Stephen
Table
Svendson, Stephen
Chair
解釋:
RIGHT JOIN返回” second_table”中所有的行盡管在”first_table”中沒有相匹配的數據。
Alias
用途:
可用在表、結果集或者列上,為它們取一個邏輯名稱
語法:
給列取別名:
SELECT column AS column_alias FROM table
給表取別名:
SELECT column FROM table AS table_alias
例:
“Persons”表中的原始數據:
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
運行下面的SQL:
SELECT LastName AS Family, FirstName AS Name
FROM Persons
返回結果:
Family
Name
Hansen
Ola
Svendson
Tove
Pettersen
Kari
運行下面的SQL:
SELECT LastName, FirstName
FROM Persons AS Employees
返回結果:
Employees中的數據有:
LastName
FirstName
Hansen
Ola
Svendson
Tove
Pettersen
Kari
Insert Into
用途:
在表中插入新行
語法:
插入一行數據
INSERT INTO table_name
VALUES (value1, value2,....)
插入一行數據在指定的字段上
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
例:
“Persons”表中的原始數據:
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
運行下面的SQL插入一行數據:
INSERT INTO Persons
VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')
插入後”Persons”表中的數據為:
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24
Sandnes
運行下面的SQL插入一行數據在指定的字段上:
INSERT INTO Persons (LastName, Address)
VALUES ('Rasmussen', 'Storgt 67')
插入後”Persons”表中的數據為:
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24
Sandnes
Rasmussen
Storgt 67
Update
用途:
更新表中原有數據
語法:
UPDATE table_name SET column_name = new_value
WHERE column_name = some_value
例:
“Person”表中的原始數據:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Storgt 67
運行下面的SQL將Person表中LastName
UPDATE Person SET FirstName = 'Nina'
WHERE LastName = 'Rasmussen'
更新後”Person”表中的數據為:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Storgt 67
同樣的,用UPDATE語句也可以同時更新多個字段:
UPDATE Person
SET Address = 'StIEn 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
更新後”Person”表中的數據為:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
StIEn 12
Stavanger
Delete
用途:
刪除表中的數據
語法:
DELETE FROM table_name WHERE column_name = some_value
例:
“Person”表中的原始數據:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
StIEn 12
Stavanger
刪除Person表中LastName為”Rasmussen”的數據:
DELETE FROM Person WHERE LastName = 'Rasmussen'
執行刪除語句後”Person”表中的數據為:
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Create Table
用途:
建立新的資料表。
語法:
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
.......
)
例:
創建一張叫“Person”的表,該表有4個字段"LastName", "FirstName", "Address", "Age"
CREATE TABLE Person
(
LastName varchar,
FirstName varchar,
Address varchar,
Age int
)
如果想指定字段的最大存儲長度,你可以這樣:
CREATE TABLE Person
(
LastName varchar(30),
FirstName varchar(30),
Address varchar(120),
Age int(3)
)
下表中列出了在SQL的一些數據類型:
Data Type
Description
integer(size)
int(size)
smallint(size)
tinyint(size)
Hold integers only. The maximum number of digits are specifIEd in parenthesis.
decimal(size,d)
numeric(size,d)
Hold numbers with fractions. The maximum number of digits are specified in "size". The maximum number of digits to the right of the decimal is specifIEd in "d".
char(size)
Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specifIEd in parenthesis.
varchar(size)
Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specifIEd in parenthesis.
date(yyyymmdd)
Holds a date
Alter Table
用途:
在已經存在的表中增加後者移除字段
語法:
ALTER TABLE table_name
ADD column_name datatype
ALTER TABLE table_name
DROP COLUMN column_name
注意:某些數據庫管理系統不允許移除表中的字段
例:
“Person”表中的原始數據:
LastName
FirstName
Address
Pettersen
Kari
Storgt 20
在Person表中增加一個名為City的字段:
ALTER TABLE Person ADD City varchar(30)
增加後表中數據如下:
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
移除Person表中原有的Address字段:
ALTER TABLE Person DROP COLUMN Address
移除後表中數據如下:
LastName
FirstName
City
Pettersen
Kari
Drop Table
用途:
在數據庫中移除一個數據表定義及該數據表中的所有資料、索引、觸發程序、條件約束及權限指定。
語法:
DROP TABLE table_name
Create Database
用途:
建立新的數據庫
語法:
CREATE DATABASE database_name
Drop Database
用途:
移除原有的數據庫
語法:
DROP DATABASE database_name
聚集函數
count
用途:
傳回選取的結果集中行的數目。
語法:
SELECT COUNT(column_name) FROM table_name
例:
“Persons”表中原始數據如下:
Name
Age
Hansen, Ola
34
Svendson, Tove
45
Pettersen, Kari
19
選取記錄總數:
SELECT COUNT(Name) FROM Persons
執行結果:
3
sum
用途:
以表達式傳回所有值的總和,或僅 DISTINCT 值。SUM 僅可用於數值資料行。已忽略 Null 值。
語法:
SELECT SUM(column_name) FROM table_name
例:
“Persons”表中原始數據如下:
Name
Age
Hansen, Ola
34
Svendson, Tove
45
Pettersen, Kari
19
選取”Persons”表中所有人的年齡總和:
SELECT SUM(Age) FROM Persons
執行結果:
98
選取”Persons”表中年齡超過20歲的人的年齡總和:
SELECT SUM(Age) FROM Persons WHERE Age>20
執行結果:
79
avg
用途:
傳回選取的結果集中值的平均值。已忽略 Null 值。
語法:
SELECT AVG(column_name) FROM table_name
例:
“Persons”表中原始數據如下:
Name
Age
Hansen, Ola
34
Svendson, Tove
45
Pettersen, Kari
19
選取”Persons”表中所有人的平均年齡:
SELECT AVG(Age) FROM Persons
執行結果:
32.67
選取”Persons”表中年齡超過20歲的人的平均年齡:
SELECT AVG(Age) FROM Persons WHERE Age>20
執行結果:
39.5
max
用途:
傳回選取的結果集中值的最大值。已忽略 Null 值。
語法:
SELECT MAX(column_name) FROM table_name
例:
“Persons”表中原始數據如下:
Name
Age
Hansen, Ola
34
Svendson, Tove
45
Pettersen, Kari
19
選取”Persons”表中的最大年齡:
SELECT MAX(Age) FROM Persons
執行結果:
45
min
用途:
傳回選取的結果集中值的最小值。已忽略 Null 值。
語法:
SELECT MIN(column_name) FROM table_name
例:
“Persons”表中原始數據如下:
Name
Age
Hansen, Ola
34
Svendson, Tove
45
Pettersen, Kari
19
選取”Persons”表中的最小年齡:
SELECT MIN(Age) FROM Persons
執行結果:
19
算術函數
abs
用途:
傳回指定數值表達式 (Numeric Expression) 的絕對正值。
語法:
ABS(numeric_expression)
例:
1.0 0.0 1.0
ceil
用途:
傳回大於等於給定數值表達式的最小整數。
語法:
CEIL(numeric_expression)
例:
124.00 -123.00
floor
用途:
傳回小於或等於給定數值表達式的最大整數。
語法:
FLOOR(numeric_expression)
例:
123.00 -124.00
cos
用途:
在指定表達式中傳回指定角度 (以弪度為單位) 的三角余弦值的數學函數。
語法:
COS(numeric_expression)
例:
-0.599465
cosh
用途:
傳回以弧度為單位的角度值,其余弦為指定的 float 表達式,也稱為反余弦。
語法:
COSH(numeric_expression)
例:
3.14159
sin
用途:
以近似的數值 (float) 表達式傳回給定角度 (以弧度) 之三角正弦函數 (Trigonometric Sine)。
語法:
SIN(numeric_expression)
例:
0.929607
sinh
用途:
傳回以弪度為單位的角度,其正弦為指定的 float 表達式 (也稱為反正弦)。
語法:
SINH(numeric_expression)
例:
-1.5708
tan
用途:
傳回輸入表達式的正切函數。
語法:
TAN(numeric_expression)
例:
1.6331778728383844E+16
tanh
用途:
傳回以弪度為單位的角度,其正切為指定的 float 表達式 (也稱為反正切)。
語法:
TANH(numeric_expression)
例:
-1.54858
exp
用途:
傳回給定的 float 表達式的指數 (Exponential) 值。
語法:
EXP(numeric_expression)
例:
2.69498e+164
log
用途:
傳回給定的 float 表達式之自然對數。
語法:
LOG(numeric_expression)
例:
1.64396
power
用途:
傳回給定表達式指定乘冪的值。
語法:
POWER(numeric_expression,v)
例:
64
sign
用途:
傳回給定的表達式之正 (+1)、零 (0) 或負 (-1) 號。
語法:
SIGN(numeric_expression)
例:
1 0 -1
sqrt
用途:
傳回給定表達式的平方。
語法:
SQRT(numeric_expression)
例:
SQRT(10)
執行結果:
100