美文网首页
数据库作业

数据库作业

作者: HavenYoung | 来源:发表于2018-09-04 20:09 被阅读0次

    一、已知有如下表,请用sql语句在mysql里建立相应的表

    表1 学生表(student)

    表2 课程表(course)

    表3 成绩表(grade)

    表4 教师表(teacher)

    表5 等级表(rank)

    二、将如下数据插入到表里
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' , '1977-09-01',95033);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,’1975-10-02’,95031);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,’1976-01-23’, 95033);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,’1976-02-20’,95033);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,’1975-02-10’,95031);
    INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,’1974-06-03’,95031);

    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论', '825');
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' , '804');
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' , '856');
    INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' , '100');

    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES ('103','3-245',86);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
    INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);

    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
    VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
    VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
    VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
    INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
    VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');

    insert into rank(down,up,rank) values(90,100,'A');
    insert into rank(down,up,rank) values(80,89,'B');
    insert into rank(down,up,rank) values(70,79,'C');
    insert into rank(down,up,rank) values(60,69,'D');
    insert into rank(down,up,rank) values(0,59,'E');

    三、sql语句练习
    1、 查询Student表中的所有记录的Sname、Ssex和Class列。
    2、 查询教师所有的单位即不重复的Depart列。
    3、 查询Student表的所有记录。
    4、 查询Grade表中成绩在60到80之间的所有记录。
    5、 查询Grade表中成绩为85,86或88的记录。
    6、 查询Student表中“95031”班或性别为“女”的同学记录。
    7、 以Class降序查询Student表的所有记录。
    8、 以Cno升序、Degree降序查询Grade表的所有记录。
    9、 查询“95031”班的学生人数。
    10、查询Grade表中的最高分的学生学号和课程号。
    11、查询‘3-105’号课程的平均分。
    12、查询Grade表中至少有5名学生选修的并以3开头的课程的平均分数。
    13、查询最低分大于70,最高分小于90的Sno列。
    14、查询所有学生的Sname、Cno和Degree列。
    15、查询所有学生的Sno、Cname和Degree列。
    16、查询所有学生的Sname、Cname和Degree列。
    17、查询“95033”班所选课程的平均分。
    mysql> select sname,ssex,class from student;
    +--------+------+-------+
    | sname | ssex | class |
    +--------+------+-------+
    | 李军 | 男 | 95033 |
    | 陆君 | 男 | 95031 |
    | 匡明 | 男 | 95031 |
    | 王丽 | 女 | 95033 |
    | 曾华 | 男 | 95033 |
    | 王芳 | 女 | 95031 |
    +--------+------+-------+
    6 rows in set (0.00 sec)

    mysql> select depart from teacher group by depart;
    +-----------------+
    | depart |
    +-----------------+
    | 电子工程系 |
    | 计算机系 |
    +-----------------+
    2 rows in set (0.00 sec)

    mysql> select * from student;
    +-----+--------+------+---------------------+-------+
    | sno | sname | ssex | sbirthday | class |
    +-----+--------+------+---------------------+-------+
    | 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
    | 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
    | 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
    | 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
    | 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
    | 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
    +-----+--------+------+---------------------+-------+
    6 rows in set (0.00 sec)

    mysql> select * from student where degree=85 or degree=86 or degree=88;
    ERROR 1054 (42S22): Unknown column 'degree' in 'where clause'
    mysql> select * from grade where degree=85 or degree=86 or degree=88;
    +-----+-------+--------+
    | sno | cno | degree |
    +-----+-------+--------+
    | 101 | 6-166 | 85 |
    | 103 | 3-245 | 86 |
    | 105 | 3-105 | 88 |
    +-----+-------+--------+
    3 rows in set (0.00 sec)

    mysql> select * from grade where degree between 60 and 80;
    +-----+-------+--------+
    | sno | cno | degree |
    +-----+-------+--------+
    | 101 | 3-105 | 64 |
    | 105 | 3-245 | 75 |
    | 107 | 6-106 | 79 |
    | 108 | 3-105 | 78 |
    | 109 | 3-105 | 76 |
    | 109 | 3-245 | 68 |
    +-----+-------+--------+
    6 rows in set (0.00 sec)

    mysql> select * from student where class=95031 or ssex='女';
    +-----+--------+------+---------------------+-------+
    | sno | sname | ssex | sbirthday | class |
    +-----+--------+------+---------------------+-------+
    | 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
    | 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
    | 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
    | 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
    +-----+--------+------+---------------------+-------+
    4 rows in set (0.00 sec)

    mysql> select * from student order by class desc;
    +-----+--------+------+---------------------+-------+
    | sno | sname | ssex | sbirthday | class |
    +-----+--------+------+---------------------+-------+
    | 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
    | 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
    | 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
    | 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
    | 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
    | 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
    +-----+--------+------+---------------------+-------+
    6 rows in set (0.00 sec)

    mysql> select * from grade order by cno,degree desc;
    +-----+-------+--------+
    | sno | cno | degree |
    +-----+-------+--------+
    | 103 | 3-105 | 92 |
    | 107 | 3-105 | 91 |
    | 105 | 3-105 | 88 |
    | 108 | 3-105 | 78 |
    | 109 | 3-105 | 76 |
    | 101 | 3-105 | 64 |
    | 103 | 3-245 | 86 |
    | 105 | 3-245 | 75 |
    | 109 | 3-245 | 68 |
    | 107 | 6-106 | 79 |
    | 101 | 6-166 | 85 |
    | 108 | 6-166 | 81 |
    +-----+-------+--------+
    12 rows in set (0.00 sec)

    mysql> select count() from student where class=95031;
    +----------+
    | count(
    ) |
    +----------+
    | 3 |
    +----------+
    1 row in set (0.00 sec)

    mysql> select sno,cno from grade order by degree limit 1;
    +-----+-------+
    | sno | cno |
    +-----+-------+
    | 101 | 3-105 |
    +-----+-------+
    1 row in set (0.00 sec)

    mysql> select sno,cno from grade order by degree desc limit 1;
    +-----+-------+
    | sno | cno |
    +-----+-------+
    | 103 | 3-105 |
    +-----+-------+
    1 row in set (0.00 sec)

    mysql> select avg(degree) from grade;
    +-------------+
    | avg(degree) |
    +-------------+
    | 80.2500 |
    +-------------+
    1 row in set (0.00 sec)

    mysql> select avg(degree) from grade where cno='3-105';
    +-------------+
    | avg(degree) |
    +-------------+
    | 81.5000 |
    +-------------+
    1 row in set (0.00 sec)

    相关文章

      网友评论

          本文标题:数据库作业

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