mysql作业记录
Posted kris12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql作业记录相关的知识,希望对你有一定的参考价值。
一、表关系
请创建如下表,并创建相关约束
班级表:class | 学生表:student | ||||||
cid | caption | grade_id | sid | sname | gender | class_id | |
1 | 一年一班 | 1 | 1 | 乔丹 | 女 | 1 | |
2 | 二年一班 | 2 | 2 | 艾弗森 | 女 | 1 | |
3 | 三年二班 | 3 | 3 | 科比 | 男 | 2 | |
老师表:teacher | 课程表:course | ||||||
tid | tname | cid | cname | teacher_id | |||
1 | 张三 | 1 | 生物 | 1 | |||
2 | 李四 | 2 | 体育 | 1 | |||
3 | 王五 | 3 | 物理 | 2 | |||
成绩表:score | 年级表:class_grade | ||||||
sid | student_id | course_id | score | gid | gname | ||
1 | 1 | 1 | 60 | 1 | 一年级 | ||
2 | 1 | 2 | 59 | 2 | 二年级 | ||
3 | 2 | 2 | 99 | 3 | 三年级 | ||
班级任职表:teach2cls | |||||||
tcid | tid | cid | |||||
1 | 1 | 1 | |||||
2 | 1 | 2 | |||||
3 | 2 | 1 | |||||
4 | 3 | 2 |
class_grade 一对多 >> class
class 一对多 >> student
teacher 一对多 >> course
student 一对多 >> score
course 一对多 >> score
teacher 多对多 >> class
一对多是在多的表中建立关联字段;一对一是在任意一张表中建立关联字段+unique;多对多是加id和两个关联字段,第三张表。
可参考: http://www.cnblogs.com/wupeiqi/articles/5748496.html
C:\\Users\\Administrator>mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 1 Server version: 5.6.39 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> create database db charset utf8; Query OK, 1 row affected (0.01 sec) mysql> use db; Database changed mysql> create table class_grade( -> gid int primary key auto_increment, -> gname varchar(16) not null unique); Query OK, 0 rows affected (1.14 sec) mysql> create table class( -> cid int primary key auto_increment, -> caption varchar(16) not null, -> grade_id int not null, -> foreign key(grade_id) references class_grade(gid)); #class_grade表与class表是一对多的关系;在多的表中建立关联关系; Query OK, 0 rows affected (0.95 sec) mysql> create table student( -> sid int primary key auto_increment, -> sname varchar(16) not null, -> sex enum(\'女\',\'男\'), -> class_id int not null, -> foreign key(class_id) references class(cid)); #class和student表也是一对多的关系,在多的表中建立关联字段; Query OK, 0 rows affected (0.64 sec) mysql> create table teacher( -> tid int primary key auto_increment, -> tname varchar(16) not null); Query OK, 0 rows affected (0.59 sec) mysql> create table course( -> cid int primary key auto_increment, -> cname varchar(16) not null, -> teacher_id int not null, -> foreign key(teacher_id) references teacher(tid)); #teacher和course是一对多关系,在多的表中建立关联字段; Query OK, 0 rows affected (0.53 sec) mysql> 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) #student和score是一对多的关系; -> on delete cascade -> on update cascade, -> foreign key(course_id) references course(cid) #course和score也是一对多的关系; -> on delete cascade -> on update cascade -> ); Query OK, 0 rows affected (1.11 sec) mysql> 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) #teacher和class是多对多关系,两个互相一对多; -> on delete cascade -> on update cascade, -> foreign key(cid) references class(cid) -> on delete cascade -> on update cascade -> ); Query OK, 0 rows affected (0.87 sec) mysql>
mysql> desc teach2cls; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | tcid | int(11) | NO | UNI | NULL | auto_increment | | tid | int(11) | NO | PRI | NULL | | | cid | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+----------------+ 3 rows in set (0.10 sec) mysql> desc score; +------------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------+------+-----+---------+----------------+ | sid | int(11) | NO | UNI | NULL | auto_increment | | student_id | int(11) | NO | PRI | NULL | | | course_id | int(11) | NO | PRI | NULL | | | score | int(11) | NO | | NULL | | +------------+---------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql> desc course; +------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+----------------+ | cid | int(11) | NO | PRI | NULL | auto_increment | | cname | varchar(16) | NO | | NULL | | | teacher_id | int(11) | NO | MUL | NULL | | +------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.02 sec) mysql> desc teacher; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | tid | int(11) | NO | PRI | NULL | auto_increment | | tname | varchar(16) | NO | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) mysql> desc student; +----------+-------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------------+------+-----+---------+----------------+ | sid | int(11) | NO | PRI | NULL | auto_increment | | sname | varchar(16) | NO | | NULL | | | sex | enum(\'女\',\'男\') | YES | | NULL | | | class_id | int(11) | NO | MUL | NULL | | +----------+-------------------+------+-----+---------+----------------+ 4 rows in set (0.08 sec) mysql> desc class; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | cid | int(11) | NO | PRI | NULL | auto_increment | | caption | varchar(16) | NO | | NULL | | | grade_id | int(11) | NO | MUL | NULL | | +----------+-------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) mysql> desc class_grade; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | gid | int(11) | NO | PRI | NULL | auto_increment | | gname | varchar(16) | NO | UNI | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)
二、插入数据
(根据下面题目可自行插入数据,建议数据量应该大一些,使用navicat插入数据简便快捷,但要会使用在cmd终端下的操作)
mysql> select * from class; +-----+-----------------+----------+ | cid | caption | grade_id | +-----+-----------------+----------+ | 1 | 一年级一班 | 1 | | 2 | 一年级二班 | 1 | | 3 | 一年级三班 | 1 | | 4 | 二年级一班 | 2 | | 5 | 二年级二班 | 2 | | 6 | 二年级三班 | 2 | | 7 | 三年级一班 | 3 | | 8 | 三年级二班 | 3 | | 9 | 三年级三班 | 3 | +-----+-----------------+----------+ 9 rows in set (0.00 sec) mysql> select * from class_grade; +-----+-----------+ | gid | gname | +-----+-----------+ | 1 | 一年级 | | 3 | 三年级 | | 2 | 二年级 | | 4 | 四年级 | +-----+-----------+ 4 rows in set (0.00 sec) mysql> select * from course; +-----+--------+------------+ | cid | cname | teacher_id | +-----+--------+------------+ | 1 | 生物 | 1 | | 2 | 体育 | 1 | | 3 | 物理 | 2 | | 4 | 化学 | 3 | | 5 | 英语 | 3 | | 6 | python | 4 | | 7 | linux | 5 | +-----+--------+------------+ 7 rows in set (0.00 sec) mysql> select * from student; +-----+-----------+------+----------+ | sid | sname | sex | class_id | +-----+-----------+------+----------+ | 1 | 乔丹 | 女 | 1 | | 2 | 艾弗森 | 女 | 1 | | 3 | 科比 | 男 | 2 | | 4 | alex | 女 | 2 | | 5 | egon | 男 | 3 | | 6 | egon2 | 女 | 3 | | 7 | kris | 男 | 4 | | 8 | wxx | 女 | 5 | | 9 | 杜拉拉 | 女 | 6 | | 10 | 姗姗 | 女 | 7 | | 11 | 丹丹 | 男 | 8 | | 12 | simith | 男 | 9 | +-----+-----------+------+----------+ 12 rows in set (0.00 sec) mysql> select * from teacher; +-----+-----------+ | tid | tname | +-----+-----------+ | 1 | 张三 | | 2 | 李四 | | 3 | 王五 | | 4 | 老男孩 | | 5 | 小女孩 | +-----+-----------+ 5 rows in set (0.00 sec) mysql> select * from score; +-----+------------+-----------+-------+ | sid | student_id | course_id | score | +-----+------------+-----------+-------+ | 1 | 1 | 1 | 60 | | 2 | 1 | 2 | 59 | | 3 | 2 | 2 | 99 | | 4 | 3 | 3 | 88 | | 5 | 3 | 4 | 76 | | 6 | 4 | 5 | 34 | | 7 | 5 | 6 | 68 | | 8 | 6 | 6 | 93 | | 9 | 7 | 7 | 77 | | 10 | 8 | 6 | 86 | | 11 | 9 | 3 | 90 | | 12 | 10 | 4 | 100 | | 13 | 11 | 5 | 52 | | 14 | 12 | 7 | 27 | +-----+------------+-----------+-------+ 14 rows in set (0.00 sec) mysql> select * from teach2cls; +------+-----+-----+ | tcid | tid | cid | +------+-----+-----+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 2 | 1 | | 4 | 3 | 2 | | 5 | 4 | 3 | | 6 | 4 | 4 | | 7 | 5 | 6 | | 8 | 5 | 5 | | 9 | 3 | 7 | | 10 | 2 | 8 | | 11 | 4 | 9 | +------+-----+-----+ 11 rows in set (0.00 sec)
class(cid caption grade_id); class_grade(gid gname)
student(sid sname sex class_id);teacher(tid tname)
course(cid cname teacher_id); score(sid student_id course_id score)
teach2cls(tcid tid cid)
三、操作表
1、自行创建测试数据;见上
2、查询学生总人数;
mysql> select count(sid) as count_student from student; +---------------+ | count_student | +---------------+ | 12 | +---------------+ 1 row in set (0.08 sec)
3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
查询 student的sid和sname
两个条件:从course表中查出“生物”和“物理”课程;从score表中查出score > 60的
mysql> select sid,sname from student -> where sid in( #带in关键字的子查询 -> SELECT score.student_id from score inner join course on score.course_id = course.cid -> where course.cname in (\'生物\', \'物理\') and score.score > 60 -> ); +-----+-----------+ | sid | sname | +-----+-----------+ | 1 | 乔丹 | | 3 | 科比 | | 9 | 杜拉拉 | +-----+-----------+
4、查询每个年级的班级数,取出班级数最多的前三个年级;
取出年级
内层select语句不能带有limit语句
mysql> select grade_id,count(grade_id) from class; +----------+-----------------+ | grade_id | count(grade_id) | +----------+-----------------+ | 1 | 9 | +----------+-----------------+ 1 row in set (0.00 sec) mysql> select grade_id,count(cid) as count_cid from class -> group by grade_id; +----------+-----------+ | grade_id | count_cid | +----------+-----------+ | 1 | 3 | | 2 | 3 | | 3 | 3 | +----------+-----------+ 3 rows in set (0.08 sec) mysql> select grade_id,count(cid) as count_cid from class -> group by grade_id -> order by count_cid desc -> limit 3; +----------+-----------+ | grade_id | count_cid | +----------+-----------+ | 1 | 3 | | 2 | 3 | | 3 | 3 | +----------+-----------+ 3 rows in set (0.07 sec)
mysql> select class_grade.gname from class_grade inner join ( -> select grade_id,count(cid) as count_cid from class -> group by grade_id -> order by count_cid desc -> limit 3) -> as t1 -> on class_grade.gid = t1.grade_id; +-----------+ | gname | +-----------+ | 一年级 | | 三年级 | | 二年级 | +-----------+ 3 rows in set (0.00 sec)
5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
获取平均成绩最高
mysql> 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 | +-----+--------+-----------+ | 10 | 姗姗 | 100.0000 | | 12 | simith | 27.0000 | +-----+--------+-----------+ 2 rows in set (0.05 sec)
6、查询每个年级的学生人数;
student的class_id和class的cid
mysql> select student.sid, class.grade_id from student,class -> where student.class_id = class.cid; +-----+----------+ | sid | grade_id | +-----+----------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 1 | | 5 | 1 | | 6 | 1 | | 7 | 2 | | 8 | 2 | | 9 | 2 | | 10 | 3 | | 11 | 3 | | 12 | 3 | +-----+----------+ 12 rows in set (0.00 sec) mysql> select t1.grade_id, count(t1.sid) as student_count 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 | student_count | +----------+---------------+ | 1 | 6 | | 2 | 3 | | 3 | 3 | +----------+---------------+ 3 rows in set (0.00 sec)
7、查询每位学生的学号,姓名,选课数,平均成绩;
mysql> select student_id, count(course_id) as count_course, -> avg(score) as avg_score from score -> group by student_id; +------------+--------------+-----------+ | student_id | count_course | avg_score | +------------+--------------+-----------+ | 1 | 3 | 65.0000 | | 2 | 1 | 99.0000 | | 3 | 1 | 88.0000 | | 4 | 1 | 34.0000 | | 5 | 1 | 68.0000 | | 6 | 1 | 93.0000 | | 7 | 1 | 77.0000 | | 8 | 1 | 86.0000 | | 9 | 1 | 90.0000 | | 10 | 1 | 100.0000 | | 11 | 1 | 52.0000 | | 12 | 1 | 27.0000 | +------------+--------------+-----------+ 12 rows in set (0.00 sec) mysql> 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 | 乔丹 | 3 | 65.0000 | | 2 | 艾弗森 | 1 | 99.0000 | | 3 | 科比 | 1 | 88.0000 | | 4 | alex | 1 | 34.0000 | | 5 | egon | 1 | 68.0000 | | 6 | egon2 | 1 | 93.0000 | | 7 | kris | 1 | 77.0000 | | 8 | wxx | 1 | 86.0000 | | 9 | 杜拉拉 | 1 | 90.0000 | | 10 | 姗姗 | 1 | 100.0000 | | 11 | 丹丹 | 1 | 52.0000 | | 12 | simith | 1 | 27.0000 | +-----+-----------+--------------+-----------+ 12 rows in set (0.00 sec)
8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
mysql> select student_id, course_id, 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)); +------------+-----------+-------+ | student_id | course_id | score | +------------+-----------+-------+ | 2 | 2 | 99 | +------------+-----------+-------+ 1 row in set (0.03 sec) mysql> select student.sname,course.cname,t1.score from( -> select student_id, course_id, 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; +-----------+-以上是关于mysql作业记录的主要内容,如果未能解决你的问题,请参考以下文章
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段