| 实验名称 | 实验6 | 实验地点 | 8-318 | ||
| 实验类型 | 设计 | 实验学时 | 1 | 实验日期 | 2018-6-14 |
| ★撰写注意:版面格式已设置好(不得更改),填入内容即可。 | |||||
| 一、实验目的 1. 掌握系统数据类型的特点和功能。 2. 掌握创建、修改表结构的方法。 3. 掌握插入、更新和删除表数据的方法。 | |||||
| 二、实验内容 1.查询所有班级的期末成绩平均分,并按照平均分降序排序。 2.查询教师基本信息和教授课程信息,其中包括未分配课程的教师信息。 3.查询160501班级中选修了“韩晋升”老师讲授的课程的学生学号、姓名、课程号和期末成绩。 4.查询每门课程的课程号、课程名和选修该课程的学生人数,并按所选人数升序排序。 5.查询两门及以上课程的期末成绩超过80分的学生姓名及平均成绩。 6.查询入学考试成绩最高的学生学号、姓名和入学成绩。 7.查询同时教授c05127号和c05109号课程的教师信息。 8.查询至少选修了姓名为“韩吟秋”的学生所选修课程中一门课程的学生学号和姓名。 9.查询所有教授c05127号课程的教师信息。 10.查询没有被任何学生选修的课程编号、课程名称和学分。 11.查询“C语言”课程期末成绩比“电子技术”课程期末成绩高的所有学生的学号和姓名。 12.查询所有班级期末平均成绩的最高分,并将其赋值给变量,通过PRINT语句输出。 13.使用游标输出学生姓名、选修课程名称和期末考试成绩。 14.使用游标统计每个学院教师所开设课程的选修率。 15.使用游标计算学生期末成绩的等级,并更新level列。 | |||||
| 三、实验环境 1. 操作系统:Windows XP 2. 开发软件:SQL Server 2008 | |||||
| 四、提交文档 提交本实验报告(电子版),文件名命名:学号 姓名《实验X:XXXXXXX》.doc 教师将批阅后(有分数)的全体学生实验报告刻入一张光盘存档,保证光盘可读。 | |||||
| 五、附:源代码 1. select studentno,AVG(final) as 平均分 from score group by studentno order by AVG(final) 2. select * from teacher select * from student select * from course insert into course(courseno,cname,ctype,period,credit) values('c05103','高等数学','必修',,4.0)
select * from score insert into score(studentno,courseno,usually,final) values('16122210009','c05103',87.00,82.00) insert into teacher(teacherno,tname,major,prof,department) values('t05001','韩晋升','软件工程','教授','计算机学院') select * from class insert into class(classno,classname,department,monitor) values('160501','计算机','计算机学院','张三') select * from teach_class insert into teach_class(teacherno,classno,courseno) values('t05001','160501','c05103') select * from teacher select * from course select * from score select classno,AVG(final) as 平均分 from student join score on student.studentno=score.studentno group by classno order by AVG(final) desc select teacher.*,cname from teacher left join teach_class on teacher.teacherno=teach_class.teacherno left join course on teach_class.classno=course.courseno 3. select student.studentno,sname,cname,final from student join score on student.studentno=score.studentno join course on course.courseno=score.courseno where score.courseno in ( select courseno from teach_class join teacher on teach_class.teacherno=teacher.teacherno where tname='韩晋升' ) and classno='090501' 4. select course.courseno,cname,COUNT(studentno) from score join course on score.courseno=course.courseno group by course.courseno,cname order by COUNT(studentno) desc 5. select sname,AVG(final) from score join student on score.studentno=student.studentno where final>=80 group by student.studentno,sname having COUNT(courseno)>=2
6. select studentno,sname,point from student where studentno=(select top 1 studentno from student order by point) 7. select teacher.teacherno,tname,major ,prof,department from teacher join teach_class on teacher.teacherno=teach_class.teacherno where courseno='c05127' 8. select distinct student.studentno,sname from score join student on score.studentno=student.studentno where courseno in ( select courseno from score join student on score.studentno=student.studentno where sname='韩吟秋' ) and sname!='韩吟秋' 9. select * from teacher where teacherno in ( select teacherno from teach_class where courseno='c05127' ) 10. select courseno,cname,credit from course where not exists( select * from score where score.courseno=course.courseno) 11. select student.studentno,sname from score sc1 join student on (sc1.studentno=student.studentno) join course c1 on(sc1.courseno=c1.courseno) where c1.cname='c语言' and exists ( select * from score sc2 join course c2 on(sc2.courseno=c2.courseno) where c2.cname='电子技术' and sc1.studentno=sc2.studentno and sc1.final>sc2.final ) 12. declare @max numeric(6,2) select @max=MAX(平均分) from ( select classno as 班级号,AVG(final) as 平均分 from score join student on (score.studentno=student.studentno) join course on (course.courseno=score.courseno) where final is not null group by classno) t print '所有班级期末平均成绩的最高分:'+cast(@max as varchar(6)) 13. declare @sname nchar(8),@cname nchar(10),@final numeric(6,2) declare sc_cursor cursor for select sname,cname,final from score join student on(score.studentno=student.studentno) join course on(score.courseno=course.courseno) open sc_cursor fetch next from sc_cursor into @sname,@cname,@final print '学生姓名 课程名称 期末成绩' print '----------------------------------' while @@FETCH_STATUS=0 begin print @sname+@cname+cast(@final as nchar(6)) fetch next from sc_cursor into @sname,@cname,@final end close sc_cursor deallocate sc_cursor 14. declare @department nchar(30),@num int,@avg float declare cur cursor static for select department,count(*) as '选修课数' from class where class.classno in( select student.classno from student group by classno ) group by department open cur fetch cur into @department,@num set @avg=@num/(select COUNT(*) from class where department=@department) print @department print @avg while @@FETCH_STATUS=0 begin fetch next from cur into @department,@num set @avg=@num/(select COUNT(*) from class where department=@department) print @department print @avg end close cur deallocate cur 15. declare @sname nchar(30),@cname nchar(30),@final float declare stu cursor static for select sname,final,cname from student,score,course where student.studentno=score.studentno and course.courseno=score.courseno open stu fetch stu into @sname,@final,@cname if @final>=90 print N'优'+@sname+@cname else if @final>=80 and @final<90 print N'良'+@sname+@cname else if @final>=70 and @final<80 print N'中'+@sname+@cname else if @final>=60 and @final<70 print N'及'+@sname+@cname else if @final<60 print N'差'+@sname+@cname while @@FETCH_STATUS=0 begin fetch next from stu into @sname,@final,@cname if @final>=90 print N'优'+@sname+@cname else if @final>=80 and @final<90 print N'良'+@sname+@cname else if @final>=70 and @final<80 print N'中'+@sname+@cname else if @final>=60 and @final<70 print N'及'+@sname+@cname else if @final<60 print N'差'+@sname+@cname end close stu deallocate stu | |||||