6.Mysql之基本SQL语法(03)
1.前言:
这小结主要是用来细说多表连接(多表查询),其中多表查询是DBA开发与运维中是重要的一环,对于从事DBA的人员来说,该技能是必须要掌握的,然后是该小结也是Mysql之基本SQL语法的最后小结
2. 多表查询
2.1语法格式
select 表名.列 ...from 表名1
join 表名2 on 表名1.列1=表名2.列2 where ... group by ... having ... order by ... Limit ...
2.2 一定要按照以上顺序语法进行书写,格式首先要正确
2.3多张表进行关联语法:A join b on A.x=b.y join C on b.m=C.n
3.示例
3.1 ER图如下:
3.2 整体实例如下:
1.创建一个数据库school create database school charset utf8mb4; 2.创建学生表(student) CREATE TABLE student( sno INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学号', sname VARCHAR(20) NOT NULL COMMENT '姓名', sage TINYINT UNSIGNED NOT NULL COMMENT '年龄', ssex ENUM('f','m') NOT NULL DEFAULT 'm' COMMENT '性别' )ENGINE=INNODB CHARSET=utf8; 3.创建教师表(teacher) CREATE TABLE teacher( tno INT NOT NULL PRIMARY KEY COMMENT '教师编号', tname VARCHAR(20) NOT NULL COMMENT '教师名字' )ENGINE=INNODB CHARSET utf8; 4.创建课程表(course) CREATE TABLE course( cno INT NOT NULL PRIMARY KEY COMMENT '课程编号', cname VARCHAR(20) NOT NULL COMMENT '课程名字', tno INT NOT NULL COMMENT '教师编号' )ENGINE=INNODB CHARSET utf8; 5.创建成绩表(score) CREATE TABLE score ( sno INT NOT NULL COMMENT '学号', cno INT NOT NULL COMMENT '课程编号', score INT NOT NULL DEFAULT 0 COMMENT '成绩' )ENGINE=INNODB CHARSET=utf8; 6.插入数据: INSERT INTO student(sno,sname,sage,ssex) VALUES (1,'zhang3',18,'m'); INSERT INTO student(sno,sname,sage,ssex) VALUES (2,'zhang4',18,'m'), (3,'li4',18,'m'), (4,'wang5',19,'f'); INSERT INTO student VALUES (5,'zh4',18,'m'), (6,'zhao4',18,'m'), (7,'ma6',19,'f'); ========================================= INSERT INTO student(sname,sage,ssex) VALUES ('oldboy',20,'m'), ('oldgirl',20,'f'), ('oldp',25,'m'); ========================================= INSERT INTO teacher(tno,tname) VALUES (101,'oldboy'), (102,'hesw'), (103,'oldguo'); DESC course; ======================================== INSERT INTO course(cno,cname,tno) VALUES (1001,'linux',101), (1002,'python',102), (1003,'mysql',103); ============================== INSERT INTO score(sno,cno,score) VALUES (1,1001,80), (1,1002,59), (2,1002,90), (2,1003,100), (3,1001,99), (3,1003,40), (4,1001,79), (4,1002,61), (4,1003,99), (5,1003,40), (6,1001,89), (6,1003,77), (7,1001,67), (7,1003,82), (8,1001,70), (9,1003,80), (10,1003,96);
练习:
1.统计张三学了几门课程 select sname, count(cno) from student join sc on student.sno=sc.sno where sname='zhang3'; select student.sname, count(sc.cno) from student join sc on student.sno=sc.sno where student.sname='zhang3'; 2.查询oldguo老师教的学生名. select 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='oldguo'; 3.查询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'; select student.sname,group_concat(course.cname) from student join sc on student.sno=sc.sno join course on sc.cno=course.cno where student.sname='zhang3' group by student.sname; 4.查询oldguo所教课程的平均分数 select course.cname,avg(sc.score) from teacher join course on teacher.tno=course.tno join sc on course.cno=sc.cno where teacher.tname='oldboy'; 5.每位老师所教课程的平均分,并按平均分排序 select teacher.tname,course.cname,avg(sc.score) from teacher join course on teacher.tno=course.tno join sc on course.cno=sc.cno group by teacher.tname order by avg(sc.score); 6.查询oldguo所教的不及格的学生姓名 select student.sname,sc.score 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 and teacher.tname='oldguo'; 7.查询所有老师所教学生不及格的信息 select teacher.tname,student.sname,course.cname,sc.score 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 group by student.sname;
4.元数据信息查询
4.0 Mysql元数据信息包括很多内容:表的大小、表的创建时间、那个用户创建的该表、该表中的数据页的信息以及索引页的一些信息等等,今天这里主要将的是关于information_schema
下面的tables表,该表信息量很大且很有用,以下就是根据该表进行统计的一些查询信息。
1 root@localhost 00:37: [information_schema]> select * from tables limit 1 \G; 2 *************************** 1. row *************************** 3 TABLE_CATALOG: def 4 TABLE_SCHEMA: information_schema 5 TABLE_NAME: CHARACTER_SETS 6 TABLE_TYPE: SYSTEM VIEW 7 ENGINE: MEMORY 8 VERSION: 10 9 ROW_FORMAT: Fixed 10 TABLE_ROWS: NULL 11 AVG_ROW_LENGTH: 384 12 DATA_LENGTH: 0 13 MAX_DATA_LENGTH: 16434816 14 INDEX_LENGTH: 0 15 DATA_FREE: 0 16 AUTO_INCREMENT: NULL 17 CREATE_TIME: 2021-06-16 00:37:40 18 UPDATE_TIME: NULL 19 CHECK_TIME: NULL 20 TABLE_COLLATION: utf8_general_ci 21 CHECKSUM: NULL 22 CREATE_OPTIONS: max_rows=43690 23 TABLE_COMMENT: 24 1 row in set (0.03 sec)
4.1显示所有的库和表的信息
SELECT TABLE_SCHEMA ,TABLE_NAME from information_schema.tables; SELECT table_schema,GROUP_CONCAT(TABLE_name) from information_schema.tables group BY TABLE_schema;
4.2查询一下innodb引擎的表
SELECT TABLE_SCHEMA ,TABLE_NAME from information_schema.tables where engine='innodb';
4.3.统计某个库下的某个表占用空间大小
公式:某个表占用空间大小=平均行长度*行数+索引长度 举例1:统计shcool库下的student表占用空间的大下 SELECT TABLE_name,(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024 from information_schema.tables WHERE TABLE_schema='shcool' and TABLE_name='student'; 举例2: 统计shcool库下所有表的总大小(求这个库所占用的空间大下) SELECT table_schema,SUM((AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024) from information_schema.tables where TABLE_schema='shcool';
4.4 统计每个库的数据量大下,并按照数据量大下排序
SELECT TABLE_schema, SUM((AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024) as total_KB from information_schema.tables GROUP BY TABLE_schema order by total_KB DESC;
4.5.配合concat()函数拼接语句或命令 *****
4.5.1 模仿以下语句,进行数据库的分库分表备份
mysqldump -uroot -p123 world city >/bak/world_city.sql SELECT concat( "mysqldump -uroot -p123"," ",table_schema," ",TABLE_name," >/bak/",TABLE_schema,"_",table_name,".sql") from information_schema.tables
4.5.2模仿以下语句,进行批量生成对world库下所有表进行操作
ALTER TABLE world.city DISCARD TABLESPACE; select concat("ALTER TABLE", TABLE_schema,".",table_name," DISCARD TABLESPACE") from information_schema.tables WHERE TABLE_schema='shcool';
5.Show 命令总结
show databases; show tables; show create database xx show create table xx :查看所有建表语句 show processlist :查看所有用户连接用户情况 show charset: show collation: show grants for xx: :查看用户权限信息 show variables like '%xx%'; : 查看参数信息 show engine : 查看所有支持的存储引擎类型 show index from xxx : 查看表的索引信息 show engine innodb status\G :查看innodb引擎详细状态信息 show binary log :查看二进制日志的列表信息 show binlog events in '' :查看二进制日志的事件信息 show master status ; :查看mysql当前使用二进制日志信息 show slave status\G; :查看从库状态信息 show relaylog events in '' :查看中继日志的事件信息 show status like '' :查看数据整体状态信息