【SQL server】SQL server基础(一)
一、关系型数据库
关系型数据库的基本元素是二维表,这些二维表可以被独立或者通过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
select * 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 --desc=descending order ; asc=ascending order --按照listprice的降序显示 select ProductID, Name, ProductNumber, Color, Size, ListPrice
from Production.Product
order by listprice desc,Name --按照listprice的降序,Name的升序显示(没有写des或asc默认以升序排列) select ProductID, Name, ProductNumber, Color, Size, ListPrice
from Production.Product
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)"+"关键字:将列与字符串连接
select ProductID, Name as ProductName, --using an alias
'The ProductNumber ' + ProductNumber + '.'as ProductNumber , --using the concatenation to join character end-to-end.
'The list price for ' + ProductNumber + ' is $ ' + convert(varchar,ListPrice) +'.' as [Description] --convert函数转换类型
from [Production].[Product]
执行后:

4)算数表达式
select BusinessEntityID
,rate*40*52 as AnnualSalary --Annual salary的值为rate*40*52
,round(rate*40*52,1) as AnnualSalary --rate*40*52,结果保留一位小数
,round(rate*40*52,0) as AnnualSalary --rate*40*52,结果保留两位小数
from [HumanResources].[EmployeePayHistory]
3.WHERE..关键字
WHERE 子句中的运算符

1)or 或and
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' --比较符=,>,<,>=,<=,<>,and且,or或orderdate在2005-08-1到2006-01-01之间 select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID=275 and TotalDue>5000 and Orderdate >= '2005-08-01' and Orderdate < '1/1/2006' --orderdate大于等于2005-08-01且小于等于2006-01-01
select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
from [Sales].[SalesOrderHeader]
where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000 --ID=275或278中 Due>5000的结果
2)like“%”或“_”通配符
select * from [Production].[Product]
where name like'Mountain' --name ='Mountain' select * from [Production].[Product]
where name like'%Mountain%' --Wildcard % matches any zero or more characters --筛选出name中含有Mountain的结果,例如...Mountain...,Mountain...,...Mountain select * from [Production].[Product]
where name like'mountain%' -- "_" matches any single character --筛选出name中以Mountain开头的结果,例如Mountain.... select * from [Production].[Product]
where name like'_ountain%' --‘_’表示任意字符,如countains
3)in 和not in
select * from [Production].[Product]
where color in ('red','white','black') --即color='red' or color='white' or color='black' select * from [Production].[Product]
where class not in ('H') -- same as using: <> 'H' --即clas <>'H',有些地方不等于可以用!=
4)is null 和is not null
3.聚合函数
1)常用函数
select count(SalesPersonID) --计算数量
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null select distinct(SalesPersonID) --列出不同的值,如有1,1,1,2,2,3,4,则结果为1,2,3,4
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null select count(distinct(SalesPersonID)) --计算不同值的数量
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
2)集合运算
select
Avg(TotalDue) as AverageTotalSales --取平均值
,Min(TotalDue) as MinimumTotalSales --取最大值
,Max(TotalDue) as MaximumTotalSales --取最小值
,Sum(TotalDue) as SummaryTotalSales --取总和
from [Sales].[SalesOrderHeader]
*注,如果select中同时包含集合函数和非集合函数,则非集合函数要放到group by中
select SalesPersonID,Max(TotalDue) as MaximumTotalSales
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null
group by SalesPersonID --取每个salespersonID对应的最大值
order by SalesPersonID
3)经典的T-SQL语句
select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales --结果显示的列
from [Sales].[SalesOrderHeader]
where SalesPersonID is not null and OrderDate >='2007/1/1' --限制条件为ID不为空,orderdate大于等于2007-01-01
group by SalesPersonID,OrderDate
having Max(TotalDue)>150000 --筛选出total due大于1500000的对应信息
order by OrderDate desc --以order date降序排列
【SQL server】SQL server基础(一)的更多相关文章
- .NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1)
原文:.NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1) 一.SQL Server 为什么要与CLR集成 1. SQL Server 提供的存储过程.函数 ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
C#构造方法(函数) 一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...
- 13Microsoft SQL Server SQL 高级事务,锁,游标,分区
Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...
- SQL Server SQL性能优化之--通过拆分SQL提高执行效率,以及性能高低背后的原因
复杂SQL拆分优化 拆分SQL是性能优化一种非常有效的方法之一, 具体就是将复杂的SQL按照一定的逻辑逐步分解成简单的SQL,借助临时表,最后执行一个等价的逻辑,已达到高效执行的目的 一直想写一遍通过 ...
- SQL Server SQL分页查询
SQL Server SQL分页查询的几种方式 目录 0. 序言 1. TOP…NOT IN… 2. ROW_NUMBER() 3. OFFSET…FETCH 4. 执行 ...
- Qt调用Server SQL中的存储过程
Server SQL中的存储过程如下: CREATE procedure PINSERTPC @pcnum int, @pcname varchar(50), @pctype int, @ipaddr ...
- 对SQL Server SQL语句进行优化的10个原则
1.使用索引来更快地遍历表. 缺省情况下建立的索引是非群集索引,但有时它并不是最佳的.在非群集索引下,数据在物理上随机存放在数据页上.合理的索引设计要建立在对各种查询的分析和预测上.一般来说:①.有大 ...
- MySQL连接数据库报时区错误:java.sql.SQLException: The server time zone value
连接MySQL数据库时报以下时区错误信息: java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized ...
- 连接mysql数据库报错java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized...解决方法
今天连接mysql数据库报错如下: java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or r ...
随机推荐
- 123457123457#0#-----com.yuming.YiZhiFanPai01--前拼后广--益智早教游戏记忆翻牌cym
com.yuming.YiZhiFanPai01--前拼后广--益智早教游戏记忆翻牌cym
- (八)UML之状态图
一.概念 状态图(Statechart Diagram)主要用于描述一个对象在其生存期间的动态行为,表现为一个对象所经历的状态序列,引起状态转移的事件(Event),以及因状态转移而伴随的动作(Act ...
- python 类型注解
函数定义的弊端 python 是动态语言,变量随时可以被赋值,且能赋值为不同类型 python 不是静态编译型语言,变量类型是在运行器决定的 动态语言很灵活,但是这种特性也是弊端 def add(x, ...
- (IStool)64位软件安装在32位操作系统时给出提示
需求:64位的软件当在32位操作系统下安装时,需要提示用户不能在32位操作系统中进行安装 实现:打包时启用64位模式(打包工具用的是Inno Setup 5) 安装脚本段需要添加以下代码: [Setu ...
- python数据结构_递归_汉诺塔问题
已经不是第一次写这个汉诺塔问题, 其实递归还真是不太好理解, 因为递归这种是想其实有点反人类, 为什么? 因为不太清楚, 写个循环一目了然, 用递归其实要把核心逻辑理清楚, 要不根本没法进行下去 所有 ...
- mysql数据恢复,binlog详解
个人博客:mysql数据恢复,binlog详解 binlog日志恢复数据,是挽救错误操作和数据损坏一根救命稻草,所以认识和使用binglog对于技术人员还是很有必要的 binlog一般用于 主从复制 ...
- [CF1070A]Find a Number_bfs
Find a Number 题目链接:http://codeforces.com/problemset/problem/1070/A 数据范围:略. 题解: 因为$d$和$s$比较小可以搜. 这就很$ ...
- S12. Android 检查更新功能实现
[概述] 不需要从 App Store 或者指定官网直接下载,可以通过 App 直接更新到最新版本. [流程设计] 显示当前版本信息以及版本更新日志 提供 “检查更新” 按钮,点击事件处理逻辑: 1) ...
- [转帖][思路/技术]Mimikatz的多种攻击方式以及防御方式
[思路/技术]Mimikatz的多种攻击方式以及防御方式 https://bbs.ichunqiu.com/thread-53954-1-1.html 之前学习过 抄密码 没想到还有这么多功能. ...
- QT QcustomPlot的简单使用
第一步.QcustomPlot是QT提供的一个第三方库,在使用前需要在QcustomPlot官网上进行下载. 第二步.把解压完的QcustomPlot压缩包中的qcustomplot.h和qcusto ...