软件测试---数据库
软件测试---数据库
之前也有考软件设计师证的时候也有学过数据库,但是为了应试完全没有系统的学过,学的不是很好,前几天重新看完Linux后开始把数据库的内容重新跟着B站的视频和一些其他网上的内容自己重新把大体框架总结了一下。我发现我好喜欢写成框架图哈哈哈,xmind还真是挺好用的,我要抛弃markdown了。
另外,接下来的一周打算看一下视频里Python和自动化测试的部分,研究生阶段还是经常用Python的,所以基本基础都知道大概过下然后学习一下自动测试用到的库,学完之后继续来做简单的分享,也请监督我呀!嘻嘻!
数据库的基本概念
数据库是用来存储数据
关系型数据库
RDBMS 核心:用表存储数据
- 表头有多个字段名,代表每一列数据的含义
- 表中存的是一行一行的数据,每一行数据有多个字段值
- 表包含多行数据,一个数据库中可以存多个表
mysql基本的增删改查语句,存储过程
1.创建表
- create table 表名(
字段名 类型 约束,
字段名 类型 约束
…
)
2.删除表
- 格式一:drop table 表名
格式二:drop table if exists 表名
3.简单查询
- select * from 表名
4.添加数据
- 格式一:所有字段设置值,值的顺序与表中字段的顺序对应
insert into 表名 values(…) - 格式二:部分字段设置值,值的顺序与给出的字段顺序对应
insert into 表名(字段1,…) values(值1,…)
5.修改
- 格式:update 表名 set 列1=值1,列2=值2… where 条件
例:修改id为5的学生数据,姓名改为 狄仁杰,年龄改为 20
update students set name=‘狄仁杰’,age=20 where id=5
6.删除
- 格式:delete from 表名 where 条件
- 逻辑删除:对于重要的数据,不能轻易执行delete语句进行删除,一旦删除,数据无法恢复,这时可以进行逻辑删除。 is_delete=0
mysql复杂查询,多表查询
1.简单查询
-
查询所有字段
- select * from 表名
-
查询指定字段
- select 列1,列2,… from 表名
-
消除重复行
- select distinct 列1,… from 表名;
2.条件
-
select 字段1,字段2… from 表名 where 条件;
-
比较运算符
-
逻辑运算符
-
模糊查询
- like
%表示任意多个任意字符
_表示一个任意字符
- like
-
范围查询
-
in
- 例1:查询家乡是北京或上海或广东的学生
select * from students where hometown in(‘北京’,‘上海’,‘广东’)
- 例1:查询家乡是北京或上海或广东的学生
-
between … and …
- 例2:查询年龄为18至20的学生
select * from students where age between 18 and 20
- 例2:查询年龄为18至20的学生
-
-
空判断
- null
-
3.排序
- select * from 表名
order by 列1 asc|desc,列2 asc|desc,… - asc从小到大排列,即升序
desc从大到小排序,即降序
4.聚合函数
- 查询学生总数
select count(*) from students; - max(列)、min(列)
- sum(列)
- avg(列)
5.分组
-
select 列1,列2,聚合… from 表名 group by 列1,列2…
-
分组后的数据筛选
- select 列1,列2,聚合… from 表名
group by 列1,列2,列3…
having 列1,…聚合…
- select 列1,列2,聚合… from 表名
-
对比where与having
- 1、where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
2、having是对group by的结果进行筛选
- 1、where是对from后面指定的表进行数据筛选,属于对原始数据的筛选
6.分页
- select * from 表名
limit start,count - 每页显示m条数据,求:显示第n页的数据
select * from students limit (n-1)*m,m
7.连接查询
-
等值连接
- select * from 表1,表2 where 表1.列=表2.列
- 内连接
select * from 表1
inner join 表2 on 表1.列=表2.列
-
左连接
- select * from 表1
left join 表2 on 表1.列=表2.列
- select * from 表1
-
右连接
- select * from 表1
right join 表2 on 表1.列=表2.列
- select * from 表1
8.自关联
9.子查询
- in 范围
格式: 主查询 where 条件 in (列子查询) - any | some 任意一个
格式: 主查询 where 列 = any (列子查询)
在条件查询的结果中匹配任意一个即可,等价于 in - all
格式: 主查询 where 列 = all(列子查询) : 等于里面所有
格式: 主查询 where 列 <>all(列子查询) : 不等一其中所有
mysql索引及事务相关概念
1.事务
-
提交
- step1:连接
命令行1:查询学生信息
select * from students; - step2:增加数据
命令行2:开启事务,插入数据
begin;
insert into students(studentNo,name) values (‘013’,‘我是新来的’); - step3:查询
命令行1:查询数据,发现并没有新增的数据
select * from students; - step4:提交
commit; - step5:查询
- step1:连接
-
回滚
- step1:连接
select * from students; - step2:增加数据
begin;
insert into students(studentNo,name) values (‘014’,‘又来一个’); - step3:查询
- step4:回滚
rollback; - step5:查询
- step1:连接
2.索引
- show index from 表名;