数据库系统概论 实验报告答案 实验六:数据表高级查询

实验六:数据表高级查询

一、实验目的

1.    掌握SELECT语句的基本语法和查询条件表示方法;

2.    掌握数据表的连接查询方法;

3.    掌握嵌套查询的表示及使用;

4.    掌握集合查询的表示及使用

二、实验学时

2学时

三、实验要求

1.    了解SELECT语句的基本语法格式和执行方法;

2.    了解连接查询的表示及使用;

3.    掌握嵌套查询和集合查询的使用的方法;

4.    完成实验报告;

四、实验内容

以实验5数据库为基础,请使用T-SQL 语句实现进行以下操作:

1.    查询名字中第2个字为‘向’的学生姓名和学号及选修的课程号、课程名;

select sname,XSKC.student.sno,XSKC.sc.cno,cname

from XSKC.student,XSKC.sc,XSKC.course

where XSKC.student.sno=XSKC.sc.sno and XSKC.sc.cno=XSKC.course.cno and sname like '_向%';

2.    列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩;

select XSKC.student.sno,sname,sdept,XSKC.sc.cno,grade

from XSKC.student,XSKC.sc,XSKC.course

where XSKC.student.sno=XSKC.sc.sno and XSKC.sc.cno=XSKC.course.cno and XSKC.sc.cno='2' or XSKC.sc.cno='8';

3.    查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;

select*

from XSKC.student

where sage != (

           select sage

            from XSKC.student

            where sname='张力');

4.    按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;

select XSKC.student.sno,sname,sdept,sum(ccredit) 已修学分

from XSKC.student,XSKC.sc,XSKC.course

where XSKC.student.sno=XSKC.sc.sno and XSKC.sc.cno=XSKC.course.cno and XSKC.sc.grade>='60'

group by XSKC.student.sno,XSKC.student.sname,XSKC.student.sdept

5.    查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;

select XSKC.student.sno,sname,cno

from XSKC.student,XSKC.sc

where XSKC.student.sno=XSKC.sc.sno and XSKC.sc.cno =any

(select cno from XSKC.sc,XSKC.student where XSKC.sc.sno=XSKC.student.sno and sname='张力' ) and sname != '张力'

6.    查询只被一名学生选修的课程的课程号、课程名;

select XSKC.sc.cno,cname

from XSKC.sc,XSKC.course

where XSKC.sc.cno=XSKC.course.cno

group by XSKC.sc.cno,cname

having count(*) = '1';

7.    使用嵌套查询出选修了“数据结构”课程的学生学号和姓名;

select XSKC.student.sno,sname

from XSKC.student,XSKC.sc

where cno = (select cno from XSKC.course where cname='数据结构' )

8.    使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;

select sname,sage,sdept

from XSKC.student

where sage< any (select sage

                 from XSKC.student

                  where sdept='cs')

       and sdept != 'CS';

9.    使用ANY、ALL 查询,列出其他院系中比WM系所有学生年龄小的学生的姓名;

select sname

from XSKC.student

where sage< ALL (select sage

                 from XSKC.student

                  where sdept='WM')

       and sdept != 'WM';

10.   分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息;

自身连接:

select two.*

from XSKC.student as one,XSKC.student as two

where one.sdept=two.sdept and one.sname='张力'and two.sname != '张力';

嵌套:

select *

from XSKC.student

where sdept = (select sdept

                 from XSKC.student

                  where sname='张力')

       and sname != '张力';

11.   使用集合查询列出CS系的学生以及性别为女的学生学号及姓名;

select sname

from XSKC.student

where sdept = 'CS'

union

select sname

from XSKC.student

where ssex = '女';

12.   使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;

交集:

select *

from XSKC.student

where sdept = 'CS'

intersect

select *

from XSKC.student

where sage <= '19';

差集:

select *

from XSKC.student

where sdept = 'CS'

except

select *

from XSKC.student

where sage <= '19';

五、实验步骤

命令方式:在【SQL Server Management Studio】窗口左上方选择【新建查询】按钮,启动SQL编辑器窗口,在光标处输入T-SQL语句,单击【执行】按钮。

  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rhyme_7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值