一、关系型数据库
关系型数据库的基本元素是二维表,这些二维表可以被独立或者通过join语句连接起来使用。主键和外键是用来连接二维表之间的主要工具
1.主键(primary key)和外键(foreign key)
主键是来唯一的标识一行数据,而且主键列必须包含唯一值,且不能包含空值(null)
主键可以建立在每张二维表的单列或多列上
一张二维表的外键可以引用某张二维表对应的主键
2.数据库中的关系表
二、SQL语句
1.sql的基本语句
SELECT <table fields list>
FROM <table names list>
WHERE <row constraints specification>
GROUP BY <grouping specification>
HAVING <grouping selection specification>
ORDER BY <order rules specification>
*SELECT...FROM..语句是必须的,从某个表选择某列
WHERE 对行进行限制,例如筛选ID>9,则小于等于9的行就被过滤
GROUPBY 集合运算时添加的一些定义,例如计算age平均值
HAVING 针对集合运算进行限制条件,例如average age>30
ORDERBY 排列,例如有ID,name,age等列,想按照ID排列则可以 ORDERBY ID
2.SELECT....FROM关键字
1)SELECT....FROM
1 select * from [Production].[Product] --*表示所有
2
3 select ProductID, Name, ProductNumber, Color, Size, ListPrice --从表中显示指定列
4 from Production.Product
5
6 select ProductID, Name, ProductNumber, Color, Size, ListPrice
7 from Production.Product
8 order by listprice desc --desc=descending order ; asc=ascending order --按照listprice的降序显示
9
10 select ProductID, Name, ProductNumber, Color, Size, ListPrice
11 from Production.Product
12 order by listprice desc,Name --按照listprice的降序,Name的升序显示(没有写des或asc默认以升序排列)
13
14 select ProductID, Name, ProductNumber, Color, Size, ListPrice
15 from Production.Product
16 order by 2 --2表示Name,按照Name的升序排列,3表示ProductNumber
2)isnull函数,判断是否为空
select ProductID, Name, ProductNumber, isnull(Color,''), isnull(Size,''), ListPrice --将color,size中的空值null替换为‘ ’,列名也为空
from Production.Product
select ProductID, Name, ProductNumber,
isnull(Color,'') as Color, isnull(Size,'') as Size123, --using an alias --as..修改列名
ListPrice
from Production.Product
执行该语句后:
3)"+"关键字:将列与字符串连接
1 select ProductID, Name as ProductName, --using an alias
2 'The ProductNumber ' + ProductNumber + '.'as ProductNumber , --using the concatenation to join character end-to-end.
3 'The list price for ' + ProductNumber + ' is $ ' + convert(varchar,ListPrice) +'.' as [Description] --convert函数转换类型
4 from [Production].[Product]
执行后:
4)算数表达式
1 select BusinessEntityID
2 ,rate*40*52 as AnnualSalary --Annual salary的值为rate*40*52
3 ,round(rate*40*52,1) as AnnualSalary --rate*40*52,结果保留一位小数
4 ,round(rate*40*52,0) as AnnualSalary --rate*40*52,结果保留两位小数
5 from [HumanResources].[EmployeePayHistory]
3.WHERE..关键字
WHERE 子句中的运算符
1)or 或and
1 select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
2 from [Sales].[SalesOrderHeader]
3 where SalesPersonID=275 and TotalDue>5000 and Orderdate between '2005-08-01' and '1/1/2006' --比较符=,>,<,>=,<=,<>,and且,or或orderdate在2005-08-1到2006-01-01之间
4
5 select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
6 from [Sales].[SalesOrderHeader]
7 where SalesPersonID=275 and TotalDue>5000 and Orderdate >= '2005-08-01' and Orderdate < '1/1/2006' --orderdate大于等于2005-08-01且小于等于2006-01-01
1 select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
2 from [Sales].[SalesOrderHeader]
3 where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000 --ID=275或278中 Due>5000的结果
2)like“%”或“_”通配符
1 select * from [Production].[Product]
2 where name like'Mountain' --name ='Mountain'
3
4 select * from [Production].[Product]
5 where name like'%Mountain%' --Wildcard % matches any zero or more characters --筛选出name中含有Mountain的结果,例如...Mountain...,Mountain...,...Mountain
6
7 select * from [Production].[Product]
8 where name like'mountain%' -- "_" matches any single character --筛选出name中以Mountain开头的结果,例如Mountain....
9
10 select * from [Production].[Product]
11 where name like'_ountain%' --‘_’表示任意字符,如countains
3)in 和not in
1 select * from [Production].[Product]
2 where color in ('red','white','black') --即color='red' or color='white' or color='black'
3
4 select * from [Production].[Product]
5 where class not in ('H') -- same as using: <> 'H' --即clas <>'H',有些地方不等于可以用!=
4)is null 和is not null
3.聚合函数
1)常用函数
1 select count(SalesPersonID) --计算数量
2 from [Sales].[SalesOrderHeader]
3 where SalesPersonID is not null
4
5 select distinct(SalesPersonID) --列出不同的值,如有1,1,1,2,2,3,4,则结果为1,2,3,4
6 from [Sales].[SalesOrderHeader]
7 where SalesPersonID is not null
8
9 select count(distinct(SalesPersonID)) --计算不同值的数量
10 from [Sales].[SalesOrderHeader]
11 where SalesPersonID is not null
2)集合运算
1 select
2 Avg(TotalDue) as AverageTotalSales --取平均值
3 ,Min(TotalDue) as MinimumTotalSales --取最大值
4 ,Max(TotalDue) as MaximumTotalSales --取最小值
5 ,Sum(TotalDue) as SummaryTotalSales --取总和
6 from [Sales].[SalesOrderHeader]
*注,如果select中同时包含集合函数和非集合函数,则非集合函数要放到group by中
1 select SalesPersonID,Max(TotalDue) as MaximumTotalSales
2 from [Sales].[SalesOrderHeader]
3 where SalesPersonID is not null
4 group by SalesPersonID --取每个salespersonID对应的最大值
5 order by SalesPersonID
3)经典的T-SQL语句
1 select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales --结果显示的列
2 from [Sales].[SalesOrderHeader]
3 where SalesPersonID is not null and OrderDate >='2007/1/1' --限制条件为ID不为空,orderdate大于等于2007-01-01
4 group by SalesPersonID,OrderDate
5 having Max(TotalDue)>150000 --筛选出total due大于1500000的对应信息
6 order by OrderDate desc --以order date降序排列