sql servier

查看 SQL Server 中所有數據庫的信息
select * from sysdatabases
1
查看當前使用的數據庫中所有表信息
use Northwind
select * from sysobjects where type='U'
select * from sysobjects where type not in('U','S','SQ','IT','D')  --當前使用的數據庫中所有表約束
exec sp_help Categories  --查看指定表結構
1
2
3
4
查詢表的所有數據
select * from Categories --商品種類
select * from Suppliers --供應廠商
select * from Products --商品信息
select * from Customers --客戶信息
select * from Employees --員工信息
select * from Shippers --貨運公司
select * from Orders --訂單信息
select * from OrderDetails --訂單詳情
--delete from OrderDetails --備份測試用
select * from Reports --報表配置
1
2
3
4
5
6
7
8
9
10
查詢結果排序
select CategoryName from Categories --默認按首字段值的首字母排序(與MySQL不同,MySQL默認是主鍵排序)
select CategoryName from Categories order by CategoryID --默認編號正序
select CategoryName from Categories order by CategoryID asc --編號正序
select CategoryName from Categories order by CategoryID desc --編號倒序
select * from OrderDetails where OrderID in(10248,10249) order by OrderID asc,ProductID asc --按多列排序1
select * from OrderDetails where OrderID in(10248,10249) order by OrderID asc,ProductID desc --按多列排序2
select * from OrderDetails where OrderID in(10248,10249) order by OrderID desc,ProductID asc --按多列排序3
select * from OrderDetails where OrderID in(10248,10249) order by OrderID desc,ProductID desc --按多列排序4
1
2
3
4
5
6
7
8
指定條數查詢
select top 2 * from Categories order by CategoryID --頭兩行數據(排序必要)
select top 2 * from Categories where CategoryID not in(select top 2 CategoryID from Categories) order by CategoryID --第二行後兩行數據
select top 2 CategoryID from Categories order by CategoryID desc --倒數兩行數據
1
2
3
分頁查詢
每頁顯示3條。

想法一
select COUNT(*) from Categories -- 8/3=2···2,最後一頁餘2條數據
select top 3 * from (select top (1*3) * from Categories order by CategoryID) Tab order by CategoryID desc --分頁第一頁,每頁3條
select top 3 * from (select top (2*3) * from Categories order by CategoryID) Tab order by CategoryID desc --分頁第二頁,每頁3條
select top (8%3) * from (select top (3*3) * from Categories order by CategoryID) Tab order by CategoryID desc --分頁第三頁,每頁3條
1
2
3
4
想法二(正序)
--select top PerPage * from Categories where CategoryID not in(select top ((NowPage-1)*PerPage) CategoryID from Categories order by CategoryID) order by CategoryID
select top 3 * from Categories where CategoryID not in(select top (0*3) CategoryID from Categories order by CategoryID) order by CategoryID
select top 3 * from Categories where CategoryID not in(select top (1*3) CategoryID from Categories order by CategoryID) order by CategoryID
select top 3 * from Categories where CategoryID not in(select top (2*3) CategoryID from Categories order by CategoryID) order by CategoryID
1
2
3
4
想法二(倒序)
select top 3 * from Categories where CategoryID not in(select top (0*3) CategoryID from Categories order by CategoryID desc) order by CategoryID desc
select top 3 * from Categories where CategoryID not in(select top (1*3) CategoryID from Categories order by CategoryID desc) order by CategoryID desc
select top 3 * from Categories where CategoryID not in(select top (2*3) CategoryID from Categories order by CategoryID desc) order by CategoryID desc
1
2
3
查詢字段指定別名
select CategoryID,CategoryName from Categories --查詢指定列
select CategoryID,CategoryName as 種類名稱 from Categories --指定列別名1
select CategoryID,CategoryName 種類名稱 from Categories --指定列別名2
select CategoryID,種類名稱=CategoryName from Categories --指定列別名3
1
2
3
4
集合函數
select count(*) 記錄總數 from Categories --計算總數
select UnitPrice,UnitPrice+10 結果值 from OrderDetails --查詢結果計算
select max(CategoryID) from Categories --求一列的最大值
select min(CategoryID) from Categories --求一列的最大值
select avg(UnitPrice) 平均價格 from Products --求所有商品的平均價格
select * from Products --求所有商品的平均價格
--select UnitPrice from Products where ProductID<=3 --查詢指定商品的價格
select avg(UnitPrice) from Products where ProductID<=3 --求指定商品的平均價格
1
2
3
4
5
6
7
8
函數查詢
select * from Categories where len(CategoryName)=3 --根據字段長度查詢
select * from Categories where len(PictureFile)=7 --根據字段長度查詢
1
2
條件查詢
select * from Categories where CategoryID=2
select * from Categories where CategoryID<>2
select * from Categories where CategoryID!=2
select * from Categories where CategoryID in(2,4,6)
select * from Categories where CategoryID not in(2,4,6)
select * from Categories where CategoryID>3
select * from Categories where CategoryID>=3 and CategoryID<6
select * from Categories where CategoryID>=3 and CategoryID<6 and CategoryID<>4
select * from Categories where CategoryID<3 or CategoryID>6
select * from Categories where CategoryID<3 or CategoryID>6 or CategoryID=5
select * from Categories where CategoryID between 3 and 5
select * from Categories where CategoryID not between 3 and 5
select * from Categories where CategoryID not between 3 and 5 and CategoryID not in(1,2)
select * from Suppliers where Fax is null
select * from Suppliers where Fax is not null
select * from Categories where CategoryName='穀類/麥片'
select * from Categories where CategoryName like '[谷,米]類/麥片'
select * from Categories where CategoryName like '^[谷,米]類/麥片'
select * from Categories where CategoryName like '_類/麥片'
select * from Categories where CategoryName like '__類/麥片'
select * from Categories where CategoryName like '%/麥片'
select * from Categories where CategoryName like '穀類/%'
select * from Categories where CategoryName like '%/%'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
通配符:
1. %,包含0個或多個字符的任意字符;
2. _,任何單個字符;
3. [],指定範圍([a-f])或集合([abcd])的任何單個字符;
4. [^],不屬於指定範圍([a-f])或集合([abcd])的任何單個字符。

日期查詢
select * from Orders where OrderDate='1996-07-04'
select * from Orders where OrderDate>='1996-01-01' and OrderDate<'1997-01-01'
select * from Orders where OrderDate between '1996-01-01' and '1996-12-31 23:59:59'
1
2
3
分組查詢
select distinct ProductID from OrderDetails --出現過的ProductID(查詢結果不會有重複的值)

select ProductID,count(ProductID) 訂單數量,sum(Quantity) 該類總量 from OrderDetails group by ProductID --按ProductID分組,並求得每種的出現次數,與該種類的數量總和

select ProductID,count(ProductID) 訂單數量,sum(Quantity) 該類總量 from OrderDetails group by ProductID having sum(Quantity)<200 --在上面分組查詢的基礎上添加新的條件

select ProductID,count(ProductID) 訂單數量,sum(Quantity) 該類總量 from OrderDetails group by ProductID having sum(Quantity)<200 and ProductID<>15 --在上面分組查詢的基礎上添加新的條件
1
2
3
4
5
6
7
臨時表
select CategoryID,CategoryName,Description into #TempTab1 from Categories where CategoryID between 3 and 5

select * from #TempTab1
drop table #TempTab1
1
2
3
4
子查詢
select * from Products where SupplierID in(select SupplierID from Suppliers where City='上海')
select Tab1.CompanyName from (select * from Suppliers where City='上海') as Tab1
select CompanyName from (select * from Suppliers where City='上海') as Tab1
1
2
3
聯表查詢
select P.CategoryID,C.CategoryName,P.ProductID,P.ProductName,P.QuantityPerUnit,P.UnitPrice,P.UnitsInStock from Products P join Categories C on P.CategoryID=C.CategoryID

select C.CategoryName,P.ProductID,P.ProductName,P.QuantityPerUnit,P.UnitPrice,P.UnitsInStock from Products P join Categories C on P.CategoryID=C.CategoryID
1
2
3
即使查詢字段裏不存在兩表的 CategoryID,仍可用兩表的 CategoryID 聯表。
連表方式:
1. 交叉連接(cross join):將兩個表不加任何約束地組合起來,在實際應用中一般沒有意義;
2. 內連接(自然連接)([inner] join):將交叉連接按照連接條件進行過濾,匹配的才能出現在結果集,通常採用主鍵=外鍵的形式;
3. 外連接:和內連接的不同是,不匹配條件的行也能出現在結果集,對應的空位會被填上NULL,左外連接(left join, left outer join)是對左表不加限制,右外連接(right join, right outer join)是對右表不加限制,全外連接(full join, full outer join)是對左右兩表都不加限制。

合併查詢
select CategoryID,CategoryName from Categories where CategoryID<=4 union select CategoryID,CategoryName from Categories where CategoryID>4 --將兩個或兩個以上的查詢結果合併
1
邏輯查詢case
select LastName+FirstName as 姓名,TitleOfCourtesy as 稱謂 from Employees
select LastName+FirstName 姓名,case Gender
when 0 then '女' 
when 1 then '男' 
end as 性別 from Employees

select LastName+FirstName 姓名,case TitleOfCourtesy
when '女士' then '女孩' 
when '先生' then '男孩' 
else '未知' 
end as 稱謂 from Employees
1
2
3
4
5
6
7
8
9
10
11
select 與 print
print 123
select 123
select 123 as Result
select 123 Result
1
2
3
4
邏輯查詢 ifelse,convert 類型轉換
declare @name nvarchar(10) set @name='點心'
if exists(select CategoryName from Categories where CategoryName=@name)
    begin 
    print '存在 '+@name --可用select
    declare @id int
    select @id=CategoryID from Categories where CategoryName=@name
    print 'ID: '+convert(varchar,@id)
    end
else print '不存在 '+@name--可用select

if(select CategoryID from Categories where CategoryName='點心')=3 print 'Right' else print 'Wrong'
1
2
3
4
5
6
7
8
9
10
11
時間控制 waitfor
waitfor delay '00:00:03'--等待3秒
select '11'
waitfor time '17:44:03'--等待到具體時間
select '22'
1
2
3
4
獲取時間 getdate, datename
select getdate()
select datename(year,getdate())
select datename(month,getdate())
select datename(day,getdate())
select datename(hour,getdate())
select datename(minute,getdate())
select datename(second,getdate())
select datename(millisecond,getdate())
select datename(year,getdate())+'-'+ datename(month,getdate())+'-'+datename(day,getdate())
1
2
3
4
5
6
7
8
9
循環控制 while
declare @i int set @i=1
while 1=1
    begin
    if @i<10 
        begin
        print @i 
        set @i=@i+1
        end
    else break
    end
1
2
3
4
5
6
7
8
9
10
視圖查詢
create view Categories_Products as select P.CategoryID,C.CategoryName,P.ProductID,P.ProductName,P.QuantityPerUnit,P.UnitPrice,P.UnitsInStock from Products P join Categories C on P.CategoryID=C.CategoryID

select * from Categories_Products --查詢視圖

exec sp_helptext Categories_Products --查詢視圖的創建語句

exec sp_help Categories_Products --查看視圖結構

create view Categories_Products with encryption as select P.CategoryID,C.CategoryName,P.ProductID,P.ProductName,P.QuantityPerUnit,P.UnitPrice,P.UnitsInStock from Products P join Categories C on P.CategoryID=C.CategoryID --創建視圖並加密,加密後不能使用 exec sp_helptext 查看它的創建語句

alter view Categories_Products with encryption as select P.CategoryID,C.CategoryName,P.ProductID,P.ProductName,P.QuantityPerUnit,P.UnitPrice,P.UnitsInStock from Products P join Categories C on P.CategoryID=C.CategoryID --加密視圖

drop view Categories_Products --刪除視圖
1
2
3
4
5
6
7
8
9
10
11
12
13
用視圖修改數據表的數據
若視圖字段來自表達式或常量,則只能進行delete操作;
若視圖字段來自集合函數,則不允許修改操作;
若視圖定義中含group by子句,則不允許修改操作;
若視圖定義中含有distinct短語,則不允許修改操作;
在一個不允許修改操作視圖上定義的視圖,不允許修改操作。
update Categories_Products set ProductName='牛奶2' where ProductID=2
update Categories_Products set ProductName='牛奶' where ProductID=2
1
2
修改與刪除數據
修改與刪除的 where 條件與條件查詢的語法相同。

select * from Categories
update Categories set CategoryName='牛奶2' where CategoryID=2
update Categories set CategoryName='牛奶2',Description='暫無描述' where CategoryID=2
delete from Categories where CategoryID=2
delete from Categories --刪除指定表內全部數據:有刪除記錄,可恢復
truncate table Categories --刪除指定表內全部數據(能重置主鍵ID的遞增起始數爲1):速度快,無刪除記錄,不可恢復,不可刪除有外鍵的表
————————————————
版權聲明:本文爲CSDN博主「petezh」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/petezh/article/details/81744922

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