数据库使用 | 青训营

114 阅读6分钟
  1. 数据库:是结构化信息或数据的有序集合,一般以电子形式存储在计算机系统中。通常由数据库管理系统(DBMS)来控制。数据、DBMS及关联应用一起被称为数据库系统,简称数据库。

  2. 数据库类型:(优劣取决于企业希望如何使用数据)

    关系数据库:把数据以表的形式进行储存,然后在各个表之间建立关系,通过这些表之间的关系来操作不同表之间的数据

    非关系数据库:NoSQL或非关系数据库,支持存储和操作非结构化及半结构化数据。数据之间不存在表与表之间的关系,数据之间可以是独立的。放弃了强事务保证和关系模型,通过最终一致性和非关系数据模型来提高web应用所注重的高可用性和高扩展性

    单机数据库:在一台计算机上完成数据的存储和查询的数据库系统

    分布式数据库:由位于不同站点的两个或多个文件组成。数据库可以存储在多台计算机上,位于同一个物理位置,或分散在不同的网络上

    OLTP数据库:(Online transactional processing)高速分析数据库,专为多个用户执行大量事务而设计

    OLAP数据库:(Online analytical processing)分析多个数据维度,帮助团队更好地理解数据中的复杂关系。大量数据读写,PB级别的存储;多维分析,复杂的聚合函数;离线/实时分析,对查询速度有要求

  3. SQL:(Structured Query)一种编程语言,目前几乎所有关系数据库都使用SQL来查询、操作和定义数据,进行数据访问控制

  4. DQL查询的完整语法:

    select 字段列表 from 表名列表 where 条件列表 group by 分组字段 having 分组后条件 order by 排序字段 limit 分页限定

  5. 基础查询

    查询多个字段:select 字段列表 from 表名;

    查询所有数据:select * from 表名;

    去除重复记录:select distinct 字段列表 from 表名;

    起别名:AS: AS也可以省略

    `例子:`
    ​
    `select name,age from stu;`
    ​
    `select * from stu;`
    ​
    `select distinct address from stu;`
    ​
    `select name,math as 数学成绩,english as 英文成绩 from stu;`
    `select name,math 数学成绩,english 英文成绩 from stu;`
    
  6. 条件查询

    语法:select 字段列表 from 表名 where 条件列表;

    符号:> 大于;< 小于;>= 大于等于;<= 小于等于;= 等于;<>或!= 不等于;

    between...and... 在某个范围之内(都包含);in(...) 多选一;

    like 占位符 模糊查询 _单个任意字符 %多个任意字符;is null 是null;is not null 不是null;

    and或&& 并且;or或|| 或者;not 或! 非;

    例子:
    ​
    select * from stu where age > 20;
    ​
    select * from stu where age >= 20 &&  age <= 30;
    select * from stu where age >= 20 and  age <= 30;
    ​
    select * from stu where age BETWEEN 20 and 30;
    ​
    select * from stu where hire_date BETWEEN '1998-09-01' and '1999-09-01';
    ​
    select * from stu where age != 18;
    select * from stu where age <> 18;
    ​
    select * from stu where age = 18 or age = 20 or age = 22;
    select * from stu where age in (18,20 ,22);
    ​
    select * from stu where english = null; -- 这个语句是不行的
    select * from stu where english is null;
    select * from stu where english is not null;
    ​
    模糊查询:
    查询姓'马'的学员信息:select * from stu where name like '马%';
    查询第二个字是'花'的学员信息:select * from stu where name like '_花%';
    查询名字中包含 '德' 的学员信息:select * from stu where name like '%德%';
    
  7. 排序查询

    语法:select 字段列表 from 表名 order by 排序字段名1 [排序方式1],排序字段名2 [排序方式2]...;

    ASC:升序排序(默认值) DESC:降序排序

    注意:若有多个排序条件,当前边的条件值一样时,才会根据第二条件进行排序

    例子:
    ​
    查询学生信息,按照年龄升序排列:select * from stu order by age ;
    查询学生信息,按照数学成绩降序排列:select * from stu order by math desc ;
    查询学生信息,按照数学成绩降序排列,如果数学成绩一样,再按照英语成绩升序排列:
        select * from stu order by math desc , english asc ;
    
  8. 聚合函数

    将一列数据作为一个整体,进行纵向计算

    语法:select 聚合函数名(列名) from 表;

    null值不参与所有聚合函数运算

    分类:

    count(列名) 统计数量(一般选用不为null的列)

    max(列名) 最大值

    min(列名) 最小值

    sum(列名) 求和

    avg(列名) 平均值

    例子:
    ​
    统计班级一共有多少个学生:select count(*) from stu;
    查询数学成绩的最高分:select max(math) from stu;
    查询数学成绩的最低分:select min(math) from stu;
    查询数学成绩的总分:select sum(math) from stu;
    查询数学成绩的平均分:select avg(math) from stu;
    
  9. 分组查询

    语法:select 字段列表 from 表名 [where 分组前条件限定] group by 分组字段名 [having 分组后条件过滤]

    分组之后,查询的字段为聚合函数和分组字段,查询其他字段无任何意义

    eg. select name, sex, avg(math) from stu group by sex; -- 这里查询name字段就没有任何意义

    例子:
    ​
    查询男同学和女同学各自的数学平均分,以及各自人数:
        select sex,avg(math),count(*) from stu group by sex;
    查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组
        select sex,avg(math),count(*) from stu where math>70 group by sex;
    查询男同学和女同学各自的数学平均分,以及各自人数,要求:分数低于70分的不参与分组,分组之后人数大于2select sex,avg(math),count(*) from stu where math>70 group by sex having count(*)>2;
    

    where和having的区别:

    执行时机不一样:where是分组前进行限定,不满足where条件,则不参与分组;having是分组后对结果过滤。

    可判断的条件不一样:where不能对聚合函数进行判断,having可以

  10. 分页查询

    语法:select 字段列表 from 表名 limit 起始索引,查询条目数

    起始索引从0开始

    例子:
    ​
    从0开始查询,查询3条数据: select * from stu limit 0,3;
    每页显示3条数据,查询第1页数据: select * from stu limit 0,3;
    每页显示3条数据,查询第2页数据: select * from stu limit 3,3;
    每页显示3条数据,查询第3页数据: select * from stu limit 6,3;
    ​
    起始索引 = (当前页码 - 1* 每页显示的条数
    
  11. SQL用途:定义数据模型;读写数据库数据

  12. SQL优点:标准化;高度非过程化(操作过程由系统自动完成,大大减轻了用户负担,有利于提高数据独立性);以同一种语法结构提供两种使用方式(终端直接输入或嵌入到高级语言);语言简洁、易学易用