数据库(3)

时间:2019-08-19
本文章向大家介绍数据库(3),主要包括数据库(3)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Python高级

子查询

即嵌套查询,将一个select查询结果当做一个单值,一个集合或一个临时表对待,参与另一个select查询

内置函数

分为2类:单行函数和聚合函数

单行函数是以一条记录为单位进行处理,而聚合函数是用来处理多条记录的(即分组, 和group by)

常用聚合函数:count,sum,max,min,avg等

distinct 去除重复列

having关键字,对分组的结果进行过滤处理,和group by子句一起使用,不能单独使用

exists关键字:判断一个集合是空还是非空

连接(Join)

连接操作就是在笛卡尔集的操作之上过滤

连接操作分为三类:

1.交叉连接:即无条件连接,即笛卡尔积

2.内连接:等值连接(使用=),不等值连接(>,<等),自然连接(特殊的等值连接,它在等值连接的基础上去除重复的字段)

3.外连接:左外连接(left join,left outer join),右外连接(right join,right outer join),全外连接(full join,full outer join,左外连接和右外连接的并集)

视图(View)

相当于一张虚表,针对不同用户的需求呈现不同的数据,其本质就是将一个select查询封装为一张虚表

视图和表一样,都可以进行CRUD操作,但不建议对视图进行DML(增删改查)操作,因为可能会破坏数据的完整性

索引(Index)

提升查询效率的数据库对象,建议基于主键或唯一键创建索引

索引也会占存储空间

优化:

select语句基于索引位置查找速度会快很多

创建索引基于唯一键或者主键创建

主键也可以用几个字段,而不单单只能有一个

扩展

当成集合查询

select st.sno, st.name, st.sex, st.phone from student st where sno not in

(select sno from score sc , course c where c.name='JAVA程序设计' and c.cno=sc.cno);

当成表查询:

select st.sno, st.name, st.sex, st.phone, t.name, t.score from student st, (select sno, name, score from course c, score sc where name='Python程序设计' and c.cno=sc.cno and score>75) t where st.sno=t.sno order by score desc;

查询出Python程序设计低于班级平均分的学生信息

select * from student st, course c, score sc where c.name='Python程序设计' and st.sno=sc.sno and c.cno=sc.cno and score<

(select avg(score) from course c, score sc where name='Python程序设计' and c.cno=sc.cno) order by score;

分别查询出男生和女生的平均分

select sex,floor(avg(score)) from student st, score sc, course c where c.name='Python程序设计' and st.sno=sc.sno and c.cno=sc.cno

group by sex

按什么字段进行分组 即group by后面接的什么 select后面就只能接什么 其余的接不了 ,即此处只能接sex ,不能接st.name 等其他信息

查看班上有多少人

select count(*) from student;

查看男女各多少人

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

having的使用:

select sno, avg(score) avg_score from score group by sno having avg_score >75;

exists的使用:

如果有大于90分的就打印成绩表,没有就不打印

select * from score where exists(select * from score where score > 90)

无条件交叉连接:

select * from student, course; 等效于

select * from student join course;

等值连接

select * from student st join score sc on st.sno=sc.sno

自然连接:

select * from student st natural join score sc;

左外连接:满足条件的留下来,此外左表中不满足条件的也会留下来

此方法可以统计此student表中一门都没考过的

select * from student st left join score sc on st.sno=sc.sno;

右外连接则相反

全外连接:

select * from student st full join score sc on st.sno=sc.sno;

关联,删除两个表共有的信息时,会同时删掉

create table test1(a int primary key);

create table test2(b int, foreign key(b) references test1(a) on delete cascade

insert into test1 values(1)

insert into test2 values(1)

delete from test1 where a =1;

创建视图

create view python_class_info (sno, sname, sex, phone, cno, cname, score) as select st.sno, st.name, st.sex, st.phone, c.cno,c.name from student st, course c, score sc,where c.name='Python程序设计' and st.sno=sc.sno and

c.cno=sc.cno

删除视图

drop view python_class_info

select * from python_class_info;

update python_class_info set score=96 where sno=1001

设置索引

create index score_i on score(sno,cno)

selcet的查询结果:

1.单值,

2.单值的集合,

3.临时表

数据库对象是持久化存在的,只要不删

多表查询就是数据库的连接操作

netstat -tlnp 查看端口号 t:tcp l:listen n:以数字的形式显示 p

重置密码mysql

vim /etc/my.cnf

skip-grant-----

systemctl restart mysqld

mysql

 

原文地址:https://www.cnblogs.com/yanruizhe/p/11379660.html