数据库复习题及答案.docx
《数据库复习题及答案.docx》由会员分享,可在线阅读,更多相关《数据库复习题及答案.docx(27页珍藏版)》请在冰豆网上搜索。
数据库复习题及答案
第一题:
考虑用二元联系(图1)对三元联系(图2)的表示:
1)分别给出E,A,B,C,RA,RB和RC的一个实例,这些实例不对应A,B,C和R的任何实例;
2)更改图1中的ER图,引入适当的约束以确保满足约束的E,A,B,C,RA,RB和RC的任何实例都对应于A,B,C和R的一个实例;
3)更改以上的转化以表示在三元联系上的全参与约束;
4)以上表示要求为E创建一个主码属性,试问如何将E处理为弱实体集,以便不需要主码?
解:
1)令E={e1,e2},A={a1,a2},B={b1},C={c1},RA={(e1,a1),(e2,a2)},Rb={(e1,b1)},Rc={(e1,c1)};
可以看出,由于元组(e2,a2)的原因,不存在任何实例对应于E,Ra,Rb,Rc
2)如下图所示:
通过引入E和关系Ra,Rb,Rc之间的全部参与的约束条件,以便在E中的每个元组都和A,B,C有关系。
3)假设A全部参与关系R,则在A和Ra之间引入全部参与约束。
将E看作弱实体集,而将Ra,Rb,Rc看作标志联系集。
如下图所示
第二题:
分别判断下列图中G1和G2是否互模拟(bisimulation),并说明理由
解:
(1)在图中标出各点的状态,我们构造关系S={(P0,Q0),(P1,Q1),(P2,Q1),(P3,Q2),(P4,Q3)}
可知G2可以模拟G1,下面我们讨论
S+1={(Q0,P0),(Q1,P1),(Q1,P2),(Q2,P3),(Q3,P4)}
是否可模拟,在G2中Q0有一个a变换可对应到G1中2个变换,即(Q1,P1)∈S-1,(Q1,P2)∈S-1。
但Q1有两个变换b,c,而在G1中公存在只有b或只有c的状态点,可知G1和G2不能互模拟。
(2)如图,标出各状态点,构造有关系
S={(P0,Q0),(P1,Q1),(P1,Q2),(P2,Q3),(P2,Q4),(P3,Q5)},
可知其中G1中的点均可由G2中的点模拟,下面我们考虑
S+1={(Q0,P0),(Q1,P1),(Q2,P1),(Q3,P2),(Q4,P2),(Q5,P3)},
可知同样其中G2中的点均可由G1中的点模拟,所以G1和G2互模拟。
第三题:
设有下列嵌套关系模式:
Emp=(Ename,childrenSetsetof(Childern),SkillsSetsetof(Skills))
Childern=(Cname,birthday)
Birthday=(day,month,year)
Skills=(Stype,examsSetsetoff(Exams))
Exams=(year,city)
1)试给出嵌套关系模式的XMLDTD表示,假定数据库中包含表emp(Emp)
2)试用SQL:
1999写出下列查询:
a)列出所有有一个孩子的生日在三月的员工的姓名;
b)列出所有在城市”Xi’an”参加过技能种类(Stype)为”Typing”考试(Exams)的员工的姓名(Ename);
c)列出关系Emp中的所有技能种类(Stype);
3)试用Xquery重写2)中的所有查询;
解:
1)
DOCTYPEdb[
ELEMENTemp(ename,children*,skills*)>
ELEMENTchildren(cname,birthday)>
ELEMENTbirthday(day,month,year)>
ELEMENTskills(stype,exams+)>
ELEMENTexams(year,city)>
ELEMENTename(#PCDATA)>
ELEMENTname(#PCDATA)>
ELEMENTday(#PCDATA)>
ELEMENTmonth(#PCDATA)>
ELEMENTyear(#PCDATA)>
ELEMENTtype(#PCDATA)>
ELEMENTcity(#PCDATA)>
]>
2)a――-selectenamefromEmpase,unnest(e.ChildrenSet)asc(Children)
where‘March’in(selectbirthday.monthfromc)
b―――selectenamefromEmpase,e.SkillsSetass,s.ExamsSetasx
wheres.stype=’typing’andx.city=’Xi’’an’
c―――selectdistincts.stypefromEmpase,e.SkillsSetass
3)a.for$ein/db/emp
let$empname:
=$e/@ename
where$e/children/birthday/month=”March”
return$empname
b.for$ein/db/emp
let$empname:
=$e/@ename
where$e/skills/stype=”typing”
and$e/skills/exams/city=”Xi’an”
return$empname
c.for$ein/db/emp
return$e/skills/stype
第四题:
FortheDTD,XML,andXQUERYgivenbelow,answerthequestionslistednext.
TheDocumentTypeDefinitionmyfriend.dtd:
ELEMENTmyfriends(person*)>
ELEMENTperson(id,name?
cell-phone*,children?
)>
ELEMENTchildren(child*)>
ELEMENTchild(name,toys*)>
ELEMENTname(#PCDATA)>
ELEMENTtoys(#PCDATA)>
ELEMENTid(#PCDATA)>...]
ELEMENTemployees(emp*)>
ELEMENTemp(id,work-phone,(contact|address)>
ELEMENTaddress(city,zip,street)>
ELEMENTid(#PCDATA)>]
ELEMENTcontact(#PCDATA)>...]
TheXMLDocumentfriends.xml:
1
``jack''
2222
2
3333
c1
c2t1
c2...
TheXMLDocumentemployees.xml:
1
9999
``me''
2
8888
czsTheXQUERYexpression:
FOR$outerin(friends.xml)//person,
LET$child:
=$outer/children
WHERE($outer/cellphone>2000)
RETURN
$outer/id
FOR$innerIN(employees.xml)/employees/emp[id=$outer/id]
RETURN
{
$outer/cellphone
$child/child
$inner/workphone
$inner/address/city
}
1)ListtheXMLoutputthattheXQUERYexpressionwouldgeneratewhenappliedtothegivenXMLinputdocuments.
2)DesignarelationalschematostorethetwogivenXMLdatafiles.
3)Designanobject-relationalschematostorethetwogivenXMLdatafiles.
4)ListtheSQLquerythatyouwouldgeneratetoexecutethegivenXQUERYexpressiononyourrelationaldatabase.StatewhatfinalcomputationswouldremaintobedonebytheXQUERYprocessorbeyondexecutingyourSQLstatement,ifany.
解:
1)
1
2222
9999
2
3333
c1
c2t1
c2
8888
c
2)person(pid,cellphone,name)
child(cid,parentid,name)
toy(tid,cid,toy_name)
emp(pid,workphone,contact,city,zip,street)
3)person(pid,name,cellphoneSetMultiSet(cellphones),ChildSetMultiSet(children))
cellphones(cellphone)
children(name,toySetMultiSet(toys))
toys(toyname)
emp(pid,workphone,contact,city,zip,street)
4)selectperson.cellphone,
array(selectchild.namechild.toyformchild
wherechild.parentid=person.pid)aschild_array,
emp.workphone,emp.city
fromperson,child,emp
whereperson.pid=emp.pidANDperson.cellphone>2000
第五题:
Supposeyouhavetorepresenttheinformationaboutparts.Eachparthasaname(unique),andatextualdescription.Partsmaybesimpleorcomplex.Asimpleparthasacolorbutnochildrensubparts.Acomplexparthasanumberofchildrensubparts(whichcanbesimpleorcomplex),eachofwhichmayberepeated.(E.g.,acarhas4wheels.)Youcanassumethateachpartcanbeachildsubpartofatmostoneotherpart(soeachpart,togetherwithitssubparts,canbeviewedasatree).Donotassumeanyfixednumberoflevelsofpartcomposition.
1)DefinetheschemaofXMLdocumentscontainingpartinformationusingDTDs.
2)GiveanexampleofadocumentinstancewhichisvalidundertheDTDs.
3)WritethefollowingqueriesinXQuery:
(a)findthenamesofalltheyellowparts.
(b)findallthepartsthathaveatleast5distinctchildrensubparts.
(c)findallthepartscontainingadescendantsubpartnamed“spoke"andnotcontainingadescendantsubpartnamed“tire".
Answer:
ANSWER:
1)
DOCTYPEParts[
ELEMENTParts(part)+>
ELEMENTPart(description,subpartinfo*|color))>
ATTLISTPartnameID#REQUIRED>
ELEMENTsubpartinfo(part)>
ATTLISTsubpartinfonameID#REQUIRED>
ELEMENTcolor(#PCDATA)>
ELEMENTdescription(#PCDATA)>
]>
2)
part1
blue
part2
part3
yellow
3)(a)for$pin/parts/part
where$p/color=”yellow”
return$p/@name
(b)for$pin/parts/part
wherecount(distinct($p/subpartinfo))>=5
return$p/@name
(c)for$pin/parts/part
where($p/@name=”sopke”)and(not($p/@name=”tire”))
return$p/@name
第六题:
ConsidertheseXMLelementsforthe'prefix'and'infix'applicationofabinaryfunction,hereadd,toitstwovariablearguments,herexandy:
add
x
y
x
add
y
CompletethefollowingXSLTtemplate-byjustfillinginthesevenversionsof"___"
-forthe(XML-to-XML)transformationof'prefix'applicationsinto'infix'
applications:
templatematch="prefix">
<_infix___>
<_larg_>value-ofselect="arg1"/>
__value-ofselect=”fun”>
__
__value-ofselect=”arg2”>
____
template>
Couldthistransformationbe'inverted'-mapping'infix'applicationsto'prefix'
applications-withoutinformationloss(writeinonly"yes"or"no"here)?
Answer:
No!
!
!
!
!
第七题:
ConsidertherelationPARTS,whichhasPart#ashashkeyandwhichincludesrecordswiththefollowingPart#values:
2369,3760,5046,4871,5659,2222,1821,1074,7115,1620,2428,3943,4750,3157,6975,4981,9208.Thehashfunctionuses8buckets,numbered0to7.Eachbucketisonediskblockandholdstworecords.
1)Loadtheserecordsintothefileinthegivenorderusingthehashfunctionh(K)=Kmod8.CalculatetheaveragenumberofblockaccessesforarandomretrievalonPart#.
2)Nowloadtherecordsintoexpandablehashfilesbasedonlinearhashing.Startwithasinglediskblock,usingthehashfunctionh0=Kmod2º,andshowhowthefilegrowsandhowthehashfunctionschangeastherecordsareinserted.Assumethatblocksaresplitwheneveranoverflowoccurs,andshowthevalueofnateachstage.
解:
1)
平均查找代价:
(8+6*2+3+3+3)/17=1.71
2)如下页图
第八题:
设关系r1(A,B,C),r2(C,D,E)有如下特性:
r1有200000个元组,r2有45000个元组,一块中可容纳25个r1元组或30个r2元组。
试估算以下每一种策略计算r1|><|r2所需存取的块数:
1)嵌套循环连接
2)块嵌套循环连接
3)归并连接
4)散列连接
解:
r1需要8000个块,r2需要1500个块。
假设有一个存储器有M页。
如果M>8000,那么使用平坦嵌套循环,通过1500+8000次磁盘存取就可以很容易的完成连接操作。
因此我们只考虑M<=8000的情况。
A.嵌套循环连接:
使用r1作为外关系,我们需要进行200000×1500+8000=300,008,000次磁盘存取。
如果r2是外关系,那么我们需要45000×8000+1500=360001500次磁盘存取。
B.块嵌套循环连接:
如果r1是外关系,我们需要
×1500+8000次磁盘存取,如果r2是外关系,我们需要
×8000+1500次磁盘存取。
C.归并连接
假设r1和r2最初没有按连接关键字进行排序,那么总的排序加上输出的耗费为Bs=1500(2
+1)+8000(2
+1)次磁盘存取。
假设具有相同连接属性值的所有员组装入内存中,那么总的耗费是Bs+1500+8000次磁盘存取。
d.散列连接
我们假设不发生溢出。
因为r2比较小,所以我们用r2作为创建关系,用r1作为探针关系。
如果M>1500,那么就不需要进行递归分割,于是耗费为3(1500+8000)=28500次磁盘存取,否则耗费为2(1500+8000)
+1500+8000次磁盘存取。
第九题:
Considerahash-joinoftworelationsRandShaving