SQL Server(三)
一、数据库操作
create database 数据库名称 ——创建
drop database 数据库名称 ——删除
use 数据库名称 ——使用
go 两条SQL语句之间分隔
二、表的操作
create table 表名( 列名 类型 其它,列名 id类型 其它 ) ——使用
primary key ——主键
identity——自增长列
not null ——非空
unique ——唯一
references ——外键
references 主表名(主表主键列)——设置外键格式
drop table 表名 ——删除
三、数据操作
1、增加数据(关键字:insert)
insert into 表名 values(每一列的值)
insert into 表名(列名) values(值)——给特定列添加值
2、删除数据(关键字:delete)
delete from 表名 where 筛选条件
3、修改数据(关键字:update)
update 表名 set 列名=值,列名=值 where 筛选条件
4、查询数据(关键字:select)
(1)简单查询
select * from 表名
select 列名 from 表名
select 列名 as 别名 from 表名
(2)条件查询 (where or and)
select * from 表名 where 条件1
select * from 表名 where 条件1 or 条件2
select * from 表名 where 条件1 and 条件2
(3)范围查询 (between and)
select * from 表名 where 列名 between 值1 and 值2
(4)离散查询 (in not in)
select * from 表名 where 列名 in(数据列表)
select * from 表名 where 列名 not in(数据列表)
(5)模糊查询 (like %任意多个字符 _任意一个字符)
select * from 表名 where 列名 like ‘%_’
(6)排序查询 ( order by desc降序 asc升序)
select * from 表名 order by 列名 ——默认升序,也可在列名后面加asc
select * from 表名 order by 列名 desc
(7)分组查询 (group by having)
select * from 表名 group by 列名 having 条件 ——having需要跟在group by 后使用
(8)分页查询 (top n 取前n个值)
select top n * from 表名
(9)去重查询 (关键字: distinct )
select distinct 列名 from 表名
(10)聚合函数(统计函数)
select count(*) from 表名
select sum(列名) from 表名
select avg(列名) from 表名
select max(列名) from 表名
高级查询:
1.连接查询(关键字:join on)扩展列
select * from Info,Nation --形成笛卡尔积
select * from Info,Nation where Info.Nation = Nation.Code
select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code
select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式
2.联合查询 (关键字:union)扩展行,一般不用
select Code,Name from Info
union
select Code,Name from Nation
3.子查询
一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。
--查询民族为汉族的所有人员信息 select * from Info where Nation = (select Code from Nation where Name = '汉族')
(1)无关子查询
子查询可以单独执行,子查询和父查询没有一定的关系
--查询系列是宝马5系的所有汽车信息 select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = '宝马5系')
(2)相关子查询
--查找油耗低于该系列平均油耗的汽车
select * from Car where Oil<(该系列的平均油耗) select avg(Oil) from Car where Brand = (该系列)
select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)
select min(列名) from 表名
SQL Server(三)的更多相关文章
- 转 一篇关于sql server 三种恢复模式的文章,从sql server 的机制上来写的,感觉很不错,转了
简介 SQL Server中的事务日志无疑是SQL Server中最重要的部分之一.因为SQL SERVER利用事务日志来确保持久性(Durability)和事务回滚(Rollback).从而还部分确 ...
- SQL Server三种表连接原理
在SQL Server数据库中,查询优化器在处理表连接时,通常会使用一下三种连接方式: 嵌套循环连接(Nested Loop Join) 合并连接 (Merge Join) Hash连接 (Hash ...
- Sql Server 三个很有用的函数
好久没有写有关SqlServer 数据库方面技术的文章了,正好今天遇到了一个问题,我就把这个当做一个练习记录下来.今天遇到一个麻烦事,详情如下:公司买了一个系统,在这个系统里面有一个“充值卡”的功能, ...
- SQL Server(三)——增、删、改、查
一.数据库操作 create database 数据库名称 ——创建drop database 数据库名称 ——删除use 数据库名称 ——使用go 两条SQL语句之间分隔 二.表的操作 create ...
- 《Microsoft SQL Server企业级平台管理实践》笔记
- 页是 SQL Server 中数据存储的基本单位,大小为 8KB. - 区是空间管理的基本单位,8个物理上连续的页的集合(64KB). - 页的类型包括: 1. Data 2. Index 3. ...
- SQL Server 2008中的Hints(提示)的简单整理
SQL Server的系统查询过程 负责在SELECT查询执行时候产生查询执行计划.SQL Server会“智能”地选择一个高效计划来取代低效的一个.大多数时候,SQL Server会把这份工作干得很 ...
- MYSQL和SQL Server 的区别
注意MYSQL使用注释 -- 时 要后面加上空格 使用 #不用 一.数据类型 MYSQL:支持enum和set类型 ;SQL SERVER:不支持 MYSQL:不支持nchar,nvarchar,nt ...
- 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...
- (转) 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
原文地址: http://www.cnblogs.com/lyhabc/p/4682986.html 这一篇是从0开始搭建SQL Server AlwaysOn 的第三篇,这一篇才真正开始搭建Alwa ...
随机推荐
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- Yii 框架 URL路径简化
Yii 框架的訪问地址若不简化会让人认为非常繁琐.未简化的地址一般格式例如以下: http://localhost:80/test/index.php?r=xxx/xxx/xxx 若是带有參数会更复杂 ...
- 【转】数据存储——APP 缓存数据线程安全问题探讨
http://blog.cnbang.net/tech/3262/ 问题 一般一个 iOS APP 做的事就是:请求数据->保存数据->展示数据,一般用 Sqlite 作为持久存储层,保存 ...
- ubuntu16.04固定IP与设置DNS【转】
本文转载自:http://www.jianshu.com/p/ea4bca38e5d7 学校几乎每次断电之后,宿舍的机器IP基本都会更改,即使可以通过图形界面去修改,但还是懒得去改.但是docker某 ...
- LightOJ - 1321 Sending Packets —— 概率期望
题目链接:https://vjudge.net/problem/LightOJ-1321 1321 - Sending Packets PDF (English) Statistics Foru ...
- java进程分析
1. 找出 java进程pid,比如 11327 2. 使用jstack 看下 锁持有情况 /usr/java/latest/bin/jstack -l 11327 3. 输出java堆栈信息,以及 ...
- Mysql中文检索匹配与正则
今天在用sql模糊查询包含字母d的时候,发现一些不包含此字母的也被查询出来了: SELECT * FROM custom WHERE custom_realname LIKE '%d%' 查询了一下, ...
- kvm初体验之三:vm的安装及管理
Host: CentOS release 6.4 (Final) Guest: CentOS release 6.6 (Final) 全程以root身份操作 1. host上创建桥br0 参考< ...
- hdu-3078 Network(lca+st算法+dfs)
题目链接: Network Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- leetcode 191 Number of 1 Bits(位运算)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...