SQL Server 数据库基础笔记分享(上)

前言

本文是个人学习SQL Server 数据库时的以往笔记的整理,内容主要是对数据库的基本增删改查的SQL语句操作约束,视图,存储过程,触发器的基本了解。

注:内容比较基础,适合入门者对SQL Server 数据库的了解!!!

正文

1.主键:

主键的作用:保证表中的每条数据的唯一性
特点: 主键不能重复 不能为空
分类:
逻辑主键:选择为表中增加的那些“自动编号”列或者“GUID”列为主键(没有实际业务上的意义)的主键 (建议使用逻辑主键)
业务主键:选择表中那些在业务中有实际意义的列作为主键
》》》》》》》》》选择主键的策略,选什么样的列作为主键《《《《《《《《《
1》主键,建议选择那些一般不会被修改的列
2》选择单列,不选择多列(不用组合主键)
3》选择那些简单列(整数列(自动编号))

 

2.char(),nchar(),varchar()之间的区别

》》》》》》》》》char(10)与varchar(10)的区别《《《《《《《《《
char(10) 固定长度,表示在数据库中存储的时候占用10个字节的空间,如果超出10个则报错,如果不够10个则用空格补全。
varchar(10) 可变长度,表示该列最多可以存储10个字节,如果实际存储不够10个字节,则会在存储的时候自动计算一下实际的存储个数,而动态的改变长度。【节省空间】

》》》》》》》》》char(10)与nchar(10)的区别《《《《《《《《《

char(10) 可以存储10个字母或者5个汉字。 用来存储数据的时候,英文站1个字节,中文站2个字节。

nchar(10) 表示可以存储10个字母或10个汉字,因为每个字符都是按照unicode方法来存储的。当使用nchar(10),来存储数据的时候无论存储的是中文还是英文都是每个字符占2个。

 

3. 创建数据库

--创建一个数据库
create database School

--删除数据库
drop database School

--创建数据库的时候,指定一些数据库的相关参数。
create database School
on primary --主数据文件
(
name='School',
size=10mb,
filename='c:school.mdf',
filegrowth=10%,
maxsize=100mb
)
log on --日志文件
(
name='School_log',
filename='c:school.ldf',
size=5mb,
filegrowth=5mb,
maxsize=50mb
)

--切换数据库
use school
go

4. 创建表

--创建表
create table Class
(
ClassId int identity(1,1) primary key,
ClassName varchar(50) not null,
ClassDesc varchar(50) not null
)

--删除表
drop table Class

--向Class表中插入数据
insert into Class(ClassName,ClsDesc)values('大三','三年');

--insert into...values.. 这种写法每次只能插入一条数据

--向Class表中插入多条数据
--重复数据不重复插入,union关键字本身就具有去掉重复的意思
--union | union all (重复插入)
insert into Class
select '大三','三年' union
select '三五','间谍' union
select '一一','多久' union
select '六七','得到'


--将Class表中的数据备份到Student表中
--这种写法会将Class表中的所有数据插入到Student表中
--前提是Student表不存在,如果这个表存在则报错。
select * into Student from Class


--向一个已经存在的表中插入数据,数据的来源是Class表
insert into Student(ClassName,ClsDesc)
select ClassName,ClsDesc from Class

 

--查询表中数据
select * from Class

5.update 数据

--将所有年龄小于20岁的人的年龄都改成19(tage是Class表后加属性)
update Class set tage=19 where tage<20

--将年龄为19岁的并且性别为0的人的姓名两边★改为☆
update Class set ClassName =replace (tname,'★','☆') where tage=19 and tgender=0

6.删除数据

delete from Class --删除所有数据 自动编号没有恢复到默认值 可以根据条件来删除
truncate table Class --重新设置了自动编号 删除只能一次性都清空,不能根据条件来删除 清除速度(性能)比delete语句快的多


delete from Class where tage=19 or tage is null --删除19岁或者空值

》》》》》》》》》删除重复数据只保留一条(id最小的一条)《《《《《《《《《
》》》》》》》》》删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录 《《《《《《《《《
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)

 

7.条件查询,模糊查询

--查询数学没有及格的学生的学号
select
fid as 学号,
fmath as 分数
from MyStudent where fmath<60

--查询年龄在20-30岁之间的男学生
select
fname as 姓名 from MyStudent where fage between 20 and 30 and fgender='男'

--查询班级id 1 2 3 的所有学生
select * from MyStudent where classid in (1,2,3)

--查询所有姓赵的同学 (通配符%表示:任意多个任意字符)
select * from MyStudent where fname like '赵%'

--查询出姓名中只要包含一个‘民’字即可。
select * from MyStudent where fname like '%民%'

--查询所有姓赵的同学,并且姓名字数是3个
--通配符 _ :表示任意的单个字符。
select * from MyStudent where fname like '赵__'
select * from MyStudent where fname like '赵%' and len(fname)=3

--查询出姓名中包含‘民’或‘用’的同学
--通配符[]:表示中括号中的任意个字符,只选一个匹配
--通配符 ^a :表示除了a这个字符都行。
select * from MyStudent where fname like '%[民用]%'

8.聚合函数

--查询数学成绩最高低分
select max(fMath) as 数学成绩最高分 from MyStudent
select min(fMath) as 数学成绩最低分 from MyStudent

--平均分(计算平均分的时候对空值不处理)
select avg(fMath) as 平均分 from MyStudent

--求数据记录中的总条数(总人数)
select count(*) as 班级总人数 from MyStudent


select
最高分=(select max(fMath) as 数学成绩最高分 from MyStudent),
最低分=(select min(fMath) as 数学成绩最低分 from MyStudent),
平均分=(select avg(fMath) as 平均分 from MyStudent)


--分数评级
--90以上 优秀
--80以上 良好
--70以上 中
--70以下 差
select chengji,
评级=
case
when shuxue>=90 then '优秀'
when shuxue>=80 then '良好'
when shuxue>=70 then '中'
else '差'
end
from Student

9.null 问题

--请查询出学生表中所有数学成绩为null的人的信息
--null在数据库中表示unknow(不知道),判断一个值是否为null,也就不能用=或者<>来判断
select * from MyStudent where fMath=null 错误(不返回任何数据)

正确 select * from MyStudent where fMath is null

--查询所有fmath为非null的值
select * from MyStudent where fMath is not null


--null值与任何数据运算后得到的还是null值。
update MyStudent set fage=fage+1 where fid=1

10.分组group by

--统计出mystudent表中,男女同学的个数

select
fgender as 性别, --这时,count(*)统计的是每一组的记录条数, 不是总条数
count(*) as 人数
from MyStudent group by fgender --先执行group by语句分组,分完组在统计每 组个数。 分出来几个组,那么count(*)就统 计几次


--查询班级的男同学的人数大于2的信息

--having是group by的条件对分组后的数据进行筛选(与where类似,都是筛选,只不过having是用来筛选分组后的组的)
select
classid as 班级号,
count(*) as 班级人数
from TblStudent
where fgender='男'
group by classid
having count(*)>2

》》》》》》》》》语句执行顺序《《《《《《《《《

select
--distinct / top 之类的关键字
fgender as 性别, --5》选择列
count(*) as 人数
from MyStudent --1》先从表中拿到数据
where fage>30 --2》从MyStudent的数据中筛选出所有年龄大于30岁的任的信息
group by fgender --3》按照性别分组,分完组得到一个新的结果集
having count(*)>500 --4》基于分组以后的结果集,然后再筛选,筛选出那些组中记录大于500的组
order by 人数 asc --6》最后把显示出来的结果排序


--语句执行顺序
from > where > group by > having > select > order by

11.日期函数

--请查询出所有入职一年以上的员工信息
select * from TblStudent
where dateadd(year,1,tsday)<getdate()


--计算两个时间差
--查询90年距今是多少年
select datediff(year,'1990-9-9',getdate())


--查询一个日期的特定部分
select year(getdate())
select datepart(year,getdate())

--输出所有数据中通话时间最长的5条记录。
select top 5 *,'通话时长(秒)'=datediff(second,Startdatetime,Enddatetime) from Calltecords order by datediff(second,Stardatetime,enddatetime) desc

后记

下篇分享视图、触发器等,分页查询、子查询、连表查询等

 

posted @ 2018-07-14 12:27  漫步前的奔跑  阅读(640)  评论(1编辑  收藏  举报