MySQL数据库

Posted 暮光微凉

tags:

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

一、创建如下表,并创建相关约束

 

二、操作表

  1、自行创建测试数据;

  2、查询学生总人数;

  3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;

  4、查询每个年级的班级数,取出班级数最多的前三个年级;

  5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;

  6、查询每个年级的学生人数;

  7、查询每位学生的学号,姓名,选课数,平均成绩;

  8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;

  9、查询姓“李”的老师的个数和所带班级数;

  10、查询班级数小于5的年级id和年级名;

  11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;

  

  12、查询学过“张三”老师2门课以上的同学的学号、姓名;

  13、查询教授课程超过2门的老师的id和姓名;

  14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;

  15、查询没有带过高年级的老师id和姓名;

  16、查询学过“张三”老师所教的所有课的同学的学号、姓名;

  17、查询带过超过2个班级的老师的id和姓名;

  18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;

  19、查询所带班级数最多的老师id和姓名;

  20、查询有课程成绩小于60分的同学的学号、姓名;

  21、查询没有学全所有课的同学的学号、姓名;

  22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;

  23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;

  24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;

  25、删除学习“张三”老师课的score表记录;

  26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;

  27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

  28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

  29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

  30、课程平均分从高到低显示(现实任课老师);

  31、查询各科成绩前三名的记录(不考虑成绩并列情况)

  32、查询每门课程被选修的学生数;

  33、查询选修了2门以上课程的全部学生的学号和姓名;

  34、查询男生、女生的人数,按倒序排列;

  35、查询姓“张”的学生名单;

  36、查询同名同姓学生名单,并统计同名人数;

  37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

  38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;

  39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;

  40、求选修了课程的学生人数

  41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;

  42、查询各个课程及相应的选修人数;

  43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

  44、查询每门课程成绩最好的前两名学生id和姓名;

  45、检索至少选修两门课程的学生学号;

  46、查询没有学生选修的课程的课程号和课程名;

  47、查询没带过任何班级的老师id和姓名;

  48、查询有两门以上课程超过80分的学生id及其平均成绩;

  49、检索“3”课程分数小于60,按分数降序排列的同学学号;

  50、删除编号为“2”的同学的“1”课程的成绩;

  51、查询同时选修了物理课和生物课的学生id和姓名;

 

此处是求知的分割线

--------------------------------------------------------------------------------------------------------------------------------------------

test_1

建库建表
            # 作业库 
                create database mysql_work; 
            # 年级表
                create table class_grade(
                -> gid int not null auto_increment,
                -> gname varchar(20) not null unique,
                -> primary key(gid)
                -> );
            # 班级表
                create table class(
                -> cid int not null primary key auto_increment,
                -> caption varchar(20) not null,
                -> grade_id int not null,
                -> foreign key(grade_id) references class_grade(gid)
                -> );
            # 学生表
                create table student(
                -> sid int not null primary key auto_increment,
                -> gender enum(\'\',\'\') not null,
                -> class_id int not null,
                -> foreign key(class_id) references class(cid)
                -> );
            # 老师表
                create table teacher(
                -> tid int not null primary key auto_increment,
                -> tname varchar(20) not null
                -> );
            # 课程表
                create table course(
                -> cid int not null primary key auto_increment,
                -> cname varchar(20) not null,
                -> teacher_id int not null ,
                -> foreign key(teacher_id) references teacher(tid)
                -> );
            # 成绩表
                create table score(
                -> sid int not null unique auto_increment,
                -> student_id int not null,
                -> course_id int not null,
                -> score int not null,
                -> primary key(student_id,course_id),
                -> foreign key(student_id) references student(sid)
                -> on delete cascade
                -> on update cascade,
                -> foreign key(course_id) references course(cid)
                -> on delete cascade
                -> on update cascade
                -> );
            # 班级任职表
                create table teach2cls(
                -> tcid int not null unique auto_increment,
                -> tid int not null,
                -> cid int not null,
                -> primary key(tid,cid),
                -> foreign key(tid) references teacher(tid)
                -> on delete cascade
                -> on update cascade,
                -> foreign key(cid) references course(cid)
                -> on delete cascade
                -> on update cascade
                -> );
                
        插入数据
            insert into class_grade(gname) values    # 4个年级
            (\'一年级\'),
            (\'二年级\'),
            (\'三年级\'),
            (\'四年级\');
            +-----+-----------+
            | gid | gname     |
            +-----+-----------+
            |   1 | 一年级    |
            |   3 | 三年级    |
            |   2 | 二年级    |
            |   4 | 四年级    |
            +-----+-----------+
            
            insert into class(caption,grade_id) values  # 9个班级
            (\'一年一班\',1),
            (\'一年二班\',1),
            (\'一年三班\',1),
            (\'二年一班\',2),
            (\'二年二班\',2),
            (\'三年一班\',3),
            (\'三年二班\',3),
            (\'四年一班\',4),
            (\'四年二班\',4);
            +-----+--------------+----------+
            | cid | caption      | grade_id |
            +-----+--------------+----------+
            |   1 | 一年一班     |        1 |
            |   2 | 一年二班     |        1 |
            |   3 | 一年三班     |        1 |
            |   4 | 二年一班     |        2 |
            |   5 | 二年二班     |        2 |
            |   6 | 三年一班     |        3 |
            |   7 | 三年二班     |        3 |
            |   8 | 四年一班     |        4 |
            |   9 | 四年二班     |        4 |
            +-----+--------------+----------+
            
            insert into student(sname,gender,class_id) values  # 12个学生
            (\'Jane\',\'\',1),
            (\'Rose\',\'\',1),
            (\'Jack\',\'\',2),
            (\'Alice\',\'\',2),
            (\'Alex\',\'\',3),
            (\'Drigon\',\'\',4),
            (\'Lily\',\'\',5),
            (\'Lucy\',\'\',6),
            (\'Jone\',\'\',6),
            (\'紫霞\',\'\',7),
            (\'张尊宝\',\'\',8),
            (\'高圆圆\',\'\',9);
            +-----+--------+----------+-----------+
            | sid | gender | class_id | sname     |
            +-----+--------+----------+-----------+
            |   1 | 女     |        1 | Jane      |
            |   2 | 女     |        1 | Rose      |
            |   3 | 男     |        2 | Jack      |
            |   4 | 女     |        2 | Alice     |
            |   5 | 男     |        3 | Alex      |
            |   6 | 男     |        4 | Drigon    |
            |   7 | 女     |        5 | Lily      |
            |   8 | 女     |        6 | Lucy      |
            |   9 | 男     |        6 | Jone      |
            |  10 | 女     |        7 | 紫霞      |
            |  11 | 男     |        8 | 张尊宝    |
            |  12 | 女     |        9 | 高圆圆    |
            +-----+--------+----------+-----------+
            
            insert into teacher(tname) values   # 4个老师
            (\'曹显\'),
            (\'王浩\'),
            (\'王五\'),
            (\'赵坤\');
            +-----+--------+
            | tid | tname  |
            +-----+--------+
            |   1 | 曹显   |
            |   2 | 王浩   |
            |   3 | 王五   |
            |   4 | 赵坤   |
            +-----+--------+
            
            insert into course(cname,teacher_id) values  # 6门课程
            (\'生物\',1),
            (\'物理\',2),
            (\'化学\',3),
            (\'语文\',3),
            (\'数学\',4),
            (\'地理\',2);
            +-----+--------+------------+
            | cid | cname  | teacher_id |
            +-----+--------+------------+
            |   1 | 生物   |          1 |
            |   2 | 物理   |          2 |
            |   3 | 化学   |          3 |
            |   4 | 语文   |          3 |
            |   5 | 数学   |          4 |
            |   6 | 地理   |          2 |
            +-----+--------+------------+
            
            insert into score(student_id,course_id,score) values # 12个学生,6门课程   
            (1,1,60),
            (1,2,59),
            (2,4,60),
            (2,5,59),
            (2,6,33),
            (3,1,59),
            (3,5,28),
            (4,4,100),
            (4,6,90),
            (5,4,88),
            (6,5,100),
            (6,6,60),
            (7,3,57),
            (7,5,60),
            (8,2,61),
            (8,4,59),
            (9,1,60),
            (9,2,61),
            (9,3,21),
            (10,5,68),
            (11,1,89),
            (12,3,100);
            +-----+------------+-----------+-------+
            | sid | student_id | course_id | score |
            +-----+------------+-----------+-------+
            |   1 |          1 |         1 |    60 |
            |   2 |          1 |         2 |    59 |
            |   3 |          2 |         4 |    60 |
            |   4 |          2 |         5 |    59 |
            |   5 |          2 |         6 |    33 |
            |   6 |          3 |         1 |    59 |
            |   7 |          3 |         5 |    28 |
            |   8 |          4 |         4 |   100 |
            |   9 |          4 |         6 |    90 |
            |  10 |          5 |         4 |    88 |
            |  11 |          6 |         5 |   100 |
            |  12 |          6 |         6 |    60 |
            |  13 |          7 |         3 |    57 |
            |  14 |          7 |         5 |    60 |
            |  15 |          8 |         2 |    61 |
            |  16 |          8 |         4 |    59 |
            |  17 |          9 |         1 |    60 |
            |  18 |          9 |         2 |    61 |
            |  19 |          9 |         3 |    21 |
            |  20 |         10 |         5 |    68 |
            |  21 |         11 |         1 |    89 |
            |  22 |         12 |         3 |   100 |
            +-----+------------+-----------+-------+ 
            
            insert into teach2cls(tid,cid) values # 4个老师 9个班级 
            (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),
            (4,1);
            +------+-----+-----+
            | tcid | tid | cid |
            +------+-----+-----+
            |    1 |   1 |   1 |
            |    2 |   1 |   2 |
            |    3 |   1 |   3 |
            |    4 |   1 |   7 |
            |    5 |   2 |   4 |
            |    6 |   2 |   8 |
            |    7 |   2 |   7 |
            |    8 |   2 |   5 |
            |    9 |   3 |   9 |
            |   10 |   3 |   3 |
            |   11 |   3 |   5 |
            |   12 |   3 |   2 |
            |   13 |   4 |   8 |
            |   14 |   4 |   4 |
            |   15 |   4 |   6 |
            |   16 |   4 |   1 |
            +------+-----+-----+
View Code

test_2

查询学生总人数;
            select count(sid) as count_student from student;
            +---------------+
            | count_student |
            +---------------+
            |            12 |
            +---------------+
View Code

test_3

查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
            select sid,sname 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 
                    group by score.student_id
                    having count(course_id) = 2
                    );
            +-----+-------+
            | sid | sname |
            +-----+-------+
            |   9 | Jone  |
            +-----+-------+
View Code

test_4

查询每个年级的班级数,取出班级数最多的前三个年级;
            select class_grade.gname from class_grade inner join 
                (select grade_id,count(cid) as count_course 
                from class 
                group by grade_id 
                order by count_course desc limit 3)
                as t1 
                on class_grade.gid = t1.grade_id;
            +-----------+
            | gname     |
            +-----------+
            | 一年级    |
            | 三年级    |
            | 二年级    |
            +-----------+
View Code

test_5

查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
            select student.sid,student.sname,t1.avg_score 
                from student 
                    inner join (
                    select 
                        student_id,avg(score) as avg_score 
                    from 
                        score 
                    group by 
                        student_id
                    having avg(score) in
                    (
                        (
                        select avg(score) as max_avg_score
                        from score
                        group by student_id
                        order by avg(score) desc
                        limit 1
                        ),
                        (
                        select avg(score) as min_avg_score
                        from score
                        group by student_id
                        order by avg(score) asc
                        limit 1
                        )
                    )
                )as t1 on student.sid = t1.student_id;
            +-----+-----------+-----------+
            | sid | sname     | avg_score |
            +-----+-----------+-----------+
            |   3 | Jack      |   43.5000 |
            |  12 | 高圆圆    |  100.0000 |
            +-----+-----------+-----------+
View Code

test_6

查询每个年级的学生人数;
            select t1.grade_id,count(t1.sid) as count_student
            from (
                    select student.sid,class.grade_id
                    from 
                        student,
                        class
                    where
                        student.class_id = class.cid
                )as t1
            group by 
                    t1.grade_id;
            +----------+---------------+
            | grade_id | count_student |
            +----------+---------------+
            |        1 |             5 |
            |        2 |             2 |
            |        3 |             3 |
            |        4 |             2 |
            +----------+---------------+
View Code

test_7

查询每位学生的学号,姓名,选课数,平均成绩;
            select student.sid,student.sname,t1.count_course,t1.avg_score
                from student 
                    left join(
                        select 
                            student_id,
                            count(course_id) as count_course,
                            avg(score) as avg_score
                        from
                            score
                        group by    
                            student_id
                        )as t1 on student.sid=t1.student_id;
            +-----+-----------+--------------+-----------+
            | sid | sname     | count_course | avg_score |
            +-----+-----------+--------------+-----------+
            |   1 | Jane      |            2 |   59.5000 |
            |   2 | Rose      |            3 |   50.6667 |
            |   3 | Jack      |            2 |   43.5000 |
            |   4 | Alice     |            2 |   95.0000 |
            |   5 | Alex      |            1 |   88.0000 |
            |   6 | Drigon    |            2 |   80.0000 |
            |   7 | Lily      |            2 |   58.5000 |
            |   8 | Lucy      |            2 |   60.0000 |
            |   9 | Jone      |            3 |   47.3333 |
            |  10 | 紫霞      |            1 |   68.0000 |
            |  11 | 张尊宝    |            1 |   89.0000 |
            |  12 | 高圆圆    |            1 |  100.0000 |
            +-----+-----------+--------------+-----------+
View Code

test_8

查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
            select student.sname,course.cname,t1.score
                from(
                    select
                        student_id,
                        course_id,
                        t1.score
                    from
                        score
                    where    
                        student_id=2 and score in
                            (
                            select    
                                max(score)
                            from
                                score
                            where
                                student_id=2
                                ),
                            (
                            select
                                min(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;
                    +-------+--------+-------+
                    | sname | cname  | score |
                    +-------+--------+-------+
                    | Rose  | 语文   |    60 |
                    | Rose  | 地理   |    33 |
                    +-------+--------+-------+
View Code

test_9

查询班级数小于5的年级id和年级名;
            方法1:
            select 
                gid,gname,count(cid) as count_cid 
            from class_grade 
                inner join class on gid=grade_id
            group 
                by gid 
            having 
                count(cid)<5;
            
            方法2:
            select
                gid,
                gname
            from
                class_grade
            where gid in (
                select
                    grade_id
                from
                    class
                group by
                    grade_id
                having
                    count(caption) < 5
            );
            +-----+-----------+-----------+
            | gid | gname     | count_cid |
            +-----+-----------+-----------+
            |   1 | 一年级    |         3 |
            |   2 | 二年级    |         2 |
            |   3 | 三年级    |         2 |
            |   4 | 四年级    |         2 |
            +-----+-----------+-----------+    
View Code

test_10

查询班级数小于5的年级id和年级名;
            方法1:
            select 
                gid,gname,count(cid) as count_cid 
            from class_grade 
                inner join class on gid=grade_id
            group 
                by gid 
            having 
                count(cid)<5;
            
            方法2:
            select
                gid,
                gname
            from
                class_grade
            where gid in (
                select
                    grade_id
                from
                    class
                group by
                    grade_id
                having
                    count(caption) < 5
            );
            +-----+-----------+-----------+
            | gid | gname     | count_cid |
            +-----+-----------+-----------+
            |   1 | 一年级    |         3 |
            |   2 | 二年级    |         2 |
            |   3 | 三年级    |         2 |
            |   4 | 四年级    |         2 |
            +-----+-----------+-----------+    
View Code

test_11

查询班级信息,包括班级id、班级名称、年级、年级级别
            select 
                class.cid,class.caption,class_grade.gid,
                case 
                    when class_grade.gid between 1 and 2 then \'\'
                    when class_grade.gid between 3 and 4 then \'\'
                    when class_grade.gid between 5 and 6 then \'\' else 0 end as \'年级级别\'
            from 
                class,
                class_grade
            where
                class.grade_id=class_grade.gid;
            +-----+--------------+-----+--------------+
            | cid | caption      | gid | 年级级别     |
            +-----+--------------+-----+--------------+
            |   1 | 一年一班     |   1 | 低           |
            |   2 | 一年二班     |   1 | 低           |
            |   3 | 一年三班     |   1 | 低           |
            |   6 | 三年一班     |   3 | 中           |
            |   7 | 三年二班     |   3 | 中           |
            |   4 | 二年一班     |   2 | 低           |
            |   5 | 二年二班     |   2 | 低           |
            |   8 | 四年一班     |   4 | 中           |
            |   9 | 四年二班     |   4 | 中           |
            +-----+--------------+-----+--------------+
View Code

test_12

查询学过“张三”老师2门课以上的同学的学号、姓名;
            select 
                student.sid,student.sname
            from 
                student
            where 
                sid in(
                    select 
                        student_id
                    from 
                        score
                    where
                        course_id in(
                            select    
                                course.cid
                            from
                                teacher,
                                course
                            where 
                                teacher.tid=course.teacher_id
                                and teacher.tname=\'张三\'
                            )
                    group by
                        student_id
                    having
                        count(course_id) >2
                );
                
            # Empty set (0.00 sec) 记录为空
View Code

test_13

查询教授课程超过2门的老师的id和姓名;
            select 
                teacher.tid,teacher.tname 
            from teacher
                where
                    tid in(
                        select 
                            teacher_id
                        from course
                        group by
                            teacher_id
                        having
                            count(cid)>2
                        );
            # Empty set (0.00 sec) 无记录
View Code

test_14

查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
            select
                sid,
                sname
            from
                student
            where
                sid in (
                    select distinct
                        student_id
                    from
                        score
                    where
                        course_id in (
                            1,
                            2
                        )
                );
            +-----+-----------+
            | sid | sname     |
            +-----+-----------+
            |   1 | Jane      |
            |   3 | Jack      |
            |   8 | Lucy      |
            |   9 | Jone      |
            |  11 | 张尊宝    |
            +-----+-----------+        
View Code

test_15