SQL server 笔记

114 阅读4分钟

学习 SQL server 笔记

数据基本 CRUD

1.新增数据

use 数据库名

go

insert into 表名([name],sex,age).value('张三','男','20')

2.查询数据


select * from 表名          //查询表里面的全部数据

select [name] from 表名     //查询表里面的name关键字

select sex from 表名        //查询表里面的性别关键字

select age from 表名        //查询表里面的年龄关键字

3.修改数据


update 表名 set [name]='小红',sex='女',age='18' where id=1   ///修改 id 为 1 的数据 

4.删除数据


delete 表名 where id=1 //使用 delete 删除数据时一定给 where 条件

进阶查询

1.别名查询


select 
[Id] as '标识ID',
[name] as '名称'
[sex] as '性别',
[age] as '年龄'
from 表名

2.条件查询


select 
[Id] as '标识ID',
[name] as '名称'
[sex] as '性别',
[age] as '年龄'
from 表名 where age=18


3.范围查询


select 
[Id] as '标识ID',
[name] as '名称'
[sex] as '性别',
[age] as '年龄'
from 表名 where age > 18 and age < 24

--  where age between 18 and 24  等同什么的查询条件(小的条件在前面,大的条件在后面)

4.null判断


select 
[Id] as '标识ID',
[name] as '名称'
[sex] as '性别',
[age] as '年龄'
from 表名 where age is null

5.查询前多少行/按比例查询结果


select top 3 * from 表名           // 查询前三条数据

select top(50) percent * from 表名 // 查询总条数的百分之五十


6.case when 判断


//进行比较分支

select top(1000) [Id]
        ,[name]         //名称
        ,[course]       //班级
        ,[score]        //分数
        ,case when score <90 then '不及格'
              when score >=90 and score <120 then '及格'
              when score >=120 and score <130 then '良好'
              when score >=130 then '优秀'
              else '鸭蛋'
              end as '等级'
from 表名 order by [score] asc //根据分数进行升序

//进行对比分支

select top(1000) [Id]
        ,[name]
        ,[course]
        ,[score]
        ,case course when '高级班' then '走向高级开发'
                     when '架构班' then '走向架构师'
                     when '全栈班' then '走向全栈开发'
                     end '开发方向'

7.in查询


select top(1000) [Id]
        ,[name]
        ,[course]
        ,[score]
from 表名 where id in (4,5,6,7,8)       //指定查询 id 为 (4,5,6,7,8)的数据

8.like模糊查询

**模糊查询笔记和通配符 % 一起使用才有效果 % 表示可以匹配任何字符


select top(1000) [Id]
        ,[name]
        ,[course]
        ,[score]
from 表名 where course like '高%'

9.with 关键字查询


 with tt as (select top(1000) [Id]
        ,[name]
        ,[course]
        ,[score]
from 表名)

select * from tt

//whit个关键字查询 相当于给一段语句设置了一个别名 后期进行链表或者复杂的查询都方便一些

10.子查询/exists关键字查询

//子查询

select * from 表名 where id in (select Id from 表名 where [name]='张三')

//exists 关键字

select [Id]
    ,[name]
    ,[course]
    ,[score]
from 表名 t1(表别名)
where exists (select [Id],[name],[course],[scors] from 表名 t2(表别名) where t1.id = t2.id and t2.name = '张三')

11.复制新表/表数据复制

//复制新表

select * into 复制表的表名 from 被复制的表名 


//把另外一个结构相同的表数据复制到指定表中

insert 被插入数据的表名 select [name],[course],[scors] from 被复制数据的表名

12.distinc 同一列去除重复


select distinct course from 表名 order by couse desc

13.排序


select * from 表名 order by score asc(升序)

select * from 表名 order by score desc(降序)

select * from 表名 order by [name],course asc (可以多列排序,从左往右优先级)
 

14.聚合查询分组


// group by (分组)

 select [name],sum(score) score from 表名 group by [name]

15.分页查询


分页查询一

declare @pagesize int;          //每一页数据数量
select pagesize=5;              //查询页面数据的条数

declare @pageindex int;         //第几页
select @pageindex=1;            //第一页

select top(@pagesize) *  from 表名
where id not in 
(
        select top(@paegsize * (@pageindex -1)) id from 表名 order by id
)

order by id


分页查询二 -- 如果 Id 没有自增 不是 int 类型

declare @pagesize int;
select @pagesize=5;

declare @pageindex int;
select @pageindex=1;

select top(@pagesize) * from
(
select row_number() over (order by score) as rownumber,* from ScoreInfo
)A

where rownumber > ((@pagesize)*(@pageindex) - 1)

分页查询三 -- 要求必须在 SQL Server2012版本之后可支持

declare @pagesize int;
select @pagesize=5;

declare @pageindex int;
select @pageindex=1;

select * from 表名 order by Id

offset (@pagesize * (@pageindex - 1)) --间隔多少条

rows fetch next (@pagesize) --获取多少条

rows only

16.union/umion all 操作