【黑馬程序員】5. 結構化查詢語言(SQL)

---------------------- ASP.Net+Android+IOS開發.Net培訓、期待與您交流! ----------------------

SQL語句(Structured Query Language)即結構化查詢語言,是一種數據庫查詢和程序設計語言,是溝通服務器和客戶端的專用語言。不同的DBMS(數據庫管理系統)都統一使用SQL語法。

注意:

      1SQL語句中的字符串要用單引號引起來;

      2SQL語句中的關鍵字是不區分大小寫的,但字符串值是區分大小寫的。

SQL主要分爲以下三種:

DDL:數據定義語言,createalterdropdeclare

DML:數據操縱語言,selectdeleteupdateinsert

DCL:數據控制語言,grant,,revoke,commit,rollback。

 

1、新建表 CREATETABLE

CREATE TABLE [dbo].[Person1]
(
	Name nvarchar(50),
	Age int,
	Salary numeric(10,2)
)

2 、插入數據insertinto

insert into Person1(Name,Age,Salary) values('張三',20,5000)

3、更新數據update

1)        更新一個列

update Person1 set Age=30

2)        更新多個列

update Person1 set Name='Tom', Age=30

3)        更新其中一部分數據

update Person1 set Age=30 where Name='張三'  --表示只更新Name張三的那條記錄。

4、刪除deletedroptable

1)        刪除數據

delete from Person1

2)        刪除表

drop table Person1

Delete也可以帶where子句來刪除指定的數據,如:

delete from Person1 where Age>30

5、數據檢索select

select * from Person1
select Name,Age from Person1
select Name as 姓名, Age as 年齡 from Person1  --as用在查詢語句中時,用來重新指定返回的column 名字,給column起別名

6、聚合函數

聚合函數是對一組值執行計算並返回單一的值的函數,它經常與SELECT語句的GROUP BY子句一同使用。SQL SERVER中的聚合函數有:

1)AVG:返回指定組中的平均值,空值被忽略。     

select avg(Salary) from Employee group by E_Id

2)COUNT:返回指定組中項目的數量。

select  count(E_Id) from Employee    

3)MAX:返回指定數據的最大值。

select max(Salary) from Employee group by Age

4)MIN:返回指定數據的最小值。  

select min(Salary) from Employee group by Age

5)SUM:返回指定數據的和,只能用於數字列,空值被忽略。

select sum(Salary) from Employee group by Age

6)COUNT_BIG:返回指定組中的項目數量,與COUNT函數不同的是COUNT_BIG返回bigint值,而COUNT返回的是int值。

selectcount_big(Salary)from Employee

7) BINARY_CHECKSUM:返回對錶中的行或表達式列表計算的二進制校驗值,用於檢測表中行的更改。    

select binary_checksum(Salary) from Employee group by E_Id

8)CHECKSUM_AGG:返回指定數據的校驗值,空值被忽略。 

select checksum_agg(binary_checksum(*)) from Employee group by E_Id

9)CHECKSUM:返回在表的行上或在表達式列表上計算的校驗值,用於生成哈希索引。

10)STDEV:返回給定表達式中所有值的統計標準偏差。    

select stdev(Salary) from Employee

11)STDEVP:返回給定表達式中的所有值的填充統計標準偏差。    

select stdevp(Salary) from Employee

12)VAR:返回給定表達式中所有值的統計方差。    

select var(Salary) from Employee

13)VARP:返回給定表達式中所有值的填充的統計方差。   

select varp(Salary) from Employee

7、       數據排序orderby

select * from Person1 order by Age
order by column_name ASC    --升序排列
order by column_name DESC         --降序排列

order by子句位於select語句的末尾,它允許指定按照一個列或多個列進行排序,還可以指定排序方式,升序或降序

order by 子句要入在where子句之後,如:

select * from Person1 where Age>20 order by Age asc

8、通配符過濾like

1)        單字符匹配的通配符爲半角狀態下的下劃線“_”,用它來匹配單個出現的字符。如:

select * from Person1 where Name Like'_om'    --以任意一個字符開頭,剩餘部分爲“om”的數據

2)        多個字符匹配的能配符爲半角的百分號“%”,用它來匹配任意次數(零次或多次)出現的任意字符。如:

select * from Person1 where Name like'%n%'    --檢索中包含字母“n”的數據

9、空值處理 is null/is not null

在數據庫中,一個列如果沒有指定值,那麼它的值就爲null。與c#中的null不同的是,數據庫中的null表示“不知道”,而不是沒有。在數據庫中,判斷是否爲空用“is null”或“is not null”。

如:  

 select * from Person1 where Name=null
select * from Person1 where Name!=null

以上兩條語句執行後沒有任何返回結果,正確的表達方式爲:

select * from Person1 where Name is null
select * from Person1 where Name is not null

10、        多值匹配in

select * from Person1 where age in(20,25,30) --檢索age爲20或25或30的記錄
select * from Person1 where age between 20 and 30  --檢索age在20至30之間的

11、        數據分組group by

Group by子句放在where子句之後。沒有出現在group by子句中的列是不能放到select語句後的列名列表中的(聚合函數除外)。

select Age,avg(Salary) from Person1 group by Age            --正確
select Age,Salary from Person1 group by Age             --錯誤,Salary不能出現在select列表中

12、        Having語句

where子句中不能使用聚合函數,必須使用havinghaving要位於group by之後。

select Age,count(*) from Perosn
group by Age
having count(*)>1

注:having 中不能使用未參數分組的列,即having中能用的列和select中能用的列一樣。如:

select Age,count(*) from Person1
group by Age
having Salary>3000

上面這條語句會報錯,因爲having中的Salary沒有出現在group by子句中。

Having是對分組後的數據的過濾,而where是對原始數據進行過濾,所以having無法代替where

137、        限制結果集行數top

select top 3*from Person1 order by Salary desc --按照Salary降序排列後取前3條記錄
select top 3*from Person1 where Number not in(select top 5 Number from Person1 order by Salary desc) --查詢的嵌套,按照工資降序排序後取從第6名開始之後的3條記錄

top可以用於對數據進行分頁顯示的場合,比如網站信息的分頁顯示。

14、        聯合結果集 union

使用原則:

每個結果集必須有相同的列數;

每個結果集的對應列的數據類型必須相兼容。   

select Name,Age from Person1
union 
select Name,Age from Person2

union 合併後的結果集默認是自動去重的,若不想去重,需在union後邊加上all

select Name,Age from Person1
union all
select Name,Age from Person2

15、        日期函數

1)    getdate():獲取當前的日期時間

2)    dateadd(datepart,number,date):計算增加後的日期

datepart:計量單位

number:增量

date:待計算的日期

如:

select dateadd(day,3,getdate())            --當前日期加3天
select dateadd(dd,-3,getdate())            --當前日期減3天

其它計量單位

計量單位

別名

備註

year

yy

年份

quarter

qq

季度

month

mm

月份

day

dd

week

wk

hour

hh

小時

minute

mi

second

ss

1)    datediff(datepart,startdate,enddate):計算兩個日期之間的差值

select datediff(hh,getdate(),getdate()+10)
select datediff(hh,getdate(),dateadd(wk,1,getdate()))

2)    datepart(datepart,date):獲取日期的指定部分

select datepart(yy,getdate())


16、        類型轉換函數

1)    cast(expressionas datatype)

cast('123' as int)         --把字符串‘123’轉換爲整型123


2)    convert(datatype,express)

convert(datetime,'2008-1-1')


17、        空值處理函數

isnull(expression,value):如果expression不爲空則返回expression,否則返回value。

select ISNULL(Name,'無名') as 姓名 from Person1 --如果Name爲空則返回‘無名’,否則返回Name的值


18、        case函數

單值判斷,相當於switch-case

基本語法:             

case expression
when value1 then return_expression1
when value2 then return_expression2
when value3 then return_expression3
else default_return
end

例:             

select Name
(case level
when 1then'VIP'
when 2then'高級'
when 3then'普通'
else '類型錯誤'
end) as 姓名
from Customer

---------------------- ASP.Net+Android+IOS開發.Net培訓、期待與您交流! ----------------------

詳細請查看:http://edu.csdn.net






 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章