MySQL实验四数据库的查询_MySQL数据库查询(实验四)MySQL数据库查询
准备⼯作:脚本⽂件xkgl.sql下载:xkgl脚本.sql
1、执⾏脚本xkgl.sql (创建xkgl库、表及插⼊数据),观察有⽆错误,如有记录错误信息,并解决。
(1) 执⾏脚本
执⾏代码:
source+xkgl.sql⽂件所放的位置;(要⽤反斜杠);
(2)检查创建表的情况
代码:
show tables;
检查结果截图:
(3)检查七张表的数据记录数
代码:
select c1.class_urse_count,d.department_count,
(select count(*) class_count from class) c1,
(select count(*) course_count from course) c2,
(select count(*) department_count from department) d,
(select count(*) grade_count from grade) g,
(select count(*) schedule_count from schedule) s1,
(select count(*) student_count from student) s2,
(select count(*) teacher_count from teacher) t;
检查结果截图:
2、在xkgl数据库中进⾏如下的单表查询
(1)查询teacher表中所有教师的姓名和年龄:
代码:
select Teachername,year(now())-year(Brith) as age from teacher;查询结果截图:
(2)查询所有系的信息:
代码:
select * from department;
查询结果截图:
(3)查询学分值⼤于等于4的课程的名称:
代码:
select * from course where credit>=4;
查询结果截图:
(4)查询Cs010901班的⼥⽣信息:
代码:
select * from student where Sex='⼥' and ClassID='Cs010901';
检查结果截图:
(5)查询学⽣姓名中第2个字为“丽”的学⽣信息:
代码:
select * from student where StudentName like '_丽%';
检查结果截图:
(6)查询选修了Dp010001号课程的学⽣中成绩位于4到8名的学⽣学号和成绩:
代码:
select StudentID,Grade from grade where CourseID='Dp010001' order by Grade desc limit 3,5;
检查结果截图:
(7)查询schedule表中学年和课程号的组合,去掉重复:
代码:
select distinct SchoolYear,CourseID from schedule;
查询结果截图:
(8)查询年龄⼤于40岁的教授和副教授的姓名和性别:
代码:
select Teachername,Sex from teacher where Profession='教授' or Profession = '副教授' and year(now())-year(Brith)>40;查询结果截图:
(9)查询course表中前5⾏数据:
代码:
select * from course limit 0,5;
查询结果截图:
3、在xkgl数据库中进⾏如下的分类汇总。
(1)查询course表中的最⼤学分和最⼩学分的课程:
代码:
select max(credit)最⾼分,min(credit)最低分 from course;
查询结果截图:
(2)查询不同职称的教师⼈数:
代码:
select Profession,count(Profession) ⼈数 from teacher group by Profession;
查询结果截图:
(3)查询grade表中选修了3门以上课程的学⽣学号:
代码:
select StudentID,count(CourseID) 选修课程数 from grade group by StudentID having count(CourseID)>=3;查询结果截图:
(4)统计各个班的男⽣和⼥⽣⼈数:
代码:
select ClassID,count(Sex='男') 男⽣⼈数,count(Sex='⼥') ⼥⽣⼈数 from student group by ClassID;
查询结果截图:
4、在xkgl数据库中进⾏如下的连接查询。
(1)查询课程名及该课程的得分情况:
代码:
select CourseName,StudentID,Grade from grade join course on grade.CourseID=course.CourseID;
查询结果截图:
(2)查询教师姓名及其所教授的课程名:
代码:
select distinct teacher.Teachername,course.CourseName from teacher inner join schedule on
teacher.TeacherID=schedule.TeacherID inner join course on schedule.CourseID=course.CourseID;
查询结果截图:
(3)查询⽐‘刘芳’⽼师年龄⼩的教师信息:
mysql下载不了怎么办代码:
select t2.* from teacher t1,teacher t2 where t1.Brith
查询结果截图:
(4)查询全部教师的授课情况,包括没有授课的⽼师:
代码:
select t.Teachername,CourseID,ClassID from schedule sc right join teacher t on sc.TeacherID=t.TeacherID;查询结果截图:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。