sql server2012学习笔记
第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

sql server2012学习笔记的更多相关文章
- 【SQL Server学习笔记】Delete 语句、Output 子句、Merge语句
原文:[SQL Server学习笔记]Delete 语句.Output 子句.Merge语句 DELETE语句 --建表 select * into distribution from sys.obj ...
- SQL server2005学习笔记(一)数据库的基本知识、基本操作(分离、脱机、收缩、备份、还原、附加)和基本语法
在软件测试中,数据库是必备知识,假期闲里偷忙,整理了一点学习笔记,共同探讨. 阅读目录 基本知识 数据库发展史 数据库名词 SQL组成 基本操作 登录数据库操作 数据库远程连接操作 数据库分离操作 数 ...
- sql注入学习笔记,什么是sql注入,如何预防sql注入,如何寻找sql注入漏洞,如何注入sql攻击 (原)
(整篇文章废话很多,但其实是为了新手能更好的了解这个sql注入是什么,需要学习的是文章最后关于如何预防sql注入) (整篇文章废话很多,但其实是为了新手能更好的了解这个sql注入是什么,需要学习的是文 ...
- 郝斌–SQL Server2005学习笔记
数据库(Database)狭义上是指存储数据的仓库,广义上包含对数据进行存储和管理的软件(DBMS)和数据本身.数据库由表.关系和操作组成. 一.数据库简介 1.为什么需要数据库 数据库简化了对数据的 ...
- sql注入学习笔记 详解篇
sql注入的原理以及怎么预防sql注入(请参考上一篇文章) https://www.cnblogs.com/KHZ521/p/12128364.html (本章主要针对MySQL数据库进行注入) sq ...
- SQl语句学习笔记(二)
merge into when matched then... when not mached then... merge into t_road_pre_parameter a fr ...
- SQL语句学习笔记
从外部EXCEl文件导入sqlserver数据库操作命令 reconfigure reconfigure go select * into abc1_1 from OPENROWSET('MICROS ...
- SQL server 学习笔记1
1.查询安装的排序规则选项喝当前的排序规则服务器属性 select * from fn_helpcollations(); 2.查看当前服务器的排序规则 select serverproperty(' ...
- sql视图学习笔记--视图
视图是为用户对数据多种显示需求而创建的,其主要用在一下几种情况: (1)限制用户只能访问特定表特定条件的内容,提高系统的安全性. (2)隐藏表结构.创建多种形式的数透视,满足不同用户需求. (3)将复 ...
随机推荐
- NPOI生成excel并下载
NPOI文件下载地址:http://npoi.codeplex.com/ 将文件直接引用至项目中即可,,,,, 虽然网上资料很多,但有可能并找不到自己想要的功能,今天闲的没事,所以就稍微整理了一个简单 ...
- 移动H5页面微信支付踩坑之旅(微信支付、单页面路由模拟、按钮加锁、轮询等常见功能)
开发背景: .net混合开发的vue模板语法的单页面应用,所以不存在脚手架以及没有路由可以跳转. 项目描述: 需要写两个页面,在订单详情页需要点击“请输入手机号”进入手机号绑定页面,手机号绑定成功后自 ...
- HTTP请求中的Keep-Alive模式,是怎么区分多个请求的?
Keep-Alive模式 我们都知道HTTP是基于TCP的,每一个HTTP请求都需要进行三步握手.如果一个页面对某一个域名有多个请求,就会进行频繁的建立连接和断开连接.所以HTTP 1.0中出现了Co ...
- centos 7 network.service control process exited
一.service network restart 出错 问题描述: vmware 12 下centos 7 网络模式,NAT 昨晚作者打算更新自己虚拟机python,发现没网络ping www.ba ...
- 学习 yii2.0——视图之间相互包含
布局 首先创建一个布局文件simple.php,路径是在views/layout/目录下. <p>this is header</p> <?= $content ?> ...
- #Leetcode# 524. Longest Word in Dictionary through Deleting
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ Given a string and a stri ...
- jQuery学习(监听DOM加载)
jQuery的extend方法 function njQuery() { } /* njQuery.extend = function (obj) { // 此时此刻的this就是njQuery这个类 ...
- 字符串和ASCII之间的转换
public class CharToAscii { public static void main(String[] args) { CharToAscii.AscToString(); CharT ...
- Day 5-2 类的继承和派生,重用
类的继承 派生 在子类中重用父类 组合 抽象类 定义: 继承指的是类与类之间的关系,是一种什么“是”什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创建新类的方式,在python中,新 ...
- C# Note30: 软件加密机制以及如何防止反编译
参考文章: C#软件license管理(简单软件注册机制) 软件加密技术和注册机制 .NET中的许可证机制--License 背景 .net是一种建立在虚拟机上执行的语言,它直接生成 MSIL 的中间 ...
