【SQL Server】基础快速复习

本文介绍了SQL的基本组成部分,包括DML、DCL、DQL和DDL操作,以及如何进行数据的增删改查。此外,还详细讲解了注释、查询优化、模糊查询、子查询、分页、函数运用、聚合函数和分组查询,以及不同类型的表连接查询,如内连接、外连接。内容全面,适合SQL初学者学习。
摘要由CSDN通过智能技术生成

一、基础知识

SQL的组成

  • DML(数据操作):insert、update、delete
  • DCL(数据控制):grant、revoke
  • DQL(数据查询):select
  • DDL(数据定义):create table、drop table

注释

--两条杠就是sqlserver注释
#这个符号是mysql的注释

简单的增删改查

语法:insert into 表名 (列名) values (值列表)

#演示
insert into student (name,age) values ('蔡徐坤',);

语法:update 表名 set 字段=值 where 条件

#演示
update student set name='李四' where id=1;

语法:delete from 表名 where 条件

#演示
delete from student where name='李四' ;

  1. 基础查询

    • 语法:select * from 表名
  2. 列别名

    • 使用as命名列

      #将id列取名为学号
      select id as 学号 from student;
      
    • 使用=命名列

      #将id列取名为学号
      select id = 学号 from student;
      
    • 使用空格命名列

      #将id列取名为学号
      select id  学号 from student;
      
  3. 查询空行和常量列

    • 查询空值:查询语句 where 字段 is null

      #查询address为空的学生
      select * from student where address is null or address=''
      
    • 常量:select ‘常量’ as 别名 from 表名

      select name,'电校' as 学校 from student
      
  4. 限制查询行数

    • 方法1:固定数值

      #显示前5行数据
      select top 5 * from student
      
    • 方法2:百分比

      #显示前20%数据
      select top 20 precent * from student
      
  5. 排序

    • 查询语句 order by 字段名 score/desc

    • order by (升序:score,降序:desc)

      #根据年龄降序排列
      select * from student order by age desc
      

二、模糊查询

  1. 通配符:一类字符,经常与like关键字一起使用

    • _:匹配一个字符

      #查询姓名两个字,姓张的学生
      select * from student where name like '张_'
      
    • %:匹配多个字符

      #查询姓张的学生,名字字数不限
      select * from student where name like '张%'
      
    • [ ]:指定范围内的一个字符

      #查询学号范围10~20的学生
      select * from student where id like [10,20]
      
    • #查询学号不在范围10~20的学生
      select * from student where id like [^10,20]
      
  2. like

  3. in

    查询年龄为18,20,21的学生

    select * from student where age in(18,20,21)
    
  4. between…and(用于闭区间)

    查询80分~100分的学生

    使用运算符

select * from student where result>=80 and result <=100

使用between…and

select * from student where result between 80 and 100

三、子查询

查询年龄比小明大的学生

  • 先查询小明的年龄
  • 添加查询条件:出生日期>小明出生日期
select * from student where age > (select age from student age where name = "小明")

查询没参加语文考试的学生

  • 查询参加过语文考试的学生学号
  • 查询学生,条件:学号 not in 列表中
select * from student where id not in (select id from result where subject = "语文")

查询人数大于40的年级信息

  • 查询出班级的学生人数 count
  • 查询筛选班级
select * from  class c where (select count(*) from student where class = c class)>3

分页查询

分页查询学生信息 top

#查询结果以5条分一页,查询第二页
select top 5 * from student
where id not in(select top 10 id from student)

四、函数

使用方法(len()为例):

#直接调用
select len(abcd)
#语句中使用
select name,len(name) as 名字长度 from student

字符串函数

  • len( ):返回字符串长度
  • upper() : 全部转为大写
  • right():指定返回字符串的数目字符
  • replace(z,x,y):替换字符串内容,将z字符串中的x字符替换为y

日期函数

  • getDate():获取当前日期
  • dateAdd():将指定的数值添加到指定的日期部
    分后的日期
  • dateDiff():两个日期之间的指定日期部分的间
  • dateName():日期中指定日期部分的字符串形式
  • datePart():日期中指定日期部分的整数形式

数学函数

  • rand():随机数
  • right(rand(),x):返回x位的随机数

系统函数

  • convert():转换数据类型

五、聚合函数和分组查询

聚合函数:对一-组值进行计算,并返回计算后的值,具有统、计数据的作用

  1. 5个聚合函数

    • sum():求和
    • avg():求平均值
    • max(), min():求最大值,最小值
    • count():计算数量
  2. 分组查询(group by)

    统计每个年级的人数

    select count(*) as 人数,gradeld as 年级 from 
    from student group by gradeld
    
  3. 分组筛选(having)

人数大于3的年级

select count(*) as 人数,gradeld as 年级 from 
from student group by gradeld
having count(*)>3

where,group by,having的执行顺序

where => group by => having

六、表连接查询

  1. 常用表连接的分类

    • 内连接(inner join)

      //两张表共有的数据才会输出
      
    • 外连接(左连接left join 右连接 right join)

      //以某一张表为基准,数据都会显示,如果没有对应的数据,显示weinull
      
  2. 内连接查询

    查询学生的成绩,要求输出姓名、分数

    select s.name,r.num
    from student s,result r
    where s.id = r.id
    
  3. 外连接查询

    • 左连接(left join)

      将左表的条数全部列出来

      select 编号,名称,作者
      from 图书表
      left join 作者表
      on 图书表.id = 作者表.id
      
    • 右连接( right join)

      将右表的条数全部列出来

      select 编号,名称,作者
      from 图书表
      right join 作者表
      on 图书表.id = 作者表.id
      
  4. 三表连接查询

    和内连接用法一样

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值