SQL实验5 子查询..doc
文本预览下载声明
实验报告
(201 —201 学年 第 学期)
课程名称 数据库原理与应用
实验名称
专 业
年 级
学号 姓名
指导教师
实验日期
实验目的与要求:
熟练掌握子查询中的无关子查询
掌握子查询中的相关子查询
掌握查询中谓词的使用 实验设备(环境):
PC机、SQL server2008 实验内容:
写出各题中各项操作的T-sql语句以及运行结果的截图展示。
--(1)查询手机数码的商品信息
--(2)查询购买苹果Iphone6的客户的姓名和住址
--(3)查询购买了“宝马小帅哥寸”的用户信息
--(4)查询从未网购过商品的用户信息
共10条记录
实验程序及结果:
(1)select *
from CommodityInfo
where SortId in(
select SortId
from CommoditySort
where SortName=手机数码)
(2)select username,useraddress
from UserInfo
where UserId in (
select UserId
from OrderInfo
where CommodityId in (
select CommodityId
from CommodityInfo
where CommodityName=苹果iphone6)
)
(3)select *
from UserInfo
where UserId in (
select UserId
from OrderInfo
where CommodityId in (
select CommodityId
from CommodityInfo
where CommodityName=宝马小帅哥寸)
)
(4)select *
from UserInfo
where UserId in(
select UserId
from UserInfo
except
select UserId
from OrderInfo)
实验总结(包括过程总结、心得体会及实验改进意见等):
在子查询实验中,学习到无关子查询的执行不依赖于外部查询,而且子查询的select语句中不能使用 order by子句,order by子句只能对最终查询结果排序,使用some\any\all\in子查询时some是any的一个同义词in和 not in是=any和all的简写,在使用exists的子查询时可检测到返回的结果集是否有行存在,在其前面加上not时,将对存在性测试结果取反。还有在实验过程中要注意的是子查询需要用括号( )括起来,子查询也可以嵌套使用,在select中不能使用image,text和ntext数据类型,而且在返回的结果的数据类型必须匹配外围查询where语句的数据类型。 指导教师评语:
成绩评定
教师签字 备注: 注:1、报告内的项目或设置,可根据实际情况加以补充和调整
2、教师批改学生实验报告应在学生提交实验报告10日内
(1)查询手机数码的商品信息
(2)查询购买苹果Iphone6的客户的姓名和住址
(3)查询购买了“宝马小帅哥寸”的用户信息
(4)查询从未网购过商品的用户信息
实验五子查询
显示全部