软件测试-数据库02

174 阅读5分钟

1、逻辑删除:并不是删除,而是做标记,标记是否被删除。

1、多加一个isdelete字段,1代表删除。0代表未删除

2、把所有的isdelte都改为0

3、当要删除的时候,就把对应的改为1

4、当要查询数据的时候,查询isdelete为0就可以

alter table student(表名) add isdelete int

update student set isdelete=1表示删除,但是可以恢复

select * from student isdelete=1查询已经本删除的人

2、简单查询(查询不影响表,就是不更改)

select * from 表名       *代表所有列

select name from 表名   查询name这一列



2.1、改字段的别名(as和.的作用是一样的)

select name as 姓名 from student



2.2、改表的别名,(同时查询两个表内的名字做区分)

select name as 姓名 from student as s,class as c



2.3、去重查询

例子:查询学生的性别有哪几种

select distinct sex from student 

去重多个字段

select distinct sex,class from student

这个一般是查询主键是否重复,因为主键不可能重复

select distinct * from student

3、条件查询-比较运算

select 字段1,字段2from 表名 where 条件;(字段过滤列,条件过滤列)

比较运算符:
等于= 
大于> 
小于< 
大于等于>= 
小于等于<= 
不等于!=或者<>

例子1:查询小乔的年龄

select age from student where name='小乔'

例子2:查询学生20以下的学生

select * from student where age<20

4、逻辑运算

且:and

或:or

非:not

例子:年龄小于20的女同学

select * from student where age<20 and sex='女'

例子:查询女同学或者1班的同学

select * from student where class='1班' or sex='女'

例子:非天津的学生

select * from student where not hometown='天津'

5、模糊查询

like

%表示任意字符

_下划线表示一个字符

例子:查询姓温的同学

select * from student where name like '温%'

例子2:查询姓温,名字为一个字的,三个字的人

select * from student where name like '温_ _'

例子2:查询名字以南结尾的

select * from student where name like '%南'

例子3:查询名字含有白的

select * from student where name like '%白%'

6、范围查询

6.1inor差不多,比or方便一点

例子:

select * from student where class='1班' or class='2班' or class='3班'

elect * from student where class in ('1班','2班','3班',)



6.2、between …and     (前面的数必须小于等于后面的数值)

例子:查询年龄在10到20的学生

select * from student where age>=10 or age<=20

select * from student where age between 10 and 20

7、空判断

空是null(相当于,考试没交试卷)   

产生原因:insert into student(age) values20),其他默认为null,或者至二级写null

空字符串(相当于,考试交试卷,但是试卷空白)

产生原因:insert into student(age) values'')

例子:没有填写年龄的学生

select * from student where age is null

select * from student where age=''        引号里什么都没有


例子:查询写了年龄的学生

select * from student where not age is null

8、排序

select * from 表名 order by1 asc|desc , 列2 asc|desc

升序:asc

降序:dsc

例子:年龄排序从小到大,但是学号大的在前面

select * from student order by age asc , studentid desc

例子:名字排序(需要将文字转换成gbk)

select * from student order by convert(name use gbk)

9、聚合函数(常用的5个)

9.1、count:统计

select count(*) from student

例子:查询学生总数

 select count(*) from student            统计整个表

 select count(name) from student         统计一列,不包含null

9.2、max和min最大值和最小值

例子:统计女生中最大的年龄

select max(age) as 最大年龄 from student where sex='女'

9.3、求和sum、求平均值avg

例子:求1班学生年龄总和

select sum(age) from student where class='1班'

select avg(age) from student where class='1班

10、分组:group by

Eg:只要见到每或者各个,这个字眼,都是用group by



select1,列2,聚合函数,,, from 表名 group by1,列2,,,

例子:查询各种性别的人数

select sex,count(*) from student group by sex

例子:查询各种年龄的分组

select age,count(*) from student group by age

例子:查询各个班级的平均年龄,最大年龄和最小年龄

select class,avg(age),max(age),min(age) from student group by class

例子:查询每个班的女和男的数量

select class,sex, count(*) from student group by class,sex

11、分组后过滤:having

例子:查询男生总人数(可以用where,也可以用having必须跟在group byselect sex,count(*) from student group by sex having sex='男'

例子:查询1班除外各个班级的平均年龄,最大年龄和最小年龄

select class,avg(age),max(age),min(age) from student group by class having class!='1班'

select class,avg(age),max(age),min(age) from student where class!='1班' group by class

12、分页(数据库中叫做获取部分行)

12.1select * from student limit start ,count            start从0 开始

例子:按年龄排序 ,获取4-7行的数据

select * from student order by age limit 3 ,4

 12.2、分页

例子:要求每页显示3条数据

获取总行数:select count(*) from student

获取页数:12/3=4

第一页:select * from student order by age limit 0 ,3 起始数0=3*(页数-1)这部分后续会有公式,属于开发的部分

第二页:select * from student order by age limit 3 ,3 起始数3=3*(页数-1eg:select * from student order by age limit 7

13、多表查询——连接查询——等值连接

13.1、方式一:select * from1,表2 where1.列=表2.列     笛卡尔积容易产生临时表,所以不用方式一

创建三个表:学生表(学号)、课程表(课程号)、成绩表(课程号、学号)

例子:查询学生的信息及学生的成绩

select * from student ,scores where student.studentnumber=scores.studentnumber

如果用了别名,后续过滤也用别名

select * from student as stu ,scores as sc where stu.studentnumber=sc.studentnumber

14、内连接:主要用这种,不会产生笛卡尔积和临时表

14.1、方式二:(又称内连接)

select * from1

inner join on2.列=表1.列

例子:还是上面的例子

select * from student 

inner join score on student.studentnumber=scores.studentnumber





例子:统计二班男女各多少人

select class sex,count(*) from student where class='2班' group by sex

例子:统计每个班级每种性别的人数没并且按照班级升序排序

select class,sex,conut(*) from student group by class,sex order by class asc

例子:查询年龄最小的学生的所有信息

select * from student order by age limit 0,1        (遇到相同年龄的,需要用到子查询,后面学习)

例子:查询课程信息及课程的成绩

select * from courses,scores where course.courseNo=scores.courseNo



select * from courses

inner join score on course.courseNo=scores.courseNo