实验五 SQL语言
一、目的与要求
1. 掌握SQL语言的查询功能; 2. 掌握SQL语言的数据操作功能;
3. 掌握对象资源管理器建立查询、索引和视图的方法;
二、实验准备
1. 了解SQL语言的查改增删四大操作的语法; 2. 了解查询、索引和视图的概念; 3. 了解各类常用函数的含义。
三、实验内容
(一)SQL查询功能
使用提供的studentdb数据库文件,先附加到目录树中,再完成下列题目,SQL命令请保存到脚本文件中。 1.基本查询
(1) 查询所有姓王的学生的姓名、学号和性别 Select St_Name ,St_Sex, St_ID From st_info
Where St_Name like '王%'
(2) 查询全体学生的情况,查询结构按班级降序排列,同一班级再按学号升序,并将结果存入新表new中 select * into new from st_info
order by Cl_Name desc,St_ID asc
(3) 对S_C_info表中选修了“体育”课的学生的平均成绩生成汇总行和明细行。(提示:用compute汇总计算) Select c_no,score From s_c_info
Where c_no=29000011 compute avg(score)
2.嵌套查询
(1) 查询其他班级中比“材料科学0601班”的学生年龄都大的学生姓名和年龄
Select St_Name ,Born_Date from st_info
where Cl_Name!='材料科学0601班' and Born_Date<(select Min(Born_Date) from st_info where Cl_Name='材料科学0601班')
(2) 用exists查询选修了“9710041”课程的学生姓名 select St_Name from st_info
where exists (select * from s_c_info where c_no = 9710041 and st_id=st_info.St_ID )
(3) 用in查询找出没有选修“9710041”课程的学生的姓名和所在班级。
select St_Name,Cl_Name from st_info
where st_ID not in (select st_id from s_c_info where c_no ='9710041')
(4) 查询选修了学号为“2001050105”的学生所选全部课程的学生姓名。 select St_Name
from st_info where St_ID in
(select distinct St_ID from s_c_info where not exists
(select * from s_c_info where st_id='2001050105' and not exists (select * from s_c_info where st_info.St_ID=s_c_info.st_id and c_no=any(select c_no from s_c_info where st_id='2001050105'))))
3.连接综合查询及其他
(1) 查询每个学生所选课程的最高成绩,要求列出学号,姓名,课程编号和分数。
select st_info.St_ID, St_Name,C_info.c_no,score
from st_info inner join s_c_info on st_info.St_ID=s_c_info.st_id inner join C_info on s_c_info.c_no=C_info.c_no
where score=(select max(s_c_info.score)from s_c_info where st_info.St_ID=s_c_info.st_id)
(2) 查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选修课程的学生总成绩为空。
select st_info.St_ID,St_Name,总成绩 from st_info
left outer join (select st_id,sum(score)as 总成绩 from s_c_info group by st_id)s_c_info on st_info.St_ID=s_c_info.st_id