mysql基础练习

Posted cjwnb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql基础练习相关的知识,希望对你有一定的参考价值。

环境准备

第一步: 创建库

MariaDB [(none)]> create database s17 charset='utf-8';
MariaDB [(none)]> use s17;

第二步: 创建表

MariaDB [s17]> CREATE TABLE `class` (
    ->   `cid` int(11) NOT NULL AUTO_INCREMENT,
    ->   `caption` varchar(32) NOT NULL,
    ->   PRIMARY KEY (`cid`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
    
MariaDB [s17]> CREATE TABLE `teacher` (
    ->   `tid` int(11) NOT NULL AUTO_INCREMENT,
    ->   `tname` varchar(32) NOT NULL,
    ->   PRIMARY KEY (`tid`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
    
MariaDB [s17]> CREATE TABLE `course` (
    ->   `cid` int(11) NOT NULL AUTO_INCREMENT,
    ->   `cname` varchar(32) NOT NULL,
    ->   `teacher_id` int(11) NOT NULL,
    ->   PRIMARY KEY (`cid`),
    ->   KEY `fk_course_teacher` (`teacher_id`),
    ->   CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
    
    
MariaDB [s17]> CREATE TABLE `student` (
    ->   `sid` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    ->   `gender` char(1) NOT NULL,
    ->   `class_id` int(11) NOT NULL,
    ->   `sname` varchar(32) NOT NULL,
    ->   CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

MariaDB [s17]> CREATE TABLE `score` (
    ->   `sid` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    ->   `student_id` int(11) NOT NULL,
    ->   `course_id` int(11) NOT NULL,
    ->   `num` int(11) NOT NULL,
    ->   CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
    ->   CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;

第三步: 准备一些数据

class表准备数据

MariaDB [s17]> INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');
MariaDB [s17]> select * from class;
+-----+--------------+
| cid | caption      |
+-----+--------------+
|   1 | 三年二班     |
|   2 | 三年三班     |
|   3 | 一年二班     |
|   4 | 二年九班     |
+-----+--------------+

teacher表准备数据

MariaDB [s17]> INSERT INTO `teacher` VALUES ('1', '张磊老师'), ('2', '李平老师'), ('3', '刘海燕老师'), ('4', '朱云海老师'), ('5', '李杰老师');

MariaDB [s17]> select * from teacher;
+-----+-----------------+
| tid | tname           |
+-----+-----------------+
|   1 | 张磊老师        |
|   2 | 李平老师        |
|   3 | 刘海燕老师      |
|   4 | 朱云海老师      |
|   5 | 李杰老师        |
+-----+-----------------+

course表准备数据

MariaDB [s17]> INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '体育', '3'), ('4', '美术', '2');

MariaDB [s17]> select * from course;
+-----+--------+------------+
| cid | cname  | teacher_id |
+-----+--------+------------+
|   1 | 生物   |          1 |
|   2 | 物理   |          2 |
|   3 | 体育   |          3 |
|   4 | 美术   |          2 |
+-----+--------+------------+

student表准备数据

MariaDB [s17]> INSERT INTO `student` VALUES ('1', '男', '1', '李四'), ('2', '女', '1', '钢蛋'), ('3', '男', '1', '张三'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '刘三'), ('14', '男', '3', '刘一'), ('15', '女', '3', '刘二'), ('16', '男', '3', '刘四');


MariaDB [s17]> select * from student;
+-----+--------+----------+--------+
| sid | gender | class_id | sname  |
+-----+--------+----------+--------+
|   1 | 男     |        1 | 李四   |
|   2 | 女     |        1 | 钢蛋   |
|   3 | 男     |        1 | 张三   |
|   4 | 男     |        1 | 张一   |
|   5 | 女     |        1 | 张二   |
|   6 | 男     |        1 | 张四   |
|   7 | 女     |        2 | 铁锤   |
|   8 | 男     |        2 | 李三   |
|   9 | 男     |        2 | 李一   |
|  10 | 女     |        2 | 李二   |
|  11 | 男     |        2 | 李四   |
|  12 | 女     |        3 | 如花   |
|  13 | 男     |        3 | 刘三   |
|  14 | 男     |        3 | 刘一   |
|  15 | 女     |        3 | 刘二   |
|  16 | 男     |        3 | 刘四   |
+-----+--------+----------+--------+

score表准备数据

MariaDB [s17]> INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');

MariaDB [s17]> select * from score;
+-----+------------+-----------+-----+
| sid | student_id | course_id | num |
+-----+------------+-----------+-----+
|   1 |          1 |         1 |  10 |
|   2 |          1 |         2 |   9 |
|   5 |          1 |         4 |  66 |
|   6 |          2 |         1 |   8 |
|   8 |          2 |         3 |  68 |
|   9 |          2 |         4 |  99 |
|  10 |          3 |         1 |  77 |
|  11 |          3 |         2 |  66 |
|  12 |          3 |         3 |  87 |
|  13 |          3 |         4 |  99 |
|  14 |          4 |         1 |  79 |
|  15 |          4 |         2 |  11 |
|  16 |          4 |         3 |  67 |
|  17 |          4 |         4 | 100 |
|  18 |          5 |         1 |  79 |
|  19 |          5 |         2 |  11 |
|  20 |          5 |         3 |  67 |
|  21 |          5 |         4 | 100 |
|  22 |          6 |         1 |   9 |
|  23 |          6 |         2 | 100 |
|  24 |          6 |         3 |  67 |
|  25 |          6 |         4 | 100 |
|  26 |          7 |         1 |   9 |
|  27 |          7 |         2 | 100 |
|  28 |          7 |         3 |  67 |
|  29 |          7 |         4 |  88 |
|  30 |          8 |         1 |   9 |
|  31 |          8 |         2 | 100 |
|  32 |          8 |         3 |  67 |
|  33 |          8 |         4 |  88 |
|  34 |          9 |         1 |  91 |
|  35 |          9 |         2 |  88 |
|  36 |          9 |         3 |  67 |
|  37 |          9 |         4 |  22 |
|  38 |         10 |         1 |  90 |
|  39 |         10 |         2 |  77 |
|  40 |         10 |         3 |  43 |
|  41 |         10 |         4 |  87 |
|  42 |         11 |         1 |  90 |
|  43 |         11 |         2 |  77 |
|  44 |         11 |         3 |  43 |
|  45 |         11 |         4 |  87 |
|  46 |         12 |         1 |  90 |
|  47 |         12 |         2 |  77 |
|  48 |         12 |         3 |  43 |
|  49 |         12 |         4 |  87 |
|  52 |         13 |         3 |  87 |
+-----+------------+-----------+-----+

表之间的关联关系

技术图片

练习题

第一轮

查询“生物”课程比“物理”课程成绩高的所有学生的学号

----> 获取所有有生物课程的人(学号,成绩) - 临时表
MariaDB [s17]> select student_id,num as sw from score left join course on score.course_id=course.cid where course.cname='生物';
+------------+----+
| student_id | sw |
+------------+----+
|          1 | 10 |
|          2 |  8 |
|          3 | 77 |
|          4 | 79 |
|          5 | 79 |
|          6 |  9 |
|          7 |  9 |
|          8 |  9 |
|          9 | 91 |
|         10 | 90 |
|         11 | 90 |
|         12 | 90 |
+------------+----+



----> 获取所有有物理课程的人(学号,成绩) - 临时表
MariaDB [s17]> select student_id,num as wl from score left join course on score.course_id=course.cid where course.cname='物理';
+------------+-----+
| student_id | wl  |
+------------+-----+
|          1 |   9 |
|          3 |  66 |
|          4 |  11 |
|          5 |  11 |
|          6 | 100 |
|          7 | 100 |
|          8 | 100 |
|          9 |  88 |
|         10 |  77 |
|         11 |  77 |
|         12 |  77 |
+------------+-----+


----> 根据【学号】连接两个临时表:
MariaDB [s17]> select A.student_id, sw,wl from (select student_id,num as sw from score left join course on score.course_id=course.cid where course.cname='生物') as A left join (select student_id,num as wl from score left join course on score.course_id=course.cid where course.cname='物理') as B on A.student_id=B.student_id where sw>if(isnull(wl),0,wl);
+------------+----+------+
| student_id | sw | wl   |
+------------+----+------+
|          1 | 10 |    9 |
|          2 |  8 | NULL |
|          3 | 77 |   66 |
|          4 | 79 |   11 |
|          5 | 79 |   11 |
|          9 | 91 |   88 |
|         10 | 90 |   77 |
|         11 | 90 |   77 |
|         12 | 90 |   77 |
+------------+----+------+

----> 将NULl这一列显示出来
MariaDB [s17]> select A.student_id, sw,wl from (select student_id,num as sw from score left join course on score.course_id=course.cid where course.cname='生物') as A left join (select student_id,num as wl from score left join course on score.course_id=course.cid where course.cname='物理') as B on A.student_id=B.student_id where sw>if(isnull(wl),0,wl);
+------------+----+------+
| student_id | sw | wl   |
+------------+----+------+
|          1 | 10 |    9 |
|          2 |  8 | NULL |
|          3 | 77 |   66 |
|          4 | 79 |   11 |
|          5 | 79 |   11 |
|          9 | 91 |   88 |
|         10 | 90 |   77 |
|         11 | 90 |   77 |
|         12 | 90 |   77 |
+------------+----+------+

说明:?

isnull(wl) 如果该字段的值非空, 则返回0; 否则返回1;

IF(expr1,expr2,expr3) 如果expr1的值为true,则返回expr2的值,如果expr1的值为false,

则返回expr3的值

查询平均成绩大于60分的同学的学号和平均成绩

MariaDB [s17]> select student_id, avg(num) from score group by student_id having avg(num)>60;
+------------+----------+
| student_id | avg(num) |
+------------+----------+
|          3 |  82.2500 |
|          4 |  64.2500 |
|          5 |  64.2500 |
|          6 |  69.0000 |
|          7 |  66.0000 |
|          8 |  66.0000 |
|          9 |  67.0000 |
|         10 |  74.2500 |
|         11 |  74.2500 |
|         12 |  74.2500 |
|         13 |  87.0000 |
+------------+----------+

having和where的区别: having的原理是先select 然后从select出来的进行筛选。而where是先筛选在select, 所以这里只能用having

查询所有同学的学号、姓名、选课数、总成绩

MariaDB [s17]> select score.student_id,student.sname, count(student_id),sum(num) from score left join student on score.student_id=student.sid group by student_id;
+------------+--------+-------------------+----------+
| student_id | sname  | count(student_id) | sum(num) |
+------------+--------+-------------------+----------+
|          1 | 李四   |                 3 |       85 |
|          2 | 钢蛋   |                 3 |      175 |
|          3 | 张三   |                 4 |      329 |
|          4 | 张一   |                 4 |      257 |
|          5 | 张二   |                 4 |      257 |
|          6 | 张四   |                 4 |      276 |
|          7 | 铁锤   |                 4 |      264 |
|          8 | 李三   |                 4 |      264 |
|          9 | 李一   |                 4 |      268 |
|         10 | 李二   |                 4 |      297 |
|         11 | 李四   |                 4 |      297 |
|         12 | 如花   |                 4 |      297 |
|         13 | 刘三   |                 1 |       87 |
+------------+--------+-------------------+----------+

查询姓“李”的老师的个数

MariaDB [s17]> select count(tid) from teacher where tname like '李%';
+------------+
| count(tid) |
+------------+
|          2 |
+------------+

查询没学过“李平”老师课的同学的学号、姓名

----> 先查到“李平老师”老师教的所有课ID     
MariaDB [s17]> select cid from course left join teacher on teacher.tid=course.teacher_id where tname='李平老师';
+-----+
| cid |
+-----+
|   2 |
|   4 |
+-----+

----> 获取选过课的所有学生ID
MariaDB [s17]> select DISTINCT student_id from score  where score.course_id in (select cid from course left join teacher on teacher.tid=course.teacher_id where tname='李平老师'); 
+------------+
| student_id |
+------------+
|          1 |
|          3 |
|          4 |
|          5 |
|          6 |
|          7 |
|          8 |
|          9 |
|         10 |
|         11 |
|         12 |
|          2 |
+------------+

----> 学生表中筛选
MariaDB [s17]> select sid,sname from student where sid not in (select DISTINCT student_id from score  where score.course_id in (select cid from course left join teacher on teacher.tid=course.teacher_id where tname='李平老师')); 
+-----+--------+
| sid | sname  |
+-----+--------+
|  13 | 刘三   |
|  14 | 刘一   |
|  15 | 刘二   |
|  16 | 刘四   |
+-----+--------+ 

查询学过“001”并且也学过编号“002”课程的同学的学号、姓名

----> 先查到学过“001”或者也学过编号“002”课程的所有课程的student_id
MariaDB [s17]> select course_id,student_id from score where course_id=1 or course_id=2;
+-----------+------------+
| course_id | student_id |
+-----------+------------+
|         1 |          1 |
|         2 |          1 |
|         1 |          2 |
|         1 |          3 |
|         2 |          3 |
|         1 |          4 |
|         2 |          4 |
|         1 |          5 |
|         2 |          5 |
|         1 |          6 |
|         2 |          6 |
|         1 |          7 |
|         2 |          7 |
|         1 |          8 |
|         2 |          8 |
|         1 |          9 |
|         2 |          9 |
|         1 |         10 |
|         2 |         10 |
|         1 |         11 |
|         2 |         11 |
|         1 |         12 |
|         2 |         12 |
+-----------+------------+

----> 统计学生学习课程的数量
MariaDB [s17]> select sid,sname,count(sid) from student left join (select course_id,student_id from score where course_id=1 or course_id=2) as A on student.sid=A.student_id group by sid;
+-----+--------+------------+
| sid | sname  | count(sid) |
+-----+--------+------------+
|   1 | 李四   |          2 |
|   2 | 钢蛋   |          1 |
|   3 | 张三   |          2 |
|   4 | 张一   |          2 |
|   5 | 张二   |          2 |
|   6 | 张四   |          2 |
|   7 | 铁锤   |          2 |
|   8 | 李三   |          2 |
|   9 | 李一   |          2 |
|  10 | 李二   |          2 |
|  11 | 李四   |          2 |
|  12 | 如花   |          2 |
|  13 | 刘三   |          1 |
|  14 | 刘一   |          1 |
|  15 | 刘二   |          1 |
|  16 | 刘四   |          1 |
+-----+--------+------------+

----> 将学生学习的课程数量是2的筛选出来
MariaDB [s17]> select sid,sname from student left join (select course_id,student_id from score where course_id=1 or course_id=2) as A on student.sid=A.student_id group by sid having count(sid)=2;
+-----+--------+
| sid | sname  |
+-----+--------+
|   1 | 李四   |
|   3 | 张三   |
|   4 | 张一   |
|   5 | 张二   |
|   6 | 张四   |
|   7 | 铁锤   |
|   8 | 李三   |
|   9 | 李一   |
|  10 | 李二   |
|  11 | 李四   |
|  12 | 如花   |
+-----+--------+

查询学过“李平”老师所教的所有课的同学的学号、姓名

----> 将李平考试教的课程的cid列出来
MariaDB [s17]> select cid from teacher left join course on teacher.tid=course.teacher_id where tname='李平老师';
+------+
| cid  |
+------+
|    2 |
|    4 |
+------+

----> 将选择李平老师的课程的所有student_id弄出来
MariaDB [s17]> select student_id from score where course_id  in (select cid from teacher left join course on teacher.tid=course.teacher_id where tname='李平老师');
+------------+
| student_id |
+------------+
|          1 |
|          3 |
|          4 |
|          5 |
|          6 |
|          7 |
|          8 |
|          9 |
|         10 |
|         11 |
|         12 |
|          1 |
|          2 |
|          3 |
|          4 |
|          5 |
|          6 |
|          7 |
|          8 |
|          9 |
|         10 |
|         11 |
|         12 |
+------------+

----> 将student表中的sid与选择李平老师的学生的id做比对, 如果student表中的sid包含于这些id, 则筛选出数据
MariaDB [s17]> select DISTINCT sid,sname from student where sid in (select student_id from score where course_id  in (select cid from teacher left join course on teacher.tid=course.teacher_id where tname='李平老师'));
+-----+--------+
| sid | sname  |
+-----+--------+
|   1 | 李四   |
|   2 | 钢蛋   |
|   3 | 张三   |
|   4 | 张一   |
|   5 | 张二   |
|   6 | 张四   |
|   7 | 铁锤   |
|   8 | 李三   |
|   9 | 李一   |
|  10 | 李二   |
|  11 | 李四   |
|  12 | 如花   |
+-----+--------+

查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名

----> 获取所有学习课程编号001的人(学号,姓名) - 临时表
MariaDB [s17]> select student.sid,sname,num from score left join student on score.student_id=student.sid where score.course_id=1 ;
+------+--------+-----+
| sid  | sname  | num |
+------+--------+-----+
|    1 | 李四   |  10 |
|    2 | 钢蛋   |   8 |
|    3 | 张三   |  77 |
|    4 | 张一   |  79 |
|    5 | 张二   |  79 |
|    6 | 张四   |   9 |
|    7 | 铁锤   |   9 |
|    8 | 李三   |   9 |
|    9 | 李一   |  91 |
|   10 | 李二   |  90 |
|   11 | 李四   |  90 |
|   12 | 如花   |  90 |
+------+--------+-----+

----> 获取所有学习课程编号002的人(学号,姓名) - 临时表
MariaDB [s17]> select student.sid,sname,num from score left join student on score.student_id=student.sid where score.course_id=2 ;
+------+--------+-----+
| sid  | sname  | num |
+------+--------+-----+
|    1 | 李四   |   9 |
|    3 | 张三   |  66 |
|    4 | 张一   |  11 |
|    5 | 张二   |  11 |
|    6 | 张四   | 100 |
|    7 | 铁锤   | 100 |
|    8 | 李三   | 100 |
|    9 | 李一   |  88 |
|   10 | 李二   |  77 |
|   11 | 李四   |  77 |
|   12 | 如花   |  77 |
+------+--------+-----+

----> 根据【学号】连接两个临时表并把分数比较高的学号和姓名显示出来
MariaDB [s17]> select A.sid,A.sname from (select student.sid,sname,num as first from score left join student on score.student_id=student.sid where score.course_id=1) as A left join (select student.sid,sname,num as second from score left join student on score.student_id=student.sid where score.course_id=2) as B on A.sid=B.sid where first>if(isnull(second),0,second);
+------+--------+
| sid  | sname  |
+------+--------+
|    1 | 李四   |
|    2 | 钢蛋   |
|    3 | 张三   |
|    4 | 张一   |
|    5 | 张二   |
|    9 | 李一   |
|   10 | 李二   |
|   11 | 李四   |
|   12 | 如花   |
+------+--------+

以上是关于mysql基础练习的主要内容,如果未能解决你的问题,请参考以下文章

MySQL----基础sql练习

mysql基础SQL练习

mysql基础练习

MySQL增删查改 练习(基础)

第24章 零基础学MySQL 章节练习题

10)-MySQL数据库基础练习(答案版)