目录

一﹑实验目的

二﹑实验平台

三﹑实验内容和步骤

四﹑命令(代码)清单

五﹑调试和测试清单


一﹑实验目的

  1. 掌握使用Transact-SQL的SELECT语句进行基本查询的方法;
  2. 掌握使用SELECT语句进行条件查询的方法;
  3. 掌握SELECT语句的GROUP BY、ORDER BY以及UNION子句的作用和使用方法。
  4. 掌握嵌套查询的方法;
  5. 掌握连接查询的操作方法。

二﹑实验平台

操作系统:Windows xp或Win7

DBMS:SQL Server 2005或SQL Server 2008

三﹑实验内容和步骤

(1)在studentdb数据库中,运行下列SQL语句将输出什么?

① select count(*) from grade

② select substring(姓名,1,2) from student_info

③ select sqrt(分数) from grade where 分数>=85

④ select year(getdate()),month(getdate()),day(getdate())

(2)在studentdb数据库中使用select语句进行基本查询。

①在student_info表中,查询每个学生的学号、姓名和出生日期信息。

②查询学号为’0002’的学生的姓名和家庭住址。

③找出所有男同学的学号和姓名。

(3)使用select语句进行条件查询。

①在grade表中查找分数在70-90范围内的学生的学号和分数。

②在grade表中查询课程编号为0003的学生的平均分。

③在grade表中查询学习各门课程的人数。

④将学生信息按出生日期由大到小顺序排序。

⑤查询所有姓“张”的学生的学号和姓名。

(4)对student_info表,按性别顺序列出学生的学号、姓名、性别、出生日期及家庭住址,性别相同的按学号由小到大顺序排列。

(5)使用group by子句列出各个学生的平均成绩。

(6)使用union运算符将student_info表中姓“张”的学生的学号和姓名与curriculum表的课程编号、课程名称返回在一个表中,且列名为u_编号、u_名称。

(7)嵌套查询

① 在student_info表中查找与“刘卫平”性别相同的所有学生的姓名、出生日期。

② 使用in子查询查找所修课程编号为0002、0005的学生学号、姓名、性别。

③列出学号为0001的学生的分数比学号为0002号的学生的最低分数高的课程编号和分数。

④列出学号为0001的学生的分数比学号为0002号的学生的最高成绩还要高的课程编号和分数。

(8)连接查询。

①查询分数在80-90范围内的学生的学号、姓名和分数信息。

②查询学习“C语言程序设计”课程的学生的学号、姓名和分数。

③查询所有男同学的选课情况,要求列出学号、姓名、课程名称和分数。

④查询每个学生的所选课程的最高成绩,要求列出学号、姓名、课程编号和分数。

⑤查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选修课程的学生的总成绩为空。

⑥ 为grade表添加数据行:学号为0004、课程编号为0006、分数为76。查询所有课程的选修情况,要求列出课程编号、课程名称、选修人数,curriculum表中没有的课程列值为空。

四﹑命令(代码)清单

(2)

①

select 学号,姓名,出生日期 from student_info

②

select 姓名,家庭住址 from student_info where 学号='0002'

③

select 学号,姓名 from student_info where 性别='男'

(3)

①

select 学号,分数 from grade where 分数>=70 and 分数<=90

②

select avg(分数) from grade where 课程编号='0003'

③

select 课程编号,count(课程编号)as 学号 from grade
group by 课程编号

④

select * from student_info
order by 出生日期 desc

⑤

select 学号,姓名 from student_info
where 姓名 like '张%'

(4)

select 学号,姓名,性别,出生日期,家庭住址 from student_info
order by 性别,学号

(5)

select 学号,avg(分数)平均成绩 from grade group by 学号

(6)

select 学号 u_编号,姓名 u_名称 from student_info where 姓名 like '张%'
union
select 课程编号,课程名称 from curriculum

(7)

①

select 姓名,出生日期 from student_info where
性别=(select 性别 from student_info where 姓名='刘卫平')

②

select 学号,姓名,性别 from student_info where
学号 in(select 学号 from grade where 课程编号 in ('0002','0005'))

③

select 课程编号,分数 from grade where
学号='0001'and 分数>(select min(分数) from grade where 学号='0002')

④

select 课程编号,分数 from grade where
学号='0001'and 分数>(select max(分数) from grade where 学号='0002')

(8)

①

select grade.学号,姓名,分数 from grade,student_info where
grade.学号=student_info.学号 and (分数 between 80 and 90)

②

select grade.学号,姓名,分数 from grade,student_info,curriculum where
grade.学号=student_info.学号 and curriculum.课程编号=grade.课程编号
and 课程名称='C语言程序设计'

③

select grade.学号,姓名,课程名称,分数 from grade,student_info,curriculum where
grade.学号=student_info.学号 and curriculum.课程编号=grade.课程编号
and 性别='男'

④

select grade.学号,姓名,课程编号,分数 from grade,student_info where
grade.学号=student_info.学号 and 分数 in(select max(分数) from grade group by 学号)

⑤

select student_info.学号,姓名,总成绩
from student_info left outer join
(select 学号,sum(分数)as 总成绩 from grade group by 学号)
grade on student_info.学号=grade.学号

⑥

insert into grade values('0004','0006',76,null)

select curriculum.课程编号,课程名称,选修人数
from curriculum right outer join
(
select 课程编号,count(*)as 选修人数
from grade group by 课程编号
)
grade on curriculum.课程编号=grade.课程编号

五﹑调试和测试清单

(1)

①

 

 

②

③

④

(2)

①

②

③

(3)

①

②

③

④

⑤

(4)

(5)

(6)

(7)

①

②

③

④

(8)

①

②

③

④

⑤

⑥

 

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐