LuffyCity-MySQL综合练习50实例
Posted 天晴天朗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LuffyCity-MySQL综合练习50实例相关的知识,希望对你有一定的参考价值。
1、请创建如下表,并添加相应约束;
2、自行构造测试数据;
- 新建数据库
- 创建表
- 构造测试数据
#Step1-创建数据库LuffyCity_mysql; #CREATE DATABASE LuffyCity_MySQL CHARSET utf8; #Step2-创建数据表; #1、创建年级表class_grade(因为class表的外键要参考class_grade表的gid字段); CREATE TABLE class_grade ( gid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, gname VARCHAR (12) NOT NULL UNIQUE #年级的名称也是唯一值;指定存储引擎为InnoDB,字符编码为utf8后续不再赘述; ) ENGINE = INNODB CHARSET = utf8; #2、创建班级表class(因为学生表的外键class_id要参考班级表的cid字段); CREATE TABLE class ( cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, caption VARCHAR (16) NOT NULL UNIQUE, #班级名称也为唯一值; grade_id INT NOT NULL, CONSTRAINT fk_class_grade FOREIGN KEY (grade_id) REFERENCES class_grade (gid) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = INNODB CHARSET = utf8; #3、学生表student(与class表多对一关系); CREATE TABLE student ( sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, sname VARCHAR (12) NOT NULL, gender ENUM (\'男\', \'女\') DEFAULT \'男\' NOT NULL, class_id INT NOT NULL, CONSTRAINT fk_class FOREIGN KEY (class_id) REFERENCES class (cid) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = INNODB CHARSET = utf8; #4、老师表teacher(因为课程表的外键teacher_id要参考老师表的tid字段); CREATE TABLE teacher ( tid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, tname VARCHAR (8) NOT NULL ) ENGINE = INNODB CHARSET = utf8; #5、课程表course(因为成绩表的外键course_id要参考课程表的cid字段); CREATE TABLE course ( cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT, cname VARCHAR (8) NOT NULL UNIQUE, #课程名唯一值; teacher_id INT NOT NULL, CONSTRAINT fk_teacher FOREIGN KEY (teacher_id) REFERENCES teacher (tid) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE = INNODB CHARSET = utf8; #6、成绩表(因为成绩表的外键course_id要参考课程表的cid字段); CREATE TABLE score ( sid INT NOT NULL UNIQUE AUTO_INCREMENT, student_id INT NOT NULL, course_id INT NOT NULL, score INT NOT NULL, CONSTRAINT fk_student FOREIGN KEY (student_id) REFERENCES student (sid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_course FOREIGN KEY (course_id) REFERENCES course (cid) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (student_id, course_id) #多字段联合主键; ) ENGINE = INNODB CHARSET = utf8; #7、班级任职表teach2cls; CREATE TABLE teach2cls ( tcid INT NOT NULL UNIQUE AUTO_INCREMENT, tid INT NOT NULL, cid INT NOT NULL, CONSTRAINT fk_teacher1 FOREIGN KEY (tid) REFERENCES teacher (tid) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fk_class1 FOREIGN KEY (cid) REFERENCES class (cid) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (tid, cid) ) ENGINE = INNODB CHARSET = utf8; #Step3-插入测试数据; #年级表;创建6个年级; INSERT INTO class_grade (gname) VALUES (\'一年级\'), (\'二年级\'), (\'三年级\'), (\'四年级\'), (\'五年级\'), (\'六年级\'); #班级表;每个年级指定n个班级; INSERT INTO class (caption, grade_id) VALUES (\'一年级1班\', 1), (\'一年级2班\', 1), (\'一年级3班\', 1), (\'二年级1班\', 2), (\'二年级2班\', 2), (\'三年级1班\', 3), (\'四年级1班\', 4), (\'四年级2班\', 4), (\'四年级3班\', 4), (\'四年级4班\', 4), (\'五年级1班\', 5), (\'六年级1班\', 6), (\'六年级2班\', 6); #学生表; INSERT INTO student (sname, gender, class_id) VALUES (\'高志粉\', \'女\', 1), (\'李静瓶\', \'女\', 2), (\'崔晓昭\', \'男\', 3), (\'崔晓姗\', \'女\', 4), (\'崔晓思\', \'女\', 5), (\'崔青良\', \'男\', 6), (\'崔晓磊\', \'男\', 7), (\'高志国\', \'男\', 8), (\'崔晓岩\', \'女\', 9), (\'高晨曦\', \'女\', 10), (\'陈浩\', \'男\', 11), (\'陈浩茹\', \'女\', 10), (\'高若曦\', \'女\', 9), (\'武倩倩\', \'女\', 12), (\'武若冰\', \'女\', 13); INSERT INTO teacher (tname) VALUES (\'崔树齐\'), (\'宋俊泽\'), (\'孙增良\'), (\'张传伟\'), (\'邓琼\'); INSERT INTO course (cname, teacher_id) VALUES (\'生物\', 1), (\'物理\', 2), (\'化学\', 3), (\'语文\', 3), (\'数学\', 4), (\'地理\', 2), (\'历史\', 5); INSERT INTO score (student_id, course_id, score) VALUES (1, 1, 60), (1, 2, 59), (2, 4, 60), (2, 5, 59), (2, 6, 33), (3, 1, 59), (3, 5, 28), (4, 4, 88), (4, 6, 90), (5, 4, 88), (6, 5, 86), (6, 6, 60), (7, 3, 57), (7, 5, 60), (8, 2, 61), (8, 4, 59), (9, 1, 60), (9, 2, 61), (9, 3, 33), (10, 5, 58), (11, 1, 89), (12, 3, 70), (13, 2, 80), (14, 1, 90), (15, 3, 91); INSERT INTO teach2cls (tid, cid) VALUES (1, 1), (1, 2), (1, 3), (1, 7), (2, 4), (2, 8), (2, 7), (2, 5), (3, 9), (3, 3), (3, 5), (3, 2), (4, 8), (4, 4), (4, 6), (5, 10), (5, 11), (5, 13), (5, 12);
3、查询练习;
1、自行创建测试数据;
2、查询学生总人数;
SELECT COUNT(cid) AS \'班级总人数\' FROM class;
3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
/* 3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名; 解题分析: 1、成绩及格,即score >=60; 2、成绩表的course_id 和 课程表的cid建立内连接查询,条件是course.cname 包含(\'生物\',\'物理\')且 成绩大于60; 3、将查询到的结果,作为student表的子查询; */ SELECT sid AS \'学生id\', sname AS \'姓名\' FROM student WHERE sid IN ( SELECT score.student_id FROM score INNER JOIN course ON score.course_id = course.cid WHERE course.cname IN (\'生物\', \'物理\') AND score.score >= 60 );
4、查询每个年级的班级数,取出班级数最多的前三个年级;
/* 4、查询每个年级的班级数,取出班级数最多的前三个年级; 解题分析: 1、年级名称及数量,从class_grade表和class表进行联合查询; 2、班级表的grade_id字段与年级表的gid字段,通过内连接关联查询; 3、通过聚合函数count计数caption,来统计班级数; */ SELECT class_grade.gname, COUNT(caption) FROM class INNER JOIN class_grade ON class.grade_id = class_grade.gid GROUP BY grade_id ORDER BY COUNT(caption) DESC LIMIT 3;
5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
/* 5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩; 解题分析: 1、成绩表中查询score; 2、通过avg成绩升级排序,以及limit1 组合,得到最高和最低的成绩; 3、最高与最低成绩的union去重后,作为学生表的子查询条件; */ SELECT t3.student_id AS "学号", t4.sname AS "姓名", t3.avg_score AS "平均成绩" FROM ( SELECT * FROM ( SELECT student_id, avg(score) AS avg_score FROM score GROUP BY student_id ORDER BY avg(score) DESC LIMIT 1 ) AS t1 UNION SELECT * FROM ( SELECT student_id, avg(score) FROM score GROUP BY student_id ORDER BY avg(score) ASC LIMIT 1 ) AS t2 ) AS t3 INNER JOIN student AS t4 ON t3.student_id = t4.sid;
6、查询每个年级的学生人数;
/* 6、查询每个年级的学生人数; 解题分析: 1、学生表与班级表进行左连接; 2、班级表与年级表通过inner join链接; 3、聚合函数count计数sid来统计学生数; 4、需要起多个别名来进行组合查询; */ SELECT gname AS \'年级\', num AS \'总人数\' FROM ( SELECT class.grade_id AS cg_id, COUNT(sid) AS num FROM student INNER JOIN class ON student.class_id = class.cid GROUP BY class.grade_id ) AS t2 INNER JOIN class_grade ON t2.cg_id = class_grade.gid ORDER BY num DESC;
7、查询每位学生的学号,姓名,选课数,平均成绩;
/* 7、查询每位学生的学号,姓名,选课数,平均成绩; 解题分析: 1、选课数来源于score表,而不是course表; 2、学号及sid,姓名即sname,选课数即count(course_id),平均成绩即avg(score) */ SELECT student.sid AS \'学号\', student.sname AS \'姓名\', count(score.course_id) AS \'选课数\', avg(score.score) AS \'平均成绩\' FROM student INNER JOIN score ON student.sid = score.student_id GROUP BY student.sid;
8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
/* 8、查询学生编号为“2”的学生的姓名、 该学生成绩最高的课程名、 成绩最低的课程名及分数; 解题分析: 1、max(score)取得最高成绩,min(score)取得最低成绩 2、 */ SELECT student.sname, course.cname, t1.score FROM ( SELECT student_id, course_id, score FROM score WHERE student_id = 2 AND score IN ( ( SELECT min(score) FROM score WHERE student_id = 2 ), ( SELECT max(score) FROM score WHERE student_id = 2 ) ) ) AS t1 INNER JOIN student ON t1.student_id = student.sid INNER JOIN course ON t1.course_id = course.cid;
9、查询姓“李”的老师的个数和所带班级数;
/* 9、查询姓“李”的老师的个数和所带班级数; 解题分析: 1、teacher表、teach2cls表连接查询; 2、like模糊匹配; */ SELECT teacher.tid, teacher.tname, t1.count_cid FROM teacher LEFT JOIN ( SELECT tid, count(cid) AS count_cid FROM teach2cls WHERE tid IN ( SELECT tid FROM teacher WHERE tname LIKE \'张%\' ) GROUP BY tid ) AS t1 ON teacher.tid = t1.tid WHERE teacher.tname LIKE \'张%\';
10、查询班级数小于5的年级id和年级名;
/* 10、查询班级数小于5的年级id和年级名; 解题分析: 1、从class_grade以及teach2cls中联合查询; */ #方案1; SELECT class_grade.gid AS \'年级id\', class_grade.gname AS \'年级名\' FROM class_grade INNER JOIN class ON class_grade.gid = class.grade_id GROUP BY class_grade.gid HAVING COUNT(caption) < 学习笔记30Python基础综合练习030 SSM综合练习06--数据后台管理系统--SSM权限操作及Spring Security入门
Python初级练习小实例(21-50例),1个实例多个例子相互参考