DBA SQL基础应用

1、select

1.1 作用
获取mysql中的数据行
1.2 单独使用select
1) select @@xxxx; 获取参数信息
      select @@prot;
      down variables like '%innodb%'
2) select 函数()
      select database();
      select now();
      select version();
1.3 SQL92标准的使用语法
1) select语法执行顺序(单表)
      select开始--->from子句--->where子句--->group by子句(select之后的执行条件)--->having子句--->order by--->limit
2) select 语句应用
      1)from
         例子:查询city表中的所有数据
               use world;
               select * from city;  --->适合表数据行较少的,生产中使用较少的
               select * from world city;
         例子:查询name和population所有值
               select name,population from city;
               select name,population from world city;
         例子:单表查询练习环境,world数据库下的介绍
               show tables from world;
               city(城市):
               --------------------
               desc city;
               id:自增的无关列,数据行的需要
               name:城市名字
               countrycode:城市所在的国家代号,CHN,USA,JPN...
               district:城市的所在区域,中国是省的意思,美国是洲的意思
               population:城市的人口数量
               说明:此表示历史数据。
               country(国家)
               countrylanguage(国家语言)
               --------------------
               刚入职的DBA熟悉业务:
               DBA刚入职时,DBA的任务
               1、搞清楚架构
                  通过公司架构图,搞清楚数据库的物理架构
                  1-2天
                  逻辑结构:
                  1)生产库信息(容易达到)
                  2)库下表信息(非常复杂)
                     a、开发和业务人员打好关系
                     b、搞到ER图(PD)
                     c、啥都没有咋整?
                        1)、找到建表语句,如果有注释,读懂注释就可以了。如果没有注释,目前只能根据列名翻译
                        2)、找到表中部分数据,分析数据特点,达到了解列功能的一个目的
      2)where
               where 配合 等值查询
               查询中国的城市信息
               select * from world.city where countrycode='CHN';
               查询美国的城市信息
               select * from world.city where countrycode='USA';
               where 配合 不等值 (> < >= <= <>)
               查询世界上人口小于100的城市
               select * from world.city where population<100;
               查询世界上人口大于10000000城市
               select * from world.city where Population>10000000;
               --------------------------
               where 配合 模糊(like)
               查询国家代号是C开头的城市
               select * from world.city where countrycode like 'C%';
               注意:like语句在MySQL中,不要出现%在前面的清空。因此效率很低,不走索引
               --------------------------
               where 配合 逻辑连接符(AND OR)
               查询城市人口在10000和20000之间的
               select * from world.city where population > 10000 and population < 20000;
               查询中国或美国的的城市信息
               select * from world.city where countrycode='CHN' or countrycode='USA'
               用union all来实现
               select * from world.city where countrycode='CHN' union all select * from world.city where countrycode='USA';
      3)group by
               group by 配合聚合函数应用
                 常用的聚合函数:
                   avg () 平均数
                   count () 统计个数
                   sum () 统计总数
                   max () 最大数
                   min () 最小数
                   group_concat () 统计列表
               -------------------------
               统计每个国家的总人口数
               select countrycode,sum(population) from world.city group by countrycode;
               统计每个国家的城市个数
               select countrycode,count(id) from world.city group by countryCode;
               统计每个国家的省的个数
               select countrycode,group_concat(district) from world.city group by countrycode;
               统计中国每个省的城市名列表
               select district,group_concat(name) from world.city where countrycode='CHN' group by district;
               统计中国每个省的总人数
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district;
      4)select
      5)having  和Linux中的管道一样。
               统计中国,每个省的总人口大于10000000万的人数
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district having sum(population) >10000000;
      6)order by 后面加desc是从大到小,不加则从小到大。
               统计中国,每个省的总人数从小到大排序
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district order by sum(population);
               统计中国,每个省的总人数从大到小排序
               SELECT district, sum(population) FROM world.city WHERE countrycode = 'CHN' GROUP BY district order by sum(population) desc;
               统计中国,每个省的总人数从小到大排序
               SELECT * FROM world.city WHERE countrycode = 'CHN' order by population desc;
      7)limit m,n 跳过m行,显示n行
         limit m offset n  跳过m行,显示n行
1.4 多表连接查询
1) 介绍4张测试表的关系
2) 什么时候用?
      需要查询的数据是来自于多张表时。
3) 怎么去多表连接查询
      传统的连接:基于where条件
          1、找表之间的关系列
          2、排列查询条件
          需求:人口数量小于100人的城市,所在国家的国土面积(城市名,国家名,国土面积)
                select name,countrycode from city where population<100;  得到PCN
                select name,surfacearea from country where code='PCN';
                上面命令合为下边的一条:
                select city.name,country.name,country.surfacearea from city,country where city.countrycode = country.code and city.population<100;
      ----------------
      内连接
          1、找表之间的关系列
          2、将俩表放在join左右
          3、将关联条件了放在on后面
          4、将所有的查询条件进行罗列。
      ---------------
      例子:
           1、查询人口数量小于100人的国家名。
              select country.name,city.name,country.SurfaceArea from city join country on city.countrycode=country.code where city.population<100;
           2、查询oldguo老师和他教的课程名称。
              select teacher.tname,course.cname from teacher join course on teacher.tno=course.tno where teacher.tname='oldguo';
           3、查询oldguo老师教的学生姓名列表
           select teacher.tname,group_concat(student.sname)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo';

           6、查询oldboy老师教的不及格学生的姓名
           select teacher.tname,group_concat(student.sname)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo' and score.score <60
           group by student.sname;
           7、 统计zhang3,学习了几门课
           select student.sname,sum(score.sno)
           from student
           join score
           on student.sno=score.sno
           join course
           on score.cno=course.cno
           where student.sname='zhang3' and student.sno
           group by student.sno;
           8、 统计zhang3,学习了几门课
           select student.sname,course.cname
           from student
           join score
           on student.sno=score.sno
           join course
           on score.cno=course.cno
           where student.sname='zhang3';
           9、查询oldguo老师教的学生名.
           select teacher.tname,student.sname
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo';
           10、查询oldguo所教课程的平均分数
           select teacher.tname,avg(score.score)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           where teacher.tname='oldguo';
           11.每位老师所教课程的平均分,并按平均分排序
           select teacher.tname,avg(score.score)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           group by teacher.tno
           order by avg(score.score);
           12.查询oldguo所教的不及格的学生姓名
           select teacher.tname,group_concat(student.sname)
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where teacher.tname='oldguo' and score.score <60
           group by student.sname;
           13.查询所有老师所教学生不及格的信息
           select teacher.tname,student.sname,score.score
           from teacher
           join course
           on teacher.tno=course.tno
           join score
           on course.cno=score.cno
           join student
           on score.sno=student.sno
           where score.score <60
           ----------------------------------
           mysql> SELECT course.cno,course.cname,SUM(sc.score)
               -> FROM course
               -> JOIN sc
               -> ON course.cno = sc.cno
               -> GROUP BY course.cname;

           ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'school.course.cno' which
           is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

           1. 在select后面出现的列,不是分组条件,并且没有在函数中出现。
           2. 如果group by 后是主键列或者是唯一条件列。如下:

           SELECT
           course.cno,course.cname,SUM(sc.score)                                                                    FROM course
           JOIN sc
           ON course.cno = sc.cno
           GROUP BY course.cno;
           -------------------------------------

2、show
3、Information_schema

--- 6. 查询oldboy老师教的不及格学生的姓名
--- 7. 统计zhang3,学习了几门课
--- 8. 查询zhang3,学习的课程名称有哪些?
--- 9. 查询oldguo老师教的学生名.
--- 10.查询oldguo所教课程的平均分数
--- 11.每位老师所教课程的平均分,并按平均分排序
--- 12.查询oldguo所教的不及格的学生姓名
--- 13.查询所有老师所教学生不及格的信息
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容