美文网首页
MySQL版-50道数据库练习题

MySQL版-50道数据库练习题

作者: _NewMoon | 来源:发表于2020-09-23 10:12 被阅读0次

    原博客戳这里:点我跳转
    (持续更新)

    环境

    • 1.MySQL 8.0
      -2. navicat for Mysql

    建表

    # 建表
    # 学生表
    CREATE TABLE `Student`(
        `s_id` VARCHAR(20),
        `s_name` VARCHAR(20) NOT NULL DEFAULT '',
        `s_birth` VARCHAR(20) NOT NULL DEFAULT '',
        `s_sex` VARCHAR(10) NOT NULL DEFAULT '',
        PRIMARY KEY(`s_id`)
    );
    # 课程表
    CREATE TABLE `Course`(
        `c_id`  VARCHAR(20),
        `c_name` VARCHAR(20) NOT NULL DEFAULT '',
        `t_id` VARCHAR(20) NOT NULL,
        PRIMARY KEY(`c_id`)
    );
    # 师表
    CREATE TABLE `Teacher`(
        `t_id` VARCHAR(20),
        `t_name` VARCHAR(20) NOT NULL DEFAULT '',
        PRIMARY KEY(`t_id`)
    );
    # 成绩表
    CREATE TABLE `Score`(
        `s_id` VARCHAR(20),
        `c_id`  VARCHAR(20),
        `s_score` INT(3),
        PRIMARY KEY(`s_id`,`c_id`)
    );
    # 插入学生表测试数据
    insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
    insert into Student values('02' , '钱电' , '1990-12-21' , '男');
    insert into Student values('03' , '孙风' , '1990-05-20' , '男');
    insert into Student values('04' , '李云' , '1990-08-06' , '男');
    insert into Student values('05' , '周梅' , '1991-12-01' , '女');
    insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
    insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
    insert into Student values('08' , '王菊' , '1990-01-20' , '女');
    #课程表测试数据
    insert into Course values('01' , '语文' , '02');
    insert into Course values('02' , '数学' , '01');
    insert into Course values('03' , '英语' , '03');
    
    # 教师表测试数据
    insert into Teacher values('01' , '张三');
    insert into Teacher values('02' , '李四');
    insert into Teacher values('03' , '王五');
    
    #成绩表测试数据
    insert into Score values('01' , '01' , 80);
    insert into Score values('01' , '02' , 90);
    insert into Score values('01' , '03' , 99);
    insert into Score values('02' , '01' , 70);
    insert into Score values('02' , '02' , 60);
    insert into Score values('02' , '03' , 80);
    insert into Score values('03' , '01' , 80);
    insert into Score values('03' , '02' , 80);
    insert into Score values('03' , '03' , 80);
    insert into Score values('04' , '01' , 50);
    insert into Score values('04' , '02' , 30);
    insert into Score values('04' , '03' , 20);
    insert into Score values('05' , '01' , 76);
    insert into Score values('05' , '02' , 87);
    insert into Score values('06' , '01' , 31);
    insert into Score values('06' , '03' , 34);
    insert into Score values('07' , '02' , 89);
    insert into Score values('07' , '03' , 98);
    
    

    对应的每个图如下:

    1.course

    course

    2.score

    score

    3.student

    student

    4.teacher

    teacher

    习题

    1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数

    SELECT a.*, b.s_score AS 01_score, c.s_score AS 02_score
    FROM student a, score b, score c
    WHERE a.s_id = b.s_id 
                AND a.s_id = c.s_id
                AND b.c_id = '01'
                AND c.c_id = '02'
                AND b.s_score > c.s_score;
    

    2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数

    SELECT A.*, S1.s_score  AS 01_SCORE,S2.s_score AS 02_SCORE
    FROM STUDENT A 
    JOIN SCORE S1
    ON A.s_id = S1.s_id AND S1.c_id = '01'
    JOIN SCORE S2 
    ON A.s_id = S2.s_id AND S2.c_id = '02'
    WHERE S1.s_score < S2.s_score
    

    3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

    SELECT S.s_id, S.s_name, AVG(A.s_score) AS AVG_SCORE
    FROM STUDENT S 
    JOIN SCORE A ON S.s_id = A.s_id
    GROUP BY S.s_id
    HAVING AVG_SCORE >= 60
    

    5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

    SELECT S.s_id,S.s_name, COUNT(SC.c_id) AS courses, SUM(SC.s_score) AS total
    FROM STUDENT S 
    LEFT JOIN SCORE SC #这里需要左连接,因为不一定所有学生都选课了
    ON S.s_id = SC.s_id
    GROUP BY S.s_id , S.s_name
    

    6.查询"李"姓老师的数量

    SELECT COUNT(t_id) 
    FROM TEACHER
    WHERE t_name like '李%'
    

    相关文章

      网友评论

          本文标题:MySQL版-50道数据库练习题

          本文链接:https://www.haomeiwen.com/subject/tbunyktx.html