首先做好前期准备工作:
- 创建名为mydb11的数据库,
- 使用数据库,
- 创建名为student和score的两张表,
- 分别向表中插入数据。
完成之后,开始进行以下操作:
-
分别查询 student 表和 score 表的所有记录。
mysql> select * from student; +-----+--------+------+------------+------------+--------------+ | id | name | sex | birth_year | department | address | +-----+--------+------+------------+------------+--------------+ | 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | | 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | | 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | | 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 | | 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | +-----+--------+------+------------+------------+--------------+ 6 rows in set (0.00 sec) mysql> select * from score; +----+--------+--------+-------+ | id | stu_id | c_name | grade | +----+--------+--------+-------+ | 1 | 901 | 计算机 | 98 | | 2 | 901 | 英语 | 80 | | 3 | 902 | 计算机 | 65 | | 4 | 902 | 中文 | 88 | | 5 | 903 | 中文 | 95 | | 6 | 904 | 计算机 | 70 | | 7 | 904 | 英语 | 92 | | 8 | 905 | 英语 | 94 | | 9 | 906 | 计算机 | 49 | | 10 | 906 | 英语 | 83 | +----+--------+--------+-------+ 10 rows in set (0.00 sec)
-
查询 student 表中第 2 条到第 5 条记录。
mysql> select * from student limit 1,4; +-----+--------+------+------------+------------+--------------+ | id | name | sex | birth_year | department | address | +-----+--------+------+------------+------------+--------------+ | 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | | 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | | 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 | +-----+--------+------+------------+------------+--------------+ 4 rows in set (0.00 sec)
-
从 student 表中查询计算机系和英语系的学生信息。
mysql> select * from student where department in ('计算机系', '英语系'); +-----+--------+------+------------+------------+--------------+ | id | name | sex | birth_year | department | address | +-----+--------+------+------------+------------+--------------+ | 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | | 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 | | 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | +-----+--------+------+------------+------------+--------------+ 4 rows in set (0.00 sec)
-
从 student 表中查询年龄小于 22 岁的学生信息。
mysql> select * from student where year(now())-birth_year<22; +-----+------+------+------------+------------+--------------+ | id | name | sex | birth_year | department | address | +-----+------+------+------------+------------+--------------+ | 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 | +-----+------+------+------------+------------+--------------+ 1 row in set (0.00 sec)
-
从 student 表中查询每个院系有多少人。
mysql> select department,count(1) from student group by department; +------------+----------+ | department | count(1) | +------------+----------+ | 计算机系 | 2 | | 中文系 | 2 | | 英语系 | 2 | +------------+----------+ 3 rows in set (0.00 sec)
-
从 score 表中查询每个科目的最高分。
mysql> select c_name,max(grade) from score group by c_name; +--------+------------+ | c_name | max(grade) | +--------+------------+ | 计算机 | 98 | | 英语 | 94 | | 中文 | 95 | +--------+------------+ 3 rows in set (0.00 sec)
-
查询李广昌的考试科目(c_name)和考试成绩(grade)。
mysql> select score.c_name,score.grade from score where stu_id= (select student.id from student where name='李广昌' ); +--------+-------+ | c_name | grade | +--------+-------+ | 计算机 | 70 | | 英语 | 92 | +--------+-------+ 2 rows in set (0.00 sec)
-
用连接的方式查询所有学生的信息和考试信息。
mysql> select s.*,c.* from student s join score c on s.id=c.stu_id; +-----+--------+------+------------+------------+--------------+----+--------+--------+-------+ | id | name | sex | birth_year | department | address | id | stu_id | c_name | grade | +-----+--------+------+------------+------------+--------------+----+--------+--------+-------+ | 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | 1 | 901 | 计算机 | 98 | | 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | 2 | 901 | 英语 | 80 | | 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | 3 | 902 | 计算机 | 65 | | 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | 4 | 902 | 中文 | 88 | | 903 | 张思维 | 女 | 2003 | 中文系 | 湖南省永州市 | 5 | 903 | 中文 | 95 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | 6 | 904 | 计算机 | 70 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | 7 | 904 | 英语 | 92 | | 905 | 王翰 | 男 | 2004 | 英语系 | 福建省厦门市 | 8 | 905 | 英语 | 94 | | 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | 9 | 906 | 计算机 | 49 | | 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | 10 | 906 | 英语 | 83 | +-----+--------+------+------------+------------+--------------+----+--------+--------+-------+ 10 rows in set (0.00 sec)
-
计算每个学生的总成绩。
mysql> select st.id,st.name,sum(sc.grade) 总成绩 from student st join score sc on st.id=sc.stu_id group by st.id,st.name; +-----+--------+--------+ | id | name | 总成绩 | +-----+--------+--------+ | 901 | 张三丰 | 178 | | 902 | 周全有 | 153 | | 903 | 张思维 | 95 | | 904 | 李广昌 | 162 | | 905 | 王翰 | 94 | | 906 | 王心凌 | 132 | +-----+--------+--------+ 6 rows in set (0.00 sec)
-
计算每个考试科目的平均成绩。
mysql> select c_name,avg(grade) 平均成绩 from score group by c_name; +--------+----------+ | c_name | 平均成绩 | +--------+----------+ | 计算机 | 70.5000 | | 英语 | 87.2500 | | 中文 | 91.5000 | +--------+----------+ 3 rows in set (0.00 sec)
-
查询计算机成绩低于 95 的学生信息。
mysql> select student.* from student join score on student.id=score.stu_id where score.c_name='计算机' and score.grade<95; +-----+--------+------+------------+------------+--------------+ | id | name | sex | birth_year | department | address | +-----+--------+------+------------+------------+--------------+ | 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | | 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | +-----+--------+------+------------+------------+--------------+ 3 rows in set (0.00 sec)
-
将计算机考试成绩按从高到低进行排序。
mysql> select student.*,score.grade from student join score on student.id=score.stu_id where score.c_name='计算机' order by score.grade desc; +-----+--------+------+------------+------------+--------------+-------+ | id | name | sex | birth_year | department | address | grade | +-----+--------+------+------------+------------+--------------+-------+ | 901 | 张三丰 | 男 | 2002 | 计算机系 | 北京市海淀区 | 98 | | 904 | 李广昌 | 男 | 1999 | 英语系 | 辽宁省阜新市 | 70 | | 902 | 周全有 | 男 | 2000 | 中文系 | 北京市昌平区 | 65 | | 906 | 王心凌 | 女 | 1998 | 计算机系 | 湖南省衡阳市 | 49 | +-----+--------+------+------------+------------+--------------+-------+ 4 rows in set (0.00 sec)
-
从 student 表和 score 表中查询出学生的学号,然后合并查询结果。
mysql> (select id from student) union (select stu_id from score); +-----+ | id | +-----+ | 901 | | 902 | | 903 | | 904 | | 905 | | 906 | +-----+ 6 rows in set (0.00 sec)
-
查询姓张或者姓王的同学的姓名、院系和考试科目及成绩。
mysql> select st.name, st.department, s.c_name, s.grade -> from student st -> join score s on st.id = s.stu_id -> where st.name like '张%' or st.name like '王%'; +--------+------------+--------+-------+ | name | department | c_name | grade | +--------+------------+--------+-------+ | 张三丰 | 计算机系 | 计算机 | 98 | | 张三丰 | 计算机系 | 英语 | 80 | | 张思维 | 中文系 | 中文 | 95 | | 王翰 | 英语系 | 英语 | 94 | | 王心凌 | 计算机系 | 计算机 | 49 | | 王心凌 | 计算机系 | 英语 | 83 | +--------+------------+--------+-------+ 6 rows in set (0.01 sec)
-
查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩。
mysql> select st.name, year(now())-st.birth_year 年龄, st.department, s.c_name, s.grade -> from student st -> join score s on st.id = s.stu_id -> where st.address like '湖南省%'; +--------+------+------------+--------+-------+ | name | 年龄 | department | c_name | grade | +--------+------+------------+--------+-------+ | 张思维 | 22 | 中文系 | 中文 | 95 | | 王心凌 | 27 | 计算机系 | 计算机 | 49 | | 王心凌 | 27 | 计算机系 | 英语 | 83 | +--------+------+------------+--------+-------+ 3 rows in set (0.00 sec)
总结
本次 MySQL 作业围绕数据库操作展开,涵盖建库建表、数据插入与查询等任务。首先创建 mydb11_stu 数据库,并在其中构建 student 和 score 表,明确各表字段及约束。接着向两表插入具体学生信息和成绩记录。最后进行多维度查询,含单表查询、多表连接查询,还运用聚合函数实现统计计算,全面考查了 MySQL 基础操作与数据处理能力。