欢迎来到天天文库
浏览记录
ID:46233828
大小:282.34 KB
页数:25页
时间:2019-11-21
《数据库原理及应用6》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、量化比较谓词exprθ{some
2、any
3、all
4、}(subquery)
5、(常数集)θ为(<,<=,=,>=,<>,>)等比较符其中some和any含义相同。但是大多数数据库产品支持some形式。例3.4.7检索出佣金百分率最小的代理商的pidSelectaidfromagentswherepercent<=all(selectpercentfromagents)例3.4.8检索出与住在Dallas或Boston的顾客拥有相同折扣率的所有的顾客编号及其姓名。(请大家思考!)Selectcid,cnamefromcustomerswherediscntin
6、(selectdiscntfromcustomerswherecity=‘Dallas’orcity=‘Boston’)当然也等价于Selectcid,cnamefromcustomerswherediscnt=some(selectdiscntfromcustomerswherecity=‘Dallas’orcity=‘Boston’)实际上exprin与expr=some或any等价考虑:exprnotin与expr<>some或any等价吗?例3.4.9检索出所有满足以下条件的顾客cid:该顾客的discnt小于任一住在Duluth的顾客的disc
7、nt。selectcidfromcustomerswherediscntsome或any应,exprnotin等价于expr<>allEXISTS谓词表示为:[not]exists(Subquery)exists(subq
8、uery)为真当且仅当子查询返回一个非空的集合。notexists(subquery)为真当且仅当子查询返回一个空的集合。例3.4.10检索通过代理商a05订货的顾客的名字。方法一:selectdistinctC.cnamefromcustomersCwhereexists(select*fromordersOwhereC.cid=O.cidandO.aid=‘a05’)还有其他解法吗?请大家思考!譬如selectC.cnamefromcustomersC,ordersOwhereC.cid=O.cidandO.aid=‘a05’Selectdistin
9、ctcnamefromcustomersCwherecidin(selectO.cidfromordersOwhereO.aid=‘a05’)例3.4.11检索出既订购了产品p01又订购了产品p07的顾客的cid。(orderswherepid=‘p01’)[cid]intersect(orderswherepid=‘p07’)[cid]考虑如何使用基础SQL解决上述问题?方法一:SelectdistinctO1.cidfromordersO1,ordersO2whereO1.cid=O2.cidandO1.pid=‘p01’andO2.pid=‘p07
10、’方法二:SelectdistinctcidfromordersOwherepid=‘p01’andexists(select*fromorderswherecid=O.cidandpid=‘p07’)回顾上一节:检索没有通过代理商a05订货的所有顾客的名字。(在此我们使用exists谓词来完成)SelectdistinctC.cnamefromcustomersCwherenotexists(select*fromordersOwhereC.cid=O.cidandO.aid=‘a05’)当然我们也可以使用all谓词SelectdistinctC.cn
11、amefromcustomersCwhereC.cid<>all(selectcidfromorderswhereaid=‘a05’)说明:差运算minus,现在许多数据库产品还不支持这样的运算符,但是高级SQL中加入了这样的运算符。一般来说,当R和S是兼容表Head(R)=Head(S)=A1…..An,R-S可以通过SQL语句表示为:selectA1….AifromRwherenotexists(select*fromSwhereS.A1=R.A1and…andS.An=R.An)例3.4.15检索订购了产品p01的顾客所在的city。请大家考虑至少
12、用三种以上的方法来实现!方法一:selectdistinctcityfromcu
此文档下载收益归作者所有