第1章 SQL安装
- 1-1 [SQL Server基础]前言 (10:44)
- 1-2 SQL Server安装 (08:29)
- 1-3 第一次登陆 SQL Server (02:58)
- 1-4 [SQL Server基础]附加、分离(Attach、Detach)数据库文件 (03:21)
- 1-5 [SQL Server基础]数据库图表关系图(ER图) (04:22)
简单记忆:一个table中的primary Keys是另一个Table中的Foreign Key
重点::区分好主键和外键
第2章 T-SQL语句
实际工作中少用*,因为可能会出现成千上万的数据
select Top 100 * from [Production].[Product] 选择前一百行数据
select ProductID, Name, ProductNumber, Color, Size, ListPrice
from Production.Product
按信息读取数据
select ProductID, Name, ProductNumber, Color, Size, ListPrice
from Production.Product
order by listprice desc //安装listprice倒序排序
asc=ascending order //正序排序
第一个按照listprice倒序排序,第二个按照listprice和Name倒序排序
按照第2个Name正序排序
isnull(size,'') 空值null变换为单引号‘’里面的数据
改变列的名称
select ProductID, Name as ProductName, --using an alias
'The list price for ' + ProductNumber + ' is $ ' + convert(varchar,ListPrice) +'.' ,--using the concatenation to join character end-to-end.
'The list price for ' + ProductNumber + ' is $ ' + convert(varchar,ListPrice) +'.' as [Description] --using brackets to let SQL server conside the strin as a column name
from Production.Product
在where语句中用>,=,<等字符
eg:
select * from [Sales].[SalesOrderHeader]
where SalesPersonID=275
select * from [Sales].[SalesOrderHeader]
where SalesOrderNumber='so43670' //string类型加单引号
where语句中使用or或and
eg:
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID=275 and TotalDue>5000 and Orderdate between '2005-08-01' and '1/1/2006'
like中使用%号 //%表示可以有,可以没有
select * from [Production].[Product]
where name like'Mountain'
select * from [Production].[Product]
where name like'%Mountain%' --Wildcard % matches any zero or more characters
_下划线表示前面有一个不知道的字符
select * from [Production].[Product]
where name like'_ountain%'
Where语句中使用in或not in
select * from [Production].[Product]
where color in ('red','white','black')
select * from [Production].[Product]
where class not in ('H') -- same as using: <> 'H'//没有H的
is null 与is not null
--Topic 10
select * from [Production].[Product]
where size is null
select * from [Production].[Product]
where size is not null
or与and的理解
--Topic 11
select * from [Production].[Product]
where color ='white'or color ='black'
select * from [Production].[Product]
where color ='white'and color ='black'
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000
工作中常用的聚合函数&经典查询语句实例
select count(SalesPersonID) //count--总和
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
select distinct(SalesPersonID) //l列出
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
select count(distinct(SalesPersonID)) //数目
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
--Topic 13
select
Avg(TotalDue) as AverageTotalSales --aggregate functions
from [Sales].[SalesOrderHeader]
select
Avg(TotalDue) as AverageTotalSales
,Min(TotalDue) as MinimumTotalSales
,Max(TotalDue) as MaximumTotalSales
,Sum(TotalDue) as SummaryTotalSales
from [Sales].[SalesOrderHeader]
select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by SalesPersonID,OrderDate
having Max(TotalDue)>150000 //大于15万的提取出来
order by SalesPersonID
----The classical T-SQL query!!!
select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null and OrderDate >='2007/1/1'
group by SalesPersonID,OrderDate
having Max(TotalDue)>150000
order by SalesPersonID
--order by OrderDate desc