数据库的应用-第三天

数据库服务基础应用操作

数据库语句应用实践

1)DCL和数据库安全有关的操作

sh 复制代码
grant/revoke						#	和授权有关操作
create use/alter user/drop user		#	和用户相关操作

commit		可以确保DML语句操作后,产生的数据信息合理存储到磁盘中
rollback	可以实现DML语句操作后,将改动调整的数据做回退操作
DML- insert update delete	可以保证存储语句的执行成功

2)DQL可以实现查询数据库数据信息

查询情况1

sh 复制代码
DQL	可以实现查询数据库数据信息
show select 
查询情况1:数据库中变量信息查询(变量--配置项信息/监控信息)######################################################
show variables like 'XXX';		查看配置项变量信息
show global status like 'xxx';	查看监控项变量信息

select @@max_connections;   -- 查看具体某个变量信息

扩展说明:数据库配置项变量如何改动
两种方式可以改动配置项
方式一:永久改动
max_connections		设置数据库最大并发连接数
	在vim /etc/my.cnf中更改
	[mysqld]
	max_connections		永久修改配置需要重启数据库服务
方式二:临时改动
set global max_connextions=3;		配置会影响全部用户功能/当会话断开重新连接配置不会失效(全局改动)
set session sql_log_bin='off';		配置只会影响当前用户/当会话断开重新连接配置会失效(会话改动)

查询情况2

bash 复制代码
查询情况2:可以查看具体数据或对象信息########################################################################
show   查看表或库信息或用户或权限或索引...
select 查看具体数据信息

单表数据信息查看
-- 创建单表查看测试数据:
https://dev.mysql.com/doc/index-other.html

加载测试数据-导入数据表到数据库中进行操作
mysql -uroot -p66666 -S /tmp/mysql.sock <world.sql

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| world              |

mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int      | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int      | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+
附加:如何导入数据表到mysql中
sh 复制代码
1.把world.sql移动到数据库安装目录下
[root@mysql local]#ll			#	当前在/usr/local
total 603964
............
-rw-r--r-- 1 root root    398629 Mar  1 07:05 world.sql⭐
............

2.使用命令导入world.sql数据表
[root@mysql local]#mysql -uroot -p666666 -S /tmp/mysql.sock <world.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
#	用管理员身份导入world.sql

3.进入数据库查看
[root@mysql local]#mysql -p666666
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 8.0.36 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;		#	展示所有数据库,发现world数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| performance_schema |
| sys                |
| world              |
+--------------------+
6 rows in set (0.00 sec)

mysql> use world;			#	使用world数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;			#	查看所有数据表
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)


world.sql导入成功



可以进行操作来查看具体内容,比如数据表结构、数据表内容
mysql> desc course;			#	查看数据表结构
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cno   | int         | NO   | PRI | NULL    |       |
| cname | varchar(20) | NO   |     | NULL    |       |
| tno   | int         | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> select * from course;		#	查看数据表内容
+------+--------+-----+
| cno  | cname  | tno |
+------+--------+-----+
| 1001 | linux  | 101 |
| 1002 | python | 102 |
| 1003 | mysql  | 103 |
| 1004 | go     | 105 |
+------+--------+-----+
4 rows in set (0.00 sec)

单表数据查看练习(基础查看)

1.单表所有数据查看
sh 复制代码
1.单表所有数据查看
	select * from 库名.表名;
	select 列名01,列表02.. from 库名.表名;
备注:以上表中全部数据查看操作,尽量少用
2.单表数据过滤查看 (where)⭐
bash 复制代码
2.单表数据过滤查看 (where)
1)等值条件查看数据信息####################################################################################
查看北京城市人口情况;
mysql> show databases;		#	查看所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| performance_schema |
| school             |
| sys                |
| world              |
+--------------------+
7 rows in set (0.02 sec)

mysql> use world;			#	使用我们需要的数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;			#	展示所有数据表
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)

mysql> desc city;			#	查看city数据表的结构
+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-------------+----------+------+-----+---------+----------------+
| ID          | int      | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population  | int      | NO   |     | 0       |                |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> select * from city;		#	查看city数据表的内容
+------+------------------------------------+-------------+------------------------+------------+
| ID   | Name                               | CountryCode | District               | Population |
+------+------------------------------------+-------------+------------------------+------------+
|    1 | Kabul                              | AFG         | Kabol                  |    1780000 |
|    2 | Qandahar                           | AFG         | Qandahar               |     237500 |
|    3 | Herat                              | AFG         | Herat                  |     186800 |
|    4 | Mazar-e-Sharif                     | AFG         | Balkh                  |     127800 |
|    5 | Amsterdam                          | NLD         | Noord-Holland          |     731200 |
|    6 | Rotterdam                          | NLD         | Zuid-Holland           |     593321 |
|    7 | Haag                               | NLD         | Zuid-Holland           |     440900 |
|    8 | Utrecht                            | NLD         | Utrecht                |     234323 |
............


#可以得出name是城市名,countrycode是国家,district是省份,population是人口


mysql> select name,population from city where name='peking';
+--------+------------+
| name   | population |
+--------+------------+
| Peking |    7472000 |
+--------+------------+
1 row in set (0.01 sec)


2)范围条件查看数据信息####################################################################################
①查看人口数量大于1000w城市和国家信息
mysql> select name,countrycode,population from city where population>10000000;
+-----------------+-------------+------------+
| name            | countrycode | population |
+-----------------+-------------+------------+
| Mumbai (Bombay) | IND         |   10500000 |
+-----------------+-------------+------------+
#		数值做范围查看 > < >= <= <>/!=			⭐⭐⭐

②查看北京上海深圳重庆等一线城市人口数量
mysql> select name,population from city where  name in ('peking','shanghai','shenzhen','chongqing');
+-----------+------------+
| name      | population |
+-----------+------------+
| Shanghai  |    9696300 |
| Peking    |    7472000 |
| Chongqing |    6351600 |
| Shenzhen  |     950500 |
+-----------+------------+
#		字符信息做范围查看 in ()  / not in ()	⭐⭐⭐


3) 多个条件列的逻辑关联查询################################################################################
多个条件逻辑关联方式
and --- 将多个条件做交集处理(将过滤条件做逐步处理 得到需要的查询结果)   
or  --- 将多个条件做并集处理(将过滤条件做分别处理 交结果进行合并显示)
①查看中国和美国所有城市以及人口数量情况
mysql> select name,population from city where countrycode='chn' or countrycode='usa'; 

②查看城市人口数量大于100w并小于300w城市信息
mysql> select name,population from city where population>1000000 and population<3000000;

#	或者另一种表达方式-->  and=between and

mysql> select name,population from city where population between 1000000 and 3000000;


4) 条件列做模糊过滤查询####################################################################################
查询广州城市人口信息
mysql> select * from city where countrycode='chn' and district like '%guang%' and name like '%guang%';
+------+--------------------+-------------+-----------+------------+
| ID   | Name               | CountryCode | District  | Population |
+------+--------------------+-------------+-----------+------------+
| 1897 | Kanton [Guangzhou] | CHN         | Guangdong |    4256300 |
+------+--------------------+-------------+-----------+------------+


5) 取重查询数据信息#######################################################################################
获取中国有多少个省份
select distinct District from city where countrycode='chn';
select count(district) from city where countrycode='chn';
#	统计省份一共有多少

6) 查看空值列信息#########################################################################################
mysql> select * from city where population is null;
	
查看非空列信息
mysql> select * from city where population is not null;

单表数据查看练习(进阶查看)

1.分组聚合查看数据信息
①聚合查看数据
sh 复制代码
①聚合查看数据		#	查询数据需要做统计分析  实现聚合查看数据需要应用聚合函数

1.查看中国人口总数
select countrycode,sum(population) from city where countrycode='chn';
+-------------+-----------------+
| countrycode | sum(population) |
+-------------+-----------------+
| CHN         |       175953614 |
+-------------+-----------------+

2.查看中国城市数量  
select countrycode,count(name) from city where countrycode='chn';
+-------------+-------------+
| countrycode | count(name) |
+-------------+-------------+
| CHN         |         364 |
+-------------+-------------+

3.查看中国人口最多的城市信息/查看中国人口最少的城市信息
mysql> select countrycode,max(population) from city where countrycode='chn';
+-------------+-----------------+
| countrycode | max(population) |
+-------------+-----------------+
| CHN         |         9696300 |
+-------------+-----------------+
1 row in set (0.05 sec)

mysql> select countrycode,min(population) from city where countrycode='chn';
+-------------+-----------------+
| countrycode | min(population) |
+-------------+-----------------+
| CHN         |           89288 |
+-------------+-----------------+


avg()  -- 取某个数值列的平均数
max()  -- 取某个数值列的最大值
min()  -- 取某个数值列的最小值
-- 直接查看列的最大值或最小值




额外:显示查看中国人口最多的城市信息,显示人口信息与城市名字
SELECT name,countrycode,population 
FROM city 
WHERE countrycode = 'chn' 
ORDER BY population DESC 
LIMIT 1;

+----------+-------------+------------+
| name     | countrycode | population |
+----------+-------------+------------+
| Shanghai | CHN         |    9696300 |
+----------+-------------+------------+


执行过程(精讲版):
我们可以把它拆解成五个简单的步骤来理解:
1. FROM city
第一步:去哪个表里找?
告诉数据库:"请打开名为 city 的那张表(里面存着所有城市的数据)。"

2. WHERE countrycode = 'chn'
第二步:筛选哪些行?
告诉数据库:"在这张表里,我只关心 countrycode(国家代码) 等于 'chn' (中国)的那些行。其他的国家(如 'usa', 'jpn')全部忽略。"
此时,手里剩下的全是中国的城市数据。

3. ORDER BY population DESC
第三步:怎么排队?
告诉数据库:"把刚才筛选出来的中国城市,按照 population(人口) 这一列进行排序。"
DESC 是 Descend(下降)的缩写,意思是从大到小排。				
此时,人口最多的城市排在了第 1 位,人口最少的排在了最后。
#	升序用ORDER BY population,降序用ORDER BY population DESC 

4. LIMIT 1
第四步:只要几个?
告诉数据库:"排好队后,我只要第 1 个(也就是排在最前面、人口最多的那一个),后面的都不要了。"
如果没有这句话,数据库会把所有中国城市按人口从大到小全部列出来。

5. SELECT name, countrycode, population
第五步:给我看什么?
告诉数据库:"对于最后留下的这唯一一行数据,我只需要看这三列信息:name(城市名)、countrycode(国家代码) 和 population(人口数)。其他的信息(比如所属省份 District、城市 ID 等)不用显示给我。"
②分组查看数据⭐
sh 复制代码
②分组查看数据		#	表示将某个列相同信息分为一组,然后将分组后数据分别做分析处理

1.查看全球范围内,每个国家的人口总数
select countrycode,sum(population) from city group by countrycode;

2.查看中国境内,每个省份的人口总数
select district,sum(population) from city where countrycode='CHN' group by district;




知识扩展说明:分组聚合查看数据常见问题

3.查看中国境内,每个省份的城市数量,以及显示省份的城市信息
select district,count(name),name from city where countrycode='CHN' group by district; 
ERROR 1140 (42000): 
In aggregated query without GROUP BY, 
expression #2 of SELECT list contains nonaggregated column 'world.city.Name'; 
this is incompatible with sql_mode=only_full_group_by

在聚合查看数据时,需要使用group by做分组操作
在分组聚合处理输出信息时,某些非聚合处理列,无法正常显示内容
sql_mode=only_full_group_by  无法实现数据表中一行信息对多行信息关联显示

分组聚合查询原理:
1)会先根据分组列信息,做排序处理
2)会将分组后的相同数据列信息合并成一行
3)会将需要分析统计列做对应聚合处理

解决以上分组聚合输出问题,可以将多行信息转换成一行
select district,count(name),group_concat(name) from city where countrycode='CHN' group by district;

命令的执行过程:
1. GROUP BY district ------ 先分类
动作:把全中国的城市,按照 district(省份/地区)分成不同的堆。
结果:比如"广东"一堆,"江苏"一堆,"北京"一堆......
2. COUNT(name) ------ 数个数
动作:在每一堆里,数一数有多少个城市名字。
结果:得到该地区的城市数量。
例如:广东 -> 50个,江苏 -> 40个。
3. GROUP_CONCAT(name) ------ 连名字
动作:把每一堆里的所有城市名字,用逗号 , 连接成一个长长的字符串。
结果:得到该地区的城市名单列表。
例如:广东 -> "广州,深圳,珠海,佛山..."


#	分行显示
select district,count(name),group_concat(name) from city where countrycode='CHN' group by district\G;
2.分组聚合过滤查看数据信息 -- having⭐
sh 复制代码
2)分组聚合过滤查看数据信息 -- having
查看全球范围内,每个国家的人口总数,将人口总数大于5000w国家信息显示出来
mysql> select countrycode,sum(population)
    -> from city
    -> group by countrycode
    -> having sum(population)>50000000
    -> ;
+-------------+-----------------+
| countrycode | sum(population) |
+-------------+-----------------+
| BRA         |        85876862 |
| CHN         |       175953614 |
| IND         |       123298526 |
| JPN         |        77965107 |
| MEX         |        59752521 |
| RUS         |        69150700 |
| USA         |        78625774 |
+-------------+-----------------+



having -- 会将聚合列作为条件过滤		(统计分析的数据做过滤)  
where  -- 会将现有数据列作为条件过滤	   (表中有的数据做过滤)
3.数据信息排序查询
sh 复制代码
3)数据信息排序查询  
查看全球范围内,每个国家的人口总数,将人口总数大于5000w国家信息显示出来,并排序显示输出
mysql> select countrycode,sum(population)
    -> from city
    -> group by countrycode
    -> having sum(population)>50000000
    -> order by sum(population)
    -> ;
+-------------+-----------------+
| countrycode | sum(population) |
+-------------+-----------------+
| MEX         |        59752521 |
| RUS         |        69150700 |
| JPN         |        77965107 |
| USA         |        78625774 |
| BRA         |        85876862 |
| IND         |       123298526 |
| CHN         |       175953614 |
+-------------+-----------------+

-- order by默认排序为升序排序⭐

查看全球范围内,每个国家的人口总数,将人口总数大于5000w国家信息显示出来,并按降序显示输出
mysql> select countrycode,sum(population)
    -> from city
    -> group by countrycode
    -> having sum(population)>50000000
    -> order by sum(population) DESC
    -> ;
+-------------+-----------------+
| countrycode | sum(population) |
+-------------+-----------------+
| CHN         |       175953614 |
| IND         |       123298526 |
| BRA         |        85876862 |
| USA         |        78625774 |
| JPN         |        77965107 |
| RUS         |        69150700 |
| MEX         |        59752521 |
+-------------+-----------------+
4.截取部分输出信息显示
sh 复制代码
4) 截取部分输出信息显示
显示全球人口总数大于5000w的前3名
mysql> select countrycode,sum(population)
    -> from city
    -> group by countrycode
    -> having sum(population)>50000000
    -> order by sum(population) DESC
    -> limit 3
    -> ;
+-------------+-----------------+
| countrycode | sum(population) |
+-------------+-----------------+
| CHN         |       175953614 |
| IND         |       123298526 |
| BRA         |        85876862 |
+-------------+-----------------+


单表查询数据常用语句:
select 输出列信息 from 库.表 where+group by+having+order by[desc]+limit;

小知识点总结❗️

sh 复制代码
1.order by排序的升序和降序

升序用ORDER BY population
降序用ORDER BY population DESC 

即order by默认就是升序,降序只需要加一个DESC
####################################################################


2.sum()和count()的区别

简单一句话总结:
COUNT() 是数个数(有多少行?有多少个非空值?)。
SUM() 是加总值(这些数字加起来是多少?)。

问 "多少个?" (How many?) -> 用 COUNT。
问 "总共多少?" (How much total?) -> 用 SUM。
####################################################################


3.where 和 having

having -- 会将聚合列作为条件过滤		(统计分析的数据做过滤)  
where  -- 会将现有数据列作为条件过滤	   (表中有的数据做过滤)

使用where的时候要后group by分组,使用having的时候要先group by分组❗️
where不能使用聚合函数,having可以使用聚合函数,如HAVING COUNT(*) > 5的表达方式
####################################################################


4.SQL的逻辑执行顺序

①FROM (找表)
②WHERE (🔴 先过滤原始数据行) -> 此时还没有分组,没有聚合值
③GROUP BY (🟢 再分组) -> 此时产生了"组"
④聚合函数计算 (COUNT, SUM, AVG...) -> 此时算出了每组的统计值
⑤HAVING (🔵 最后过滤分组后的结果) -> 此时才能对统计值进行判断
⑥SELECT (显示结果)
⑦ORDER BY (排序)
⑧LIMIT (取多少行)

多表数据信息查看(连表信息查看)

sh 复制代码
多表数据信息查看(连表信息查看)
加载多表查询测试数据
mysql -uroot -p123456 -S /tmp/mysql.sock <school.sql

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| school 

mysql> use school;

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| course           |
| sc               |
| student          |
| teacher          |
+------------------+

相同列可以进行关联
sc和student两个表格有相同的列,可以进行关联
再看一下表的信息teacher和student,没有相同的列,所以不能直接关联在一起,可以间接的关联在一起
数据表连表操作
sh 复制代码
数据库连表操作
如果想实现连表操作,必须将有相同列的表进行连接,没有相同列的表不能做连表操作
方法一:内连接
mysql> select * from teacher,course where teacher.tno=course.tno;
+-----+--------+------+--------+-----+
| tno | tname  | cno  | cname  | tno |
+-----+--------+------+--------+-----+
| 101 | oldboy | 1001 | linux  | 101 |
| 102 | xiaoQ  | 1002 | python | 102 |
| 103 | xiaoA  | 1003 | mysql  | 103 |
+-----+--------+------+--------+-----+

mysql> select * from teacher join course on teacher.tno=course.tno;
+-----+--------+------+--------+-----+
| tno | tname  | cno  | cname  | tno |
+-----+--------+------+--------+-----+
| 101 | oldboy | 1001 | linux  | 101 |
| 102 | xiaoQ  | 1002 | python | 102 |
| 103 | xiaoA  | 1003 | mysql  | 103 |
+-----+--------+------+--------+-----+


扩展:内连接语句写法
select * from 表01,表02,表03,表04 where 表01.列=表02.列 and 表02.列=表03.列 and 表03.列=表04.列;


select * from 表01 
join 表02 
on 表01.列=表02.列
join 表03
on 表02.列=表03.列
join 表04
on 表03.列=表04.列;


方法二:外连接
select * from teacher left join course on teacher.tno=course.tno;
-- 左外连接,会把左边的表作为主表,右边的表为子表
   主表数据信息在连接时都会加载显示,子表中只会加载显示和主表有关联信息,没有关联的信息用null表示
   
select * from teacher right join course on teacher.tno=course.tno;
-- 右外连接,会把右表的表作为主表,左边的表作为子表
   主表数据信息在连接时都会加载显示,子表中只会加载显示和主表有关联信息,没有关联的信息用null表示
连表查询数据解题步骤
sh 复制代码
连表查询数据有解题步骤:
1)确认库中表的关联关系(画图显示表中关联关系)
   teacher.tno=course.tno  course.cno=sc.cno sc.sno=student.sno  
2) 根据题目需求,确认需要的数据表信息
3)将多个数据表信息进行连接整合为一张大表
4) 根据整合后的单表做数据查询调取
练习题
sh 复制代码
练习题:
01 统计zhang3,学习了几门课?
   需要加载数据表:   student course
   将数据表进行连接: student.sno=sc.sno    sc.cno=course.cno     student+sc+course
   select * from student join sc
   on student.sno=sc.sno
   join course
   on sc.cno=course.cno;
   查询调取数据信息:
   select student.sname,count(course.cname) from student join sc
   on student.sno=sc.sno
   join course
   on sc.cno=course.cno
   where student.sname='zhang3';

   select sname,count(cname) from tea_cou_sc_stu  where sname='zhang3';

02 查询zhang3,学习的课程名称有哪些?
   select student.sname,course.cname from student join sc
   on student.sno=sc.sno
   join course
   on sc.cno=course.cno
   where student.sname='zhang3';

   查询zhang3,学习课程信息,以及数量信息;
   select student.sname,count(course.cname),group_concat(course.cname) from student join sc
   on student.sno=sc.sno
   join course
   on sc.cno=course.cno
   where student.sname='zhang3';
+--------+---------------------+----------------------------+
| sname  | count(course.cname) | group_concat(course.cname) |
+--------+---------------------+----------------------------+
| zhang3 |                   2 | linux,python               |
+--------+---------------------+----------------------------+

03 查询xiaoA老师教的学生名?
需要加载表: teacher course sc student
select * from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno;
单表查询数据:
select teacher.tname,student.sname from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno
where teacher.tname='xiaoA';

如果有老师重名情况如何统计每个老师授课学生情况
select teacher.tno,teacher.tname,count(student.sname) from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno
where teacher.tname='xiaoA'
group by teacher.tno;

04 查询xiaoA老师教课程的平均分数?
需要加载表: teacher course sc
select * from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno;
单表查询数据:
select teacher.tname,avg(sc.score) from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
where teacher.tname='xiaoA';

如果有老师重名情况 一个老师可能教授多门课程
select teacher.tno,teacher.tname,avg(sc.score) from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
where teacher.tname='xiaoA'
group by teacher.tno,course.cname;


05 每位老师所教课程的平均分,并按平均分排序?
需要加载表: teacher course sc
select * from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno;
单表查询数据:
select teacher.tno,course.cname,avg(sc.score) from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
group by teacher.tno,course.cname
order by avg(sc.score);

06 查询xiaoA老师教的不及格的学生姓名?
需要加载表: teacher course sc stdent
select * from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno;
单表查询数据:
select teacher.tname,student.sname,sc.score,course.cname from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno
where teacher.tname='xiaoA' and sc.score<60;

07 查询所有老师所教学生不及格的信息?
select teacher.tname,student.sname,sc.score,course.cname from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno
where sc.score<60;
select 查询的扩展操作
sh 复制代码
扩展01:别名查询数据信息
表别名查询数据:
select a.tname,d.sname,c.score,b.cname from teacher as a 
join course b
on a.tno=b.tno
join sc c
on b.cno=c.cno
join student d
on c.sno=d.sno
where c.score<60;
列别名查询数据:
select a.tname as '讲师名',d.sname as '学生名',c.score,b.cname from teacher as a 
join course b
on a.tno=b.tno
join sc c
on b.cno=c.cno
join student d
on c.sno=d.sno
where c.score<60;

扩展02:多表拼接可以实现纵向拼接
mysql> select * from teacher union all select * from teacher02;
+-----+--------+
| tno | tname  |
+-----+--------+
| 101 | oldboy |
| 102 | xiaoQ  |
| 103 | xiaoA  |
| 104 | xiaoB  |
| 101 | oldboy |
| 105 | xiaoE  |
+-----+--------+
-- 当使用union all和union时,都可以实现纵向拼接两张表的数据
   union all -- 不会将两张表相同行数据做取重输出
   union     -- 会将两张表相同行数据做取重输出

扩展03:数据表视图操作/视图管理应用
什么是视图:
视图等价于将复杂查询表的语句做别名设置;
t1 视图 = select xxx 


如果有老师重名情况 一个老师可能教授多门课程
select teacher.tno,teacher.tname,avg(sc.score) from teacher 
join course
on teacher.tno=course.tno
join sc
on course.cno=sc.cno
where teacher.tname='xiaoA'
group by teacher.tno,course.cname;


select * from teacher 
join course
on teacher.tno=course.tno 
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno;

create view tea_cou_sc_stu as
select teacher.tno,teacher.tname,course.cno,course.cname,sc.sno,sc.score,student.sname,student.sage,student.ssex from teacher 
join course
on teacher.tno=course.tno 
join sc
on course.cno=sc.cno
join student
on sc.sno=student.sno;

select sname,count(cname) from tea_cou_sc_stu  where sname='zhang3';  
-- 利用视图可以简化连表语句的应用

总结

sh 复制代码
数据库语句应用
1)DCL语句应用  commit/rollback 
2)DQL语句应用
   查看数据库变量信息(配置变量/监控变量) show variables like / show global status like/ select @@变量名
   数据变量信息如何调整  永久修改 -- vim /etc/my.cnf 临时修改 -- set global 变量名=值;/set session 变量名=值
   查看数据库对象信息
   查看数据库数据表信息  show 
   查看数据表数据信息    select  
                       单表查看数据 -- select+from+where+group by+having+order by+limit
                       多表查看数据 -- 内连接查询/外连接查询
                       内连接查询:select * from 表01 join 表02 on 表01.列=表02.列
                       外连接查询:select * from 表01 left/right join 表02 on 表01.列=表02.列  
                       								左外连接/右外连接
                       连表查询扩展说明:
                       -- 连表的别名设置  表别名/列别名
                       -- 连表的纵向拼接  union all/union
                       -- 连表后视图应用  create view 视图名称 as 连表语句

作业

sh 复制代码
课程作业:
01 查询平均成绩大于60分的同学的学号和平均成绩
02 查询所有同学的学号,姓名。选课数,总成绩
03 查询各科成绩最高和最低的分,以如下形式显示:课程ID,最高分,最低分
04 查询每门课程被选修的学生数
05 查询出只选修了一门课程的全部学生的学号和姓名
06 查询平均成绩大于85的所有学生的学号、姓名和平均成绩