数据库第五章课后习题答案

合集下载

数据库第五章习题及答案

数据库第五章习题及答案

数据库第五章习题及答案本文档为数据库第五章的习题及答案,帮助读者巩固数据库相关知识。

习题1. 数据库的优点有哪些?数据库具有以下优点: - 数据共享:多个用户可以同时访问和共享数据库中的数据。

- 数据一致性:数据库提供事务管理能力,保证了数据的一致性。

- 数据持久性:数据在数据库中是永久存储的,不会因为系统关机或程序结束而丢失。

- 数据冗余度低:数据库通过规范化设计,减少了数据的冗余性,提高了数据的存储效率。

- 数据独立性:数据库支持数据与应用程序的独立性,提高了系统的灵活性和维护性。

- 数据安全性:数据库提供了用户权限管理和数据备份机制,保证了数据的安全性。

2. 数据库的三级模式结构是什么?数据库的三级模式结构包括: - 外模式(视图层):外模式是用户所看到的数据库的子集,用于描述用户对数据库的逻辑视图。

每个用户可以有不同的外模式来满足自己的需求。

- 概念模式(逻辑层):概念模式是全局数据库的逻辑结构和组织方式,描述了数据的总体逻辑视图。

概念模式独立于具体的应用程序,是数据库管理员的角度来看待数据库的。

- 内模式(物理层):内模式是数据库的存储结构和物理组织方式,描述了数据在存储介质上的实际存储方式。

3. 数据库的完整性约束有哪些?数据库的完整性约束包括: - 实体完整性约束:确保表的主键不为空,每个实体都能够唯一标识。

- 参照完整性约束:确保外键的引用关系是有效的,即外键值必须等于被引用表中的主键值或者为空。

- 用户定义完整性约束:用户可以自定义额外的完整性约束,如检查约束、唯一约束、默认约束等。

4. 数据库的关系模型有哪些特点?数据库的关系模型具有以下特点: - 数据用二维表的形式进行组织,表由行和列组成,每一行表示一个实体,每一列表示一个属性。

- 表与表之间通过主键和外键建立关联关系,形成关系。

- 关系模型提供了一种数据独立性的设计方法,使得应用程序与数据的逻辑结构相分离,提高了系统的灵活性和可维护性。

数据库系统基础教程课后答案第五章

数据库系统基础教程课后答案第五章

Exercise 5.1.1 As a set:Average = 2.37 As a bag:Average = 2.48 Exercise 5.1.2 As a set:Average = 218 As a bag:Average = 215 Exercise 5.1.3a As a set:As a bag:Exercise 5.1.3bπbore(Ships Classes)Exercise 5.1.4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union of bags R and S will have tuple t appear n + m times. The further union of bag T with the tuple t appearing o times will have tuple t appear n + m + o times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the union of bags R and S will have tuple t appear m + o times. The further union of bag R with the tuple t appearing n times will have tuple t appear m + o + n times in the final result.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result. Since we cannot have duplicates, the result only has at most one copy of the tuple t.Exercise 5.1.4bFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the intersectionof bags R and S will have tuple t appear min( n, m ) times. The further intersection of bag T with the tuple t appearing o times will produce tuple t min( o, min( n, m ) ) times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the intersection of bags R and S will have tuple t appear min( m, o ) times. The further intersection of bag R with the tuple t appearing n times will produce tuple t min( n, min( m, o ) ) times in thefinal result.The intersection of bags R,S and T will yield a result where tuple t appears min( n,m,o ) times. For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result.Exercise 5.1.4cFor bags:On the left-hand side:Given that tuple r in R, which appears m times, can successfully join with tuple s in S,which appears n times, we expect the result to contain mn copies. Also given that tuple tin T, which appears o times, can successfully join with the joined tuples of r and s, weexpect the final result to have mno copies.On the right-hand side:Given that tuple s in S, which appears n times, can successfully join with tuple t in T,which appears o times, we expect the result to contain no copies. Also given that tuple rin R, which appears m times, can successfully join with the joined tuples of s and t, weexpect the final result to have nom copies.The order in which we perform the natural join does not matter for bags.For sets:This is a similar case when dealing with bags except the joined tuples can only appear at most once in each result. If there are tuples r,s,t in relations R,S,T that can successfully join, then the result will contain a tuple with the schema of their joined attributes.Exercise 5.1.4dFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the union of these two bags R ⋃ S, tuple t would appear n + m times. Likewise, in the union of these two bags S ⋃ R, tuple t would appear m + n times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in both sets R and S will the union R ⋃ S have the tuple t. The same reasoning holds when we take the union S ⋃ R.Therefore the commutative law for union holds.Exercise 5.1.4eFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the intersection of these two bags R ∩ S, tuple t would appear min( n,m ) times. Likewise in the intersection of these two bags S ∩ R, tuple t would appear min( m,n ) times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in at least one of the sets R and S will the intersection R ∩ S have the tuple t. The same reasoning holds when we take the intersection S ∩ R.Therefore the commutative law for intersection holds.Exercise 5.1.4fFor bags:Suppose a tuple t occurs n times in bag R and tuple u occurs m times in bag S. Suppose also that the two tuples t,u can successfully join. Then in the natural join of these two bags R S, the joined tuple would appear nm times. Likewise in the natural join of these two bags S R, the joined tuple would appear mn times. Both sides of the relation yield the same result.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuples u,v might appear respectively in sets R and S one or zero times. The combinations of number of occurrences for tuples u,v in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple u exists in Rand tuple v exists in S will the natural join R S have the joined tuple. The same reasoning holds when we take the natural join S R.Therefore the commutative law for natural join holds.Exercise 5.1.4gFor bags:Suppose tuple t appears m times in R and n times in S. If we take the union of R and S first, we will get a relation where tuple t appears m + n times. Taking the projection of a list of attributes L will yield a resulting relation where the projected attributes from tuple t appear m + n times. If we take the projection of the attributes in list L first, then the projected attributes from tuple t would appear m times from R and n times from S. The union of these resulting relations would have the projected attributes of tuple t appear m + n times.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t exists in R or S (or both R and S) will the projected attributes of tuple t appear in the result.Therefore the law holds.Exercise 5.1.4hFor bags:Suppose tuple t appears u times in R, v times in S and w times in T. On the left hand side, the intersection of S and T would produce a result where tuple t would appear min(v , w) times. With the addition of the union of R, the overall result would have u + min(v , w) copies of tuple t. On the right hand side, we would get a result of min(u + v, u + w) copies of tuple t. The expressions on both the left and right sides are equivalent.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R,S and T one or zero times. The combinations of number of occurrences for tuple t in R, S and T respectively are (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0) and (1,1,1). Only when tuple t appears in R or in both S and T will the result have tuple t.Therefore the distributive law of union over intersection holds.Exercise 5.1.4iSuppose that in relation R, u tuples satisfy condition C and v tuples satisfy condition D. Suppose also that w tuples satisfy both conditions C and D where w≤ min(v , w). Then the left hand side will return those w tuples. On the right hand side, σC(R) produces u tuples and σD(R) produces v tuples. However, we know the intersection will produce the same w tuples in the result.When considering bags and sets, the only difference is bags allow duplicate tuples while sets only allow one copy of the tuple. The example above applies to both cases.Therefore the law holds.Exercise 5.1.5aFor sets, an arbitrary tuple t appears on the left hand side if it appears in both R,S and not in T. The same is true for the right hand side.As an example for bags, suppose that tuple t appears one time each in both R,T and two times in S. The result of the left hand side would have zero copies of tuple t while the right hand side would have one copy of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5bFor sets, an arbitrary tuple t appears on the left hand side if it appears in R and either S or T. This is equivalent to saying tuple t only appears when it is in at least R and S or in R and T. The equivalence is exactly the right side’s expression.As an example for bags, suppose that tuple t appears one time in R and two times each in S and T. Then the left hand side would have one copy of tuple t in the result while the right hand side would have two copies of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5cFor sets, an arbitrary tuple t appears on the left hand side if it satisfies condition C, condition Dor both condition C and D. On the right hand side, σC(R) selects those tuples that satisfy condition C while σD(R) selects those tuples that satisfy condition D. However, the union operator will eliminate duplicate tuples, namely those tuples that satisfy both condition C and D. Thus we are ensured that both sides are equivalent.As an example for bags, we only need to look at the union operator. If there are indeed tuples that satisfy both conditions C and D, then the right hand side will contain duplicate copies of those tuples. The left hand side, however, will only have one copy for each tuple of the original set of tuples.Exercise 5.2.1bExercise 5.2.1cExercise 5.2.1dExercise 5.2.1fExercise 5.2.1gExercise 5.2.1hExercise 5.2.1iExercise 5.2.1jExercise 5.2.1kExercise 5.2.1lExercise 5.2.1mExercise 5.2.1nExercise 5.2.2aApplying the δ operator on a relation with no duplicates will yield the same relation. Thus δ is idempotent.Exercise 5.2.2bThe result of πL is a relation over the list of attributes L. Performing the projection again will return the same relation because the relation only contains the list of attributes L. Thus πL is idempotent.Exercise 5.2.2cThe result of σC is a relation where condition C is satisfied by every tuple. Performing the selection again will return the same relation because the relation only contains tuples that satisfy the condition C. Thus σC is idempotent.Exercise 5.2.2dThe result of γL is a relation whose schema consists of the grouping attributes and the aggregated attributes. If we perform the same grouping operation, there is no guarantee that the expression would make sense. The grouping attributes will still appear in the new result. However, the aggregated attributes may or may not appear correctly. If the aggregated attribute is given a different name than the original attribute, then performing γL would not make sense because it contains an aggregation for an attribute name that does not exist. In this case, the resultingrelation would, according to the definition, only contain the grouping attributes. Thus, γL is not idempotent.Exercise 5.2.2eThe result of τ is a sorted list of tuples based on some attributes L. If L is not the entire schema of relation R, then there are attributes that are not sorted on. If in relation R there are two tuples that agree in all attributes L and disagree in some of the remaining attributes not in L, then it is arbitrary as to which order these two tuples appear in the result. Thus, performing the operation τ multiple times can yield a different relation where these two tuples are swapped. Thus, τ is not idempotent.Exercise 5.2.3If we only consider sets, then it is possible. We can take πA(R) and do a product with itself. From this product, we take the tuples where the two columns are equal to each other.If we consider bags as well, then it is not possible. Take the case where we have the two tuples (1,0) and (1,0). We wish to produce a relation that contains tuples (1,1) and (1,1). If we use the classical operations of relational algebra, we can either get a result where there are no tuples or four copies of the tuple (1,1). It is not possible to get the desired relation because no operation can distinguish between the original tuples and the duplicated tuples. Thus it is not possible to get the relation with the two tuples (1,1) and (1,1).Exercise 5.3.1a)Answer(model) ← PC(model,speed,_,_,_) AND speed ≥ 3.00b)Answer(maker) ← Laptop(model,_,_,hd,_,_) AND Product(maker,model,_) AND hd ≥100c)Answer(model,price) ← PC(model,_,_,_,price) AND Product(maker,model,_) ANDmaker=’B’Answer(model,price) ← Laptop(mode l,_,_,_,_,price) AND Product(maker,model,_)AND maker=’B’Answer(model,price) ← Printer(model,_,_,price) AND Product(maker,model,_) ANDmaker=’B’d)Answer(model) ← Printer(model,color,type,_) AND color=’true’ AND type=’laser’e)PCMaker(maker) ← Product(maker,_,type) AND type=’pc’LaptopMaker(maker) ← Product(maker,_,type) AND type=’laptop’Answer(maker) ← LaptopMaker(maker) AND NOT PCMaker(maker)f)Answer(hd) ← PC(model1,_,_,hd,_) AND PC(model2,_,_,hd,_) AND model1 <>model2g)Answer(model1,model2) ← PC(model1,speed, ram,_,_) ANDPC(model2,_speed,ram,_,_) AND model1 < model2h)FastComputer(model) ← PC(model,speed,_,_,_) AND speed ≥ 2.80FastComputer(model) ← Laptop(model,speed,_,_,_,_) AND speed ≥ 2.80Answer(maker) ← Product(maker,model1,_) AND Product(maker,mod el2,_) ANDFastComputer(model1) AND FastComputer(model2) AND model1 <> model2i)Computers(model,speed) ← PC(model,speed,_,_,_)Computers(model,speed) ← Laptop(model,speed,_,_,_,_)SlowComputers(model) ← Computers(model,speed) AND Computers(model1,speed1)AND speed < speed1FastestComputers(model) ← Computers(model,_) AND NOT SlowComputers(model)Answer(maker) ← Fast estComputers(model) AND Product(maker,model,_)j)PCs(maker,speed) ← PC(model,speed,_,_,_) AND Product(maker,model,_) Answer(maker) ← PCs(maker,spe ed) AND PCs(maker,speed1) AND PCs(maker,speed2) AND speed <> speed1 AND speed <> speed2 AND speed1 <> speed2k)PCs(maker,model) ← Product(maker,model,type) AND type=’pc’Answer(maker) ← PCs(maker,model) AND PCs(maker,model1) ANDPCs(maker,model2) AND PCs(maker,model3) AND model <> model1 AND model <>model2 AND model1 <> model2 AND (model3 = model OR model3 = model1 ORmodel3 = model2)Exercise 5.3.2a)Answer(class,country) ← Classes(class,_,country,_,bore,_) AND bore ≥ 16b)Answer(name) ← Ships(name,_,launch ed) AND launched < 1921c)Answer(ship) ← Outcomes(ship,battle,result) AND battle=’Denmark Strait’ AND result= ‘sunk’d)Answer(name) ← Classes(class,_,_,_,_,displacement) AND Ships(name,class,launched)AND displacement > 35000 AND launched > 1921e)Answer(nam e,displacement,numGuns) ← Classes(class,_,_,numGuns,_,displacement)AND Ships(name,class,_) AND Outcomes (ship,battle,_) AND battle=’Guadalcanal’AND ship=namef)Answer(name) ← Ships(name,_,_)Answer(name) ← Outcomes(name,_,_) AND NOT Answer(name)g)MoreThan One(class) ← Ships(name,class,_) AND Ships(name1,class,_) AND name <>name1Answer(class) ← Classes(class,_,_,_,_,_) AND NOT MoreThanOne(class)h)Battleship(country) ← Classes(_,type,country,_,_,_) AND type=’bb’Battlecruiser(country) ← Classes(_,type,country,_,_,_) AND type=’bc’Answer(country) ← Battleship(country) AND Battlecruiser(country)i)Results(ship,result,date) ← Battles(name,date) AND Outcomes(ship,battle,result) ANDbattle=nameAnswer(ship) ← Results(ship,result,date) AND Results(ship,_,date1) ANDresult=’damaged’ AND date < date1Exercise 5.3.3A nswer(x,y) ← R(x,y) AND z = zExercise 5.4.1aAnswer(a,b,c) ← R(a,b,c)Answer(a,b,c) ← S(a,b,c)Exercise 5.4.1bAnswer(a,b,c) ← R(a,b,c) AND S(a,b,c)Exercise 5.4.1cAnswer(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)Exercise 5.4.1dUnion(a,b,c) ← R(a,b,c)Union(a,b,c) ← S(a,b,c)Answer(a,b,c) ← Union(a,b,c) AND NOT T(a,b,c)Exercise 5.4.1eJ(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)K(a,b,c) ← R(,a,b,c) AND NOT T(a,b,c)Answer(a,b,c) ← J(a,b,c) AND K(a,b,c)Exercise 5.4.1fAnswer(a,b) ← R(a,b,_)Exercise 5.4.1gJ(a,b) ← R(a,b,_)K(a,b) ← S(_,a,b)Answer(a,b) ← J(a,b) AND K(a,b)Exercise 5.4.2aAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2bAnswer(x,y,z) ← R(x,y,z) AND x < y AND y < z Exercise 5.4.2cAnswer(x,y,z) ← R(x,y,z) AND x < yAnswer(x,y,z) ← R(x,y,z) AND y < zExercise 5.4.2dChange: NOT(x < y OR x > y)To: x ≥ y AND x ≤ yThe above simplifies to x = yAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2eChange: NOT((x < y OR x > y) AND y < z)NOT(x < y OR x > y) OR y ≥ z(x ≥ y AND x ≤ y) OR y ≥ zTo: x = y OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x = yAnswer(x,y,z) ← R(x,y,z) AND y ≥ zExercise 5.4.2fChange: NOT((x < y OR x < z) AND y < z)NOT(x < y OR x < z) OR y ≥ z To: (x ≥ y AND x ≥ z) OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x ≥ y AND x ≥ zAnswer(x,y,z) ← R(x,y,z) AND y ≥zExercise 5.4.3aAnswer(a,b,c,d) ← R(a,b,c) AND S(b,c,d)Exercise 5.4.3bAnswer(b,c,d,e) ← S(b,c,d) AND T(d,e)Exercise 5.4.3cAnswer(a,b,c,d,e) ← R(a,b,c) AND S(b,c,d) AND T(d,e)Exercise 5.4.4a)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syb)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < sy AND ry < szc)Answer(rx,ry,rz,sx,sy,s z) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < syAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry < szd)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = sye)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syAnswe r(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szf)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx ≥ sy AND rx ≥ szAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szExercise 5.4.5aR1 := πx,y(Q R)Exercise 5.4.5bR1 := ρR1(x,z)(Q)R2 := ρR2(z,y)(Q)R3 := πx,y(R1 (R1.z = R2.z) R2)Exercise 5.4.5cR1 := πx,y(Q R)R2 := σx < y(R1)。

数据库系统基础教程第五章答案

数据库系统基础教程第五章答案

数据库系统基础教程第五章答案Exercise 5.1.1 As a set:Average = 2.37 As a bag:Average = 2.48 Exercise 5.1.2Average = 218 As a bag:Average = 215 Exercise 5.1.3a As a set:18As a bag:bore1516141615151418Exercise 5.1.3bπ(Ships Classes)boreExercise 5.1.4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union of bags R and S will have tuple t appear n + m times. The further union of bag T with the tuple t appearing o times will havetuple t appear n + m + o times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively,the union of bags R and S will have tuple t appear m + o times. Thefurther union of bag R with the tuple t appearing n times will havetuple t appear m + o + n times in the final result.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result. Since we cannot have duplicates, the result only has at most one copy of the tuple t.Exercise 5.1.4bFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively,the intersection of bags R and S will have tuple t appear min( n, m )times. The further intersection of bag T with the tuple t appearing otimes will produce tuple t min( o, min( n, m ) ) times in the finalresult.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively,the intersection of bags R and S will have tuple t appear min( m, o )times. The further intersection of bag R with the tuple t appearing ntimes will produce tuple t min( n, min( m, o ) ) times in the finalresult.The intersection of bags R,S and T will yield a result where tuple t appears min( n,m,o ) times.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result.Exercise 5.1.4cFor bags:On the left-hand side:Given that tuple r in R, which appears m times, can successfully joinwith tuple s in S, which appears n times, we expect the result tocontain mn copies. Also given that tuple t in T, which appears o times, can successfully join with the joined tuples of r and s, we expect thefinal result to have mno copies.On the right-hand side:Given that tuple s in S, which appears n times, can successfully joinwith tuple t in T, which appears o times, we expect the result tocontain no copies. Also given that tuple r in R, which appears m times,can successfully join with the joined tuples of s and t, we expect thefinal result to have nom copies.The order in which we perform the natural join does not matter for bags.For sets:This is a similar case when dealing with bags except the joined tuples can only appear at most once in each result. If there are tuples r,s,t inrelations R,S,T that can successfully join, then the result will contain a tuple with the schema of their joined attributes. Exercise 5.1.4dFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the union of these two bags R S, tuple t would appear n + m times. Likewise, in the union of these two bags S R, tuple t would appear m + n times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in both sets R and S will the union R S have the tuple t. The same reasoning holds when we take the union S R.Therefore the commutative law for union holds.Exercise 5.1.4eFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the intersection of these two bags R ∩ S, tuple t would appear min( n,m ) times. Likewise in the intersection of these two bags S ∩ R, tuple t would appear min( m,n ) times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuplet in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in at least one of the sets R and S will the intersection R ∩ S have the tuple t. The same reasoning holds when we take the intersection S ∩ R.Therefore the commutative law for intersection holds.Exercise 5.1.4fFor bags:Suppose a tuple t occurs n times in bag R and tuple u occurs m times in bag S. Suppose also that the two tuples t,u can successfully join. Then in thenatural join of these two bags R S, the joined tuple would appear nm times. Likewise in the natural join of these two bags S R, the joined tuple would appear mn times. Both sides of the relation yield the same result.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuples u,v might appear respectively in sets R and S one or zero times. The combinations of number of occurrences for tuples u,v in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple u exists in R and tuple v exists in S will the natural join R S have the joined tuple. The same reasoning holds when we take the natural join S R.Therefore the commutative law for natural join holds.Exercise 5.1.4gFor bags:Suppose tuple t appears m times in R and n times in S. If we take the union of R and S first, we will get a relation where tuple t appears m + n times. Taking the projection of a list of attributes L will yield a resultingrelation where the projected attributes from tuple t appear m + n times. If we take the projection of the attributes in list L first, then the projected attributes from tuple t would appear m times from R and n times from S. The union of these resulting relations would have the projected attributes oftuple t appear m + n times.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple tmight appear in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t exists in R or S (or both R and S) will the projected attributes of tuple t appear in the result.Therefore the law holds.Exercise 5.1.4hFor bags:Suppose tuple t appears u times in R, v times in S and w times in T. On theleft hand side, the intersection of S and T would produce a result where tuple t would appear min(v , w) times. With the addition of the union of R, the overall result would have u + min(v , w) copies of tuple t. On the right hand side, we would get a result of min(u + v, u + w) copies of tuple t. The expressions on both the left and right sides are equivalent.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple tmight appear in sets R,S and T one or zero times. The combinations of numberof occurrences for tuple t in R, S and T respectively are (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0) and (1,1,1). Only when tuple t appears in R or in both S and T will the result have tuple t.Therefore the distributive law of union over intersection holds.Exercise 5.1.4iSuppose that in relation R, u tuples satisfy condition C and v tuples satisfy condition D. Suppose also that w tuples satisfy both conditions C and D where w≤ min(v , w). Then the left hand side will return those w tuples. On the(R) produces u tuples and σD(R) produces v tuples. However, right hand side, σCwe know the intersection will produce the same w tuples in the result.。

数据库系统原理课后答案 第五章

数据库系统原理课后答案 第五章

5.1 名词解释(1)SQL模式:SQL模式是表和授权的静态定义。

一个SQL模式定义为基本表的集合。

一个由模式名和模式拥有者的用户名或账号来确定,并包含模式中每一个元素(基本表、视图、索引等)的定义。

(2)SQL数据库:SQL(Structured Query Language),即‘结构式查询语言’,采用英语单词表示和结构式的语法规则。

一个SQL数据库是表的汇集,它用一个或多个SQL模式定义。

(3)基本表:在SQL中,把传统的关系模型中的关系模式称为基本表(Base Table)。

基本表是实际存储在数据库中的表,对应一个关系。

(4)存储文件:在SQL中,把传统的关系模型中的存储模式称为存储文件(Stored File)。

每个存储文件与外部存储器上一个物理文件对应。

(5)视图:在SQL中,把传统的关系模型中的子模式称为视图(View),视图是从若干基本表和(或)其他视图构造出来的表。

(6)行:在SQL中,把传统的关系模型中的元组称为行(row)。

(7)列:在SQL中,把传统的关系模型中的属性称为列(coloumn)。

(8)实表:基本表被称为“实表”,它是实际存放在数据库中的表。

(9)虚表:视图被称为“虚表”,创建一个视图时,只把视图的定义存储在数据词典中,而不存储视图所对应的数据。

(10)相关子查询:在嵌套查询中出现的符合以下特征的子查询:子查询中查询条件依赖于外层查询中的某个值,所以子查询的处理不只一次,要反复求值,以供外层查询使用。

(11)联接查询:查询时先对表进行笛卡尔积操作,然后再做等值联接、选择、投影等操作。

联接查询的效率比嵌套查询低。

(12)交互式SQL:在终端交互方式下使用的SQL语言称为交互式SQL。

(13)嵌入式SQL:嵌入在高级语言的程序中使用的SQL语言称为嵌入式SQL。

(14)共享变量:SQL和宿主语言的接口。

共享变量有宿主语言程序定义,再用SQL 的DECLARE语句说明, SQL语句就可引用这些变量传递数据库信息。

《MySQL数据库原理、设计与应用》第5章课后习题答案

《MySQL数据库原理、设计与应用》第5章课后习题答案

第五章一、填空题1.逗号或,2. 33.FLOOR(3+RAND()*(11-3+1))或FLOOR(3+RAND()*9)4.NULL5.ON DUPLICATE KEY二、判断题1.错2.对3.错4.对5.对三、选择题1. D2. B3. D4. A5. C四、简答题1.请简述DELETE与TRUNCA TE的区别。

答:①实现方式不同:TRUNCATE本质上先执行删除(DROP)数据表的操作,然后再根据有效的表结构文件(.frm)重新创建数据表的方式来实现数据清空操作。

而DELETE语句则是逐条的删除数据表中保存的记录。

②执行效率不同:在针对大型数据表(如千万级的数据记录)时,TRUNCATE清空数据的实现方式,决定了它比DELETE语句删除数据的方式执行效率更高。

③对AUTO_INCREMENT的字段影响不同,TRUNCATE清空数据后,再次向表中添加数据,自动增长字段会从默认的初始值重新开始,而使用DELETE语句删除表中的记录时,则不影响自动增长值。

④删除数据的范围不同:TRUNCATE语句只能用于清空表中的所有记录,而DELETE语句可通过WHERE指定删除满足条件的部分记录。

⑤返回值含义不同:TRUNCATE操作的返回值一般是无意义的,而DELETE语句则会返回符合条件被删除的记录数。

⑥所属SQL语言的不同组成部分:DELETE语句属于DML数据操作语句,而TRUNCA TE通常被认为是DDL数据定义语句。

2.请简述WHERE与HA VING之间的区别。

1答:①WHERE操作是从数据表中获取数据,用于将数据从磁盘存储到内存中,而HA VING是对已存放到内存中的数据进行操作。

②HA VING位于GROUP BY子句后,而WHERE位于GROUP BY 子句之前。

③HA VING关键字后可以跟聚合函数,而WHERE则不可以。

通常情况下,HA VING关键字与GROUPBY一起使用,对分组后的结果进行过滤。

数据库第五章习题及答案

数据库第五章习题及答案

第五章 关系数据理论一、 单项选择题1、设计性能较优的关系模式称为规范化,规范化主要的理论依据是 ( )A 、关系规范化理论B 、关系运算理论C 、关系代数理论D 、数理逻辑2、关系数据库规范化是为解决关系数据库中( )问题而引入的。

A 、插入、删除和数据冗余B 、提高查询速度C 、减少数据操作的复杂性D 、保证数据的安全性和完整性3、当关系模式R (A ,B )已属于3NF ,下列说法中( )是正确的。

A 、它一定消除了插入和删除异常B 、一定属于BCNFC 、仍存在一定的插入和删除异常D 、A 和C 都是4、在关系DB 中,任何二元关系模式的最高范式必定是( )A 、1NFB 、2NFC 、3NFD 、BCNF5、当B 属性函数依赖于A 属性时,属性A 与B 的联系是( )A 、1对多B 、多对1C 、多对多D 、以上都不是6、在关系模式中,如果属性A 和B 存在1对1的联系,则说( )A 、A B B 、B A C 、A B D 、以上都不是7、关系模式中,满足2NF 的模式,( )A 、可能是1NFB 、必定是1NFC 、必定是3NFD 、必定是BCNF8、关系模式R 中的属性全部是主属性,则R 的最高范式必定是( )A 、2NFB 、3NFC 、BCNFD 、4NF9、关系模式的候选关键字可以有( c ),主关键字有( 1个 )A 、0个B 、1个C 、1个或多个D 、多个10、如果关系模式R 是BCNF 范式,那么下列说法不正确的是( )。

A 、R 必是3NFB 、R 必是1NFC 、R 必是2NFD 、R 必是4NF11、图4.5中给定关系R ( )。

A 、不是3NFB 、是3NF 但不是2NFC 、是3NF 但不是BCNFD 、是BCNF12、设有如图4.6所示的关系R ,它是( )A 、1NFB 、2NFC 、3NFD 、4NF二、 填空题1、如果模式是BCNF ,则模式R 必定是(3NF ),反之,则( 不一定 )成立。

(完整版)数据库系统基础教程第五章答案

(完整版)数据库系统基础教程第五章答案

Exercise 5.1.1 As a set:speed2.662.101.422.803.202.202.001.863.06 Average = 2.37 As a bag:speed2.662.101.422.803.203.202.202.202.002.801.862.803.06 Average = 2.48 Exercise 5.1.2 As a set:hd25080320200300160 Average = 218 As a bag:hd2502508025025032020025025030016016080 Average = 215 Exercise 5.1.3a As a set:bore15161418As a bag:bore1516141615151418Exercise 5.1.3bπbore(Ships Classes)Exercise 5.1.4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union of bags R and S will have tuple t appear n + m times. The further union of bag T with the tuple t appearing o times will have tuple t appear n + m + o times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the union of bags R and S will have tuple t appear m + o times. The further union of bag R with the tuple t appearing n times will have tuple t appear m + o + n times in the final result.For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result. Since we cannot have duplicates, the result only has at most one copy of the tuple t.Exercise 5.1.4bFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the intersectionof bags R and S will have tuple t appear min( n, m ) times. The further intersection of bag T with the tuple t appearing o times will produce tuple t min( o, min( n, m ) ) times in the final result.On the right-hand side:Given bags S and T where a tuple t appears m and o times respectively, the intersection of bags R and S will have tuple t appear min( m, o ) times. The further intersection of bag R with the tuple t appearing n times will produce tuple t min( n, min( m, o ) ) times in thefinal result.The intersection of bags R,S and T will yield a result where tuple t appears min( n,m,o ) times. For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set. The tuple t only appears in the result if all the sets have the tuple t. Otherwise, the tuple t will not appear in the result.Exercise 5.1.4cFor bags:On the left-hand side:Given that tuple r in R, which appears m times, can successfully join with tuple s in S,which appears n times, we expect the result to contain mn copies. Also given that tuple tin T, which appears o times, can successfully join with the joined tuples of r and s, weexpect the final result to have mno copies.On the right-hand side:Given that tuple s in S, which appears n times, can successfully join with tuple t in T,which appears o times, we expect the result to contain no copies. Also given that tuple rin R, which appears m times, can successfully join with the joined tuples of s and t, weexpect the final result to have nom copies.The order in which we perform the natural join does not matter for bags.For sets:This is a similar case when dealing with bags except the joined tuples can only appear at most once in each result. If there are tuples r,s,t in relations R,S,T that can successfully join, then the result will contain a tuple with the schema of their joined attributes.Exercise 5.1.4dFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the union of these two bags R ⋃ S, tuple t would appear n + m times. Likewise, in the union of these two bags S ⋃ R, tuple t would appear m + n times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in both sets R and S will the union R ⋃ S have the tuple t. The same reasoning holds when we take the union S ⋃ R.Therefore the commutative law for union holds.Exercise 5.1.4eFor bags:Suppose a tuple t occurs n and m times in bags R and S respectively. In the intersection of these two bags R ∩ S, tuple t would appear min( n,m ) times. Likewise in the intersection of these two bags S ∩ R, tuple t would appear min( m,n ) times. Both sides of the relation yield the same result.For sets:A tuple t can only appear at most one time. Tuple t might appear each in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t appears in at least one of the sets R and S will the intersection R ∩ S have the tuple t. The same reasoning holds when we take the intersection S ∩R.Therefore the commutative law for intersection holds.Exercise 5.1.4fFor bags:Suppose a tuple t occurs n times in bag R and tuple u occurs m times in bag S. Suppose also that the two tuples t,u can successfully join. Then in the natural join of these two bags R S, the joined tuple would appear nm times. Likewise in the natural join of these two bags S R, the joined tuple would appear mn times. Both sides of the relation yield the same result.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuples u,v might appear respectively in sets R and S one or zero times. The combinations of number of occurrences for tuples u,v in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple u exists in Rand tuple v exists in S will the natural join R S have the joined tuple. The same reasoning holds when we take the natural join S R.Therefore the commutative law for natural join holds.Exercise 5.1.4gFor bags:Suppose tuple t appears m times in R and n times in S. If we take the union of R and S first, we will get a relation where tuple t appears m + n times. Taking the projection of a list of attributes L will yield a resulting relation where the projected attributes from tuple t appear m + n times. If we take the projection of the attributes in list L first, then the projected attributes from tuple t would appear m times from R and n times from S. The union of these resulting relations would have the projected attributes of tuple t appear m + n times.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R and S one or zero times. The combinations of number of occurrences for tuple t in R and S respectively are (0,0), (0,1), (1,0), and (1,1). Only when tuple t exists in R or S (or both R and S) will the projected attributes of tuple t appear in the result.Therefore the law holds.Exercise 5.1.4hFor bags:Suppose tuple t appears u times in R, v times in S and w times in T. On the left hand side, the intersection of S and T would produce a result where tuple t would appear min(v , w) times. With the addition of the union of R, the overall result would have u + min(v , w) copies of tuple t. On the right hand side, we would get a result of min(u + v, u + w) copies of tuple t. The expressions on both the left and right sides are equivalent.For sets:An arbitrary tuple t can only appear at most one time in any set. Tuple t might appear in sets R,S and T one or zero times. The combinations of number of occurrences for tuple t in R, S and T respectively are (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0) and (1,1,1). Only when tuple t appears in R or in both S and T will the result have tuple t.Therefore the distributive law of union over intersection holds.Exercise 5.1.4iSuppose that in relation R, u tuples satisfy condition C and v tuples satisfy condition D. Suppose also that w tuples satisfy both conditions C and D where w≤ min(v , w). Then the left hand side will return those w tuples. On the right hand side, σC(R) produces u tuples and σD(R) produces v tuples. However, we know the intersection will produce the same w tuples in the result.When considering bags and sets, the only difference is bags allow duplicate tuples while sets only allow one copy of the tuple. The example above applies to both cases.Therefore the law holds.Exercise 5.1.5aFor sets, an arbitrary tuple t appears on the left hand side if it appears in both R,S and not in T. The same is true for the right hand side.As an example for bags, suppose that tuple t appears one time each in both R,T and two times in S. The result of the left hand side would have zero copies of tuple t while the right hand side would have one copy of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5bFor sets, an arbitrary tuple t appears on the left hand side if it appears in R and either S or T. This is equivalent to saying tuple t only appears when it is in at least R and S or in R and T. The equivalence is exactly the right side’s expression.As an example for bags, suppose that tuple t appears one time in R and two times each in S and T. Then the left hand side would have one copy of tuple t in the result while the right hand side would have two copies of tuple t.Therefore the law holds for sets but not for bags.Exercise 5.1.5cFor sets, an arbitrary tuple t appears on the left hand side if it satisfies condition C, condition D or both condition C and D. On the right hand side, σC(R) selects those tuples that satisfy condition C while σD(R) selects those tuples that satisfy condition D. However, the union operator will eliminate duplicate tuples, namely those tuples that satisfy both condition C and D. Thus we are ensured that both sides are equivalent.As an example for bags, we only need to look at the union operator. If there are indeed tuples that satisfy both conditions C and D, then the right hand side will contain duplicate copies of those tuples. The left hand side, however, will only have one copy for each tuple of the original set of tuples.A+B A2B210154910164167916 Exercise 5.2.1bB+1C-1103334431143 Exercise 5.2.1cA B0101232434 Exercise 5.2.1dB C010224253434A B01232434 Exercise 5.2.1fB C0124253402 Exercise 5.2.1gA SUM(B)022734 Exercise 5.2.1hB AVG(C)0 1.52 4.534 Exercise 5.2.1iA23Exercise 5.2.1jA MAX(C)24 Exercise 5.2.1kA B C23423401┴01┴24┴34┴Exercise 5.2.1lA B C234234┴01┴24┴25┴02 Exercise 5.2.1mA B C23423401┴01┴24┴34┴┴01┴24┴25┴02Exercise 5.2.1nA R.B S.B C0124012501340134012401250134013423┴┴24┴┴34┴┴┴┴01┴┴02Exercise 5.2.2aApplying the δ operator on a relation with no duplicates will yield the same relation. Thus δ is idempotent.Exercise 5.2.2bThe result of πL is a relation over the list of attributes L. Performing the projection again will return the same relation because the relation only contains the list of attributes L. Thus πL is idempotent.Exercise 5.2.2cThe result of σC is a relation where condition C is satisfied by every tuple. Performing the selection again will return the same relation because the relation only contains tuples that satisfy the condition C. Thus σC is idempotent.Exercise 5.2.2dThe result of γL is a relation whose schema consists of the grouping attributes and the aggregated attributes. If we perform the same grouping operation, there is no guarantee that the expression would make sense. The grouping attributes will still appear in the new result. However, the aggregated attributes may or may not appear correctly. If the aggregated attribute is given a different name than the original attribute, then performing γL would not make sense because it contains an aggregation for an attribute name that does not exist. In this case, the resultingrelation would, according to the definition, only contain the grouping attributes. Thus, γL is not idempotent.Exercise 5.2.2eThe result of τ is a sorted list of tuples based on some attributes L. If L is not the entire schema of relation R, then there are attributes that are not sorted on. If in relation R there are two tuples that agree in all attributes L and disagree in some of the remaining attributes not in L, then it is arbitrary as to which order these two tuples appear in the result. Thus, performing the operation τmultiple times can yield a different relation where these two tuples are swapped. Thus, τ is not idempotent.Exercise 5.2.3If we only consider sets, then it is possible. We can take πA(R) and do a product with itself. From this product, we take the tuples where the two columns are equal to each other.If we consider bags as well, then it is not possible. Take the case where we have the two tuples (1,0) and (1,0). We wish to produce a relation that contains tuples (1,1) and (1,1). If we use the classical operations of relational algebra, we can either get a result where there are no tuples or four copies of the tuple (1,1). It is not possible to get the desired relation because no operation can distinguish between the original tuples and the duplicated tuples. Thus it is not possible to get the relation with the two tuples (1,1) and (1,1).Exercise 5.3.1a)Answer(model) ← PC(model,speed,_,_,_) AND speed ≥ 3.00b)Answer(maker) ← Laptop(model,_,_,hd,_,_) AND Product(maker,model,_) AND hd ≥100c)Answer(model,price) ← PC(model,_,_,_,price) AND Product(maker,model,_) ANDmaker=’B’Answer(model,price) ← Laptop(model,_,_,_,_,price) AND Product(maker,model,_)AND maker=’B’Answer(model,price) ← Printer(model,_,_,price) AND Product(maker,model,_) ANDmaker=’B’d)Answer(model) ← Printer(model,color,type,_) AND color=’true’ AND type=’laser’e)PCMaker(maker) ← Product(maker,_,type) AND type=’pc’LaptopMaker(maker) ← Product(maker,_,type) AND type=’laptop’Answer(maker) ← LaptopMaker(maker) AND NOT PCMaker(maker)f)Answer(hd) ← PC(model1,_,_,hd,_) AND PC(model2,_,_,hd,_) AND model1 <>model2g)Answer(model1,model2) ← PC(model1,speed, ram,_,_) ANDPC(model2,_speed,ram,_,_) AND model1 < model2h)FastComputer(model) ← PC(model,speed,_,_,_) AND speed ≥ 2.80FastComputer(model) ← Laptop(model,speed,_,_,_,_) AND speed ≥ 2.80Answer(maker) ← Product(maker,model1,_) AND Product(maker,model2,_) ANDFastComputer(model1) AND FastComputer(model2) AND model1 <> model2i)Computers(model,speed) ← PC(model,speed,_,_,_)Computers(model,speed) ← Laptop(model,speed,_,_,_,_)SlowComputers(model) ← Computers(model,speed) AND Computers(model1,speed1) AND speed < speed1FastestComputers(model) ← Computers(model,_) AND NOT SlowComputers(model)Answer(maker) ← FastestComputers(model) AND Product(maker,model,_) j)PCs(maker,speed) ← PC(model,speed,_,_,_) AND Product(maker,model,_) Answer(maker) ← PCs(maker,speed) AND PCs(maker,speed1) ANDPCs(maker,speed2) AND speed <> speed1 AND speed <> speed2 AND speed1 <>speed2k)PCs(maker,model) ← Product(maker,model,type) AND type=’pc’Answer(maker) ← PCs(maker,model) AND PCs(maker,model1) ANDPCs(maker,model2) AND PCs(maker,model3) AND model <> model1 AND model <> model2 AND model1 <> model2 AND (model3 = model OR model3 = model1 ORmodel3 = model2)Exercise 5.3.2a)Answer(class,country) ← Classes(class,_,country,_,bore,_) AND bore ≥ 16b)Answer(name) ← Ships(name,_,launched) AND launched < 1921c)Answer(ship) ← Outcomes(ship,battle,result) AND battle=’Denmark Strait’ AND result= ‘sunk’d)Answer(name) ← Classes(class,_,_,_,_,displacement) AND Ships(name,class,launched)AND displacement > 35000 AND launched > 1921e)Answer(name,displacement,numGuns) ← Classes(class,_,_,numGuns,_,displacement)AND Ships(name,class,_) AND Outcomes (ship,battle,_) AND battle=’Guadalcanal’AND ship=namef)Answer(name) ← Ships(name,_,_)Answer(name) ← Outcomes(name,_,_) AND NOT Answer(name)g)MoreThanOne(class) ← Ships(name,class,_) AND Ships(name1,class,_) AND name <>name1Answer(class) ← Classes(class,_,_,_,_,_) AND NOT MoreThanOne(class)h)Battleship(country) ← Classes(_,type,country,_,_,_) AND type=’bb’Battlecruiser(country) ← Classes(_,type,country,_,_,_) AND type=’bc’Answer(country) ← Battleship(country) AND Battlecruiser(country)i)Results(ship,result,date) ← Battles(name,date) AND Outcomes(ship,battle,result) ANDbattle=nameAnswer(ship) ← Results(ship,result,date) AND Results(ship,_,date1) ANDresult=’damaged’ AND date < date1Exercise 5.3.3Answer(x,y) ← R(x,y) AND z = zExercise 5.4.1aAnswer(a,b,c) ← R(a,b,c)Answer(a,b,c) ← S(a,b,c)Exercise 5.4.1bAnswer(a,b,c) ← R(a,b,c) AND S(a,b,c)Exercise 5.4.1cAnswer(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)Exercise 5.4.1dUnion(a,b,c) ← R(a,b,c)Union(a,b,c) ← S(a,b,c)Answer(a,b,c) ← Union(a,b,c) AND NOT T(a,b,c)Exercise 5.4.1eJ(a,b,c) ← R(a,b,c) AND NOT S(a,b,c)K(a,b,c) ← R(,a,b,c) AND NOT T(a,b,c)Answer(a,b,c) ← J(a,b,c) AND K(a,b,c)Exercise 5.4.1fAnswer(a,b) ← R(a,b,_)Exercise 5.4.1gJ(a,b) ← R(a,b,_)K(a,b) ← S(_,a,b)Answer(a,b) ← J(a,b) AND K(a,b)Exercise 5.4.2aAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2bAnswer(x,y,z) ← R(x,y,z) AND x < y AND y < z Exercise 5.4.2cAnswer(x,y,z) ← R(x,y,z) AND x < yAnswer(x,y,z) ← R(x,y,z) AND y < zExercise 5.4.2dChange:NOT(x < y OR x > y)To:x ≥ y AND x ≤ yThe above simplifies to x = yAnswer(x,y,z) ← R(x,y,z) AND x = yExercise 5.4.2eChange:NOT((x < y OR x > y) AND y < z)NOT(x < y OR x > y) OR y ≥ z(x ≥ y AND x ≤ y) OR y ≥ zTo:x = y OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x = yAnswer(x,y,z) ← R(x,y,z) AND y ≥ zExercise 5.4.2fChange:NOT((x < y OR x < z) AND y < z)NOT(x < y OR x < z) OR y ≥ z To:(x ≥ y AND x ≥ z) OR y ≥ zAnswer(x,y,z) ← R(x,y,z) AND x ≥ y AND x ≥ zAnswer(x,y,z) ← R(x,y,z) AND y ≥zExercise 5.4.3aAnswer(a,b,c,d) ← R(a,b,c) AND S(b,c,d)Exercise 5.4.3bAnswer(b,c,d,e) ← S(b,c,d) AND T(d,e)Exercise 5.4.3cAnswer(a,b,c,d,e) ← R(a,b,c) AND S(b,c,d) AND T(d,e)Exercise 5.4.4a)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syb)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < sy AND ry < szc)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx < syAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry < szd)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = sye)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx = syAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szf)Answer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND rx ≥ sy AND rx ≥ szAnswer(rx,ry,rz,sx,sy,sz) ← R(rx,ry,rz) AND S(sx,sy,sz) AND ry ≥ szExercise 5.4.5aR1 := πx,y(Q R)Exercise 5.4.5bR1 := ρR1(x,z)(Q)R2 := ρR2(z,y)(Q)R3 := πx,y(R1 (R1.z = R2.z) R2)Exercise 5.4.5cR1 := πx,y(Q R)R2 := σx < y(R1)。

数据库应用基础第5章习题参考答案

数据库应用基础第5章习题参考答案

1.选择题(1)SQL 语言中,删除一个视图的命令是( B )。

A. DELETEB. DROPC. CLEARD. REMOVE(2)建立索引的作用之一是 ( D )。

A . 节省存储空间 B. 便于管理C . 提高查询速度 D. 提高查询和更新的速度(3)以下关于主索引和候选索引的叙述正确的是 ( C )。

A .主索引和候选索引都能保证表记录的惟一性B .主索引和候选索引都可以建立在数据库表和自由表上C .主索引可以保证表记录的惟一性,而候选索引不能D .主索引和侯选索引是相同的概念(4)在数据库设计器中,不能完成的操作是( )。

A .创建数据表关联BC .修改关联中的主键表和外键表D .删除关联 (5)下面所列条目中,( C )不是标准的SQL 语句。

A. ALTER TABLE B. CREATE TABLE C. ALTER VIEW D. CREATE VIEW2.填空题(1)索引是数据库中一种特殊类型的对象,它与( 数据库表 )有着紧密的关系。

(2)在数据库中,索引使数据库程序无需对整个表进行( 扫描 ),就可以在其中找到所需数据。

(3)在SQL Server 2000中可创建3种类型的索引,即惟一性索引、( 主键索引 )和聚集索引。

(4)视图是一个( 虚拟表 ),并不包含任何的物理数据。

(5)视图属性包括视图( 视图名称、权限、所有者、创建日期 )和用于创建视图的文本等几个方面。

3.问答题(1)聚集索引与非聚集索引之间有哪些不同点?在一个表中是否可以建立多少个聚集索引和非聚集索引?答:在建立了聚集索引的基本表中,表中各记录的物理顺序与索引键值的逻辑顺序相同;数据表中数据更改后需要对记录重新物理排序。

而在只建立了非聚集索引的表中,记录的物理顺序不一定与索引键值保持一致;数据表中数据更改后,不需要对表中记录重新排序,只需要更新对应的索引即可。

一个基本表中只能建立一个聚集索引,但可以建立多个非聚集索引。

数据库第五章课后习题答案

数据库第五章课后习题答案

数据库第五章课后习题答案关系规范化理论题⽬4.20 设关系模式R(ABC),F是R上成⽴的FD集,F={B→A,C→A },ρ={AB,BC }是R上的⼀个分解,那么分解ρ是否保持FD集F?并说明理由。

答:已知F={ B→A,C→A },⽽πAB(F)={ B→A },πBC(F)=φ,显然,分解ρ丢失了FD C→A。

4.21 设关系模式R(ABC),F是R上成⽴的FD集,F={B→C,C→A },那么分解ρ={AB,AC }相对于F,是否⽆损分解和保持FD?并说明理由。

答:①已知F={ B→C,C→A },⽽πAB(F)=φ,πAC(F)={ C→A }显然,这个分解丢失了FD B→C②⽤测试过程可以知道,ρ相对于F是损失分解。

4.22 设关系模式R(ABCD),F是R上成⽴的FD集,F={A→B,B→C,A→D,D→C },ρ={AB,AC,BD }是R的⼀个分解。

①相对于F,ρ是⽆损分解吗?为什么?②试求F在ρ的每个模式上的投影。

③ρ保持F吗?为什么?答:①⽤测试过程可以知道,ρ相对于F是损失分解。

②πAB(F)={ A→B },πAC(F)={ A→C },πBD(F)=φ。

③显然,分解ρ不保持FD集F,丢失了B→C、A→D和D→C等三个FD。

4.23设关系模式R(ABCD),R上的FD集F={A→C,D→C,BD→A},试说明ρ={AB,ACD,BCD }相对于F是损失分解的理由。

答:据已知的F集,不可能把初始表格修改为有⼀个全a⾏的表格,因此ρ相对于F是损失分解。

4.24 设关系模式R(ABCD)上FD集为F,并且F={A→B,B→C,D→B}。

① R分解成ρ={ACD,BD},试求F在ACD和BD上的投影。

② ACD和BD是BCNF吗?如不是,望分解成BCNF。

解:① F在模式ACD上的投影为{A→C,D→C},F在模式BD上的投影为{D→B}。

②由于模式ACD的关键码是AD,因此显然模式ACD不是BCNF。

数据库复习 第五章 习题

数据库复习 第五章  习题

第五章习题一、选择题:1.关系规范化中的删除操作异常是指①,插入操作异常是指②。

A.不该删除的数据被删除B.不该插入的数据被插入C.应该删除的数据未被删除D.应该插入的数据未被插入答案:①A ②D2.设计性能较优的关系模式称为规范化,规范化主要的理论依据是____。

A.关系规范化理论B.关系运算理论C.关系代数理论D.数理逻辑答案:A3.规范化理论是关系数据库进行逻辑设计的理论依据。

根据这个理论,关系数据库中的关系必须满足:其每一属性都是____。

A.互不相关的B.不可分解的C.长度可变的D.互相关联的答案:B4.关系数据库规范化是为解决关系数据库中____问题而引人的。

A.插入、删除异常和数据冗余B.提高查询速度C.减少数据操作的复杂性D.保证数据的安全性和完整性答案:A5.规范化过程主要为克服数据库逻辑结构中的插入异常,删除异常以及____的缺陷。

A.数据的不一致性B.结构不合理C.冗余度大D.数据丢失答案:C6.当关系模式R(A,B)已属于3NF,下列说法中____是正确的。

A.它一定消除了插入和删除异常B.仍存在一定的插入和删除异常C.一定属于BCNF D.A和C都是答案:B7.关系模型中的关系模式至少是____。

A.1NF B.2NF C.3NF D.BCNF答案:A8.在关系DB中,任何二元关系模式的最高范式必定是____。

A.1NF B.2NF C.3NF D.BCNF答案:D9.在关系模式R中,若其函数依赖集中所有候选关键宇都是决定因素,则R最高范式是____。

A.2NF B.3NF C.4 NF D.BCNF答案:C10.当B属性函数依赖于A属性时,属性A与B的联系是____。

A.1对多B.多对1 C.多对多D.以上都不是答案:B11.在关系模式中,如果属性A和B存在1对1的联系,则说____。

A.A→B B.B→A C.A↔B D.以上都不是答案:C12.候选码中的属性称为____。

数据库原理与应用 第5章答案解析肖海蓉、任民宏

数据库原理与应用 第5章答案解析肖海蓉、任民宏

第5 章网络数据库管理系统SQL Server 2012课后习题参考答案1、简答题(1)简述组成SQL Server 2012 数据库的三种类型的文件。

答:SQL Server 数据库文件根据其作用的不同,可以分为主数据文件、次数据文件、事务日志文件3 种类型。

①主数据文件(primary file):主数据文件是数据库的起点,指向数据库文件的其他部分。

主数据文件是用来存放数据和数据库的初始化(启动)信息和部分或全部数据,是SQL Server 数据库的主体,它是每个数据库不可缺少的部分,每个数据库有且仅有一个主数据文件,用户数据和对象也可以存储在此文件中,主数据文件的文件扩展名为.mdf。

②次数据文件(secondary file):用来存储主数据文件没有存储的其他数据和对象。

如果数据库中的数据量很大,除了将数据存储在主数据文件中以外,还可以将一部分数据存储在次数据文件中;如果主数据文件足够大,能够容纳数据库中的所有数据,则该数据库不需要次数据文件。

使用次数据文件是因为数据量太过庞大,可以将数据分散存储在多个不同磁盘上以方便进行管理、提高读取速度。

次数据文件的扩展名为.ndf。

③事务日志文件(transaction log file):用来记录数据库更新情况的文件,SQL Server 2012具有事务功能,可以保证数据库操作的一致性和完整性,用事务日志文件来记录所有事务及每个事务对数据库进行的插入、删除和更新操作。

事务日志是数据库的重要组件,如果数据库遭到破坏,可以根据事务日志文件分析出错的原因;如果数据丢失,可以使用事务日志恢复数据库内容。

每个数据库至少拥有一个事务日志文件,也可以拥有多个日志文件。

事务日志文件的文件扩展名为.ldf。

(2)SQL Server 2012 有哪些系统数据库,它们的作用是什么?SQL Server 2012 中主要包括master、model、tempdb 和msdb 四个系统数据库。

数据库设计第五章(高教版)含课后答案

数据库设计第五章(高教版)含课后答案

数据库设计第五章(⾼教版)含课后答案第5章数据库设计与ER模型5.1 基本内容分析5.1.1 本章重要概念(1)DBS⽣存期及其7个阶段的任务和⼯作,DBD过程的输⼊和输出。

(2)概念设计的重要性、主要步骤。

逻辑设计阶段的主要步骤。

(3)ER模型的基本元素,属性的分类,联系的元数、连通词、基数。

采⽤ER⽅法的概念设计步骤。

(4)ER模型到关系模型的转换规则。

采⽤ER⽅法的逻辑设计步骤。

(5)ER模型的扩充:弱实体,超类和⼦类。

5.1.2 本章的重点篇幅(1)教材中P193-194的转换规则和实例。

(2)教材中P196-200的四个ER模型实例。

5.1.3 对ER模型的理解ER模型是⼈们认识客观世界的⼀种⽅法、⼯具。

ER模型具有客观性和主观性两重含义。

ER模型是在客观事物或系统的基础上形成的,在某种程度上反映了客观现实,反映了⽤户的需求,因此ER模型具有客观性。

但ER模型⼜不等同于客观事物的本⾝,它往往反映事物的某⼀⽅⾯,⾄于选取哪个⽅⾯或哪些属性,如何表达则决定于观察者本⾝的⽬的与状态,从这个意义上说,ER模型⼜具有主观性。

ER模型的设计过程,基本上是两⼤步:·先设计实体类型(此时不要涉及到“联系”);·再设计联系类型(考虑实体间的联系)。

具体设计时,有时“实体”与“联系”两者之间的界线是模糊的。

数据库设计者的任务就是要把现实世界中的数据以及数据间的联系抽象出来,⽤“实体”与“联系”来表⽰。

另外,设计者应注意,ER模型应该充分反映⽤户需求,ER模型要得到⽤户的认可才能确定下来。

5.2 教材中习题5的解答5.1名词解释(1)·软件⼯程:研究如何⽤科学知识、⼯程⽅⾯的纪律指导软件开发的过程,以提⾼软件质量和开发效率,降低开发成本,这样的⼀门学科称为“软件⼯程”。

·软件⽣存期:软件⽣存期是指从软件的规划、研制、实现、投⼊运⾏后的维护,直到它被新的软件所取代⽽停⽌使⽤的整个期间。

数据库第5章习题参考答案

数据库第5章习题参考答案

第5章习题解答1.选择题(1)为数据表创建索引的目的是_______。

A.提高查询的检索性能B.节省存储空间C.便于管理D.归类(2)索引是对数据库表中_______字段的值进行排序。

A.一个B.多个C.一个或多个D.零个(3)下列_______类数据不适合创建索引。

A.经常被查询搜索的列B.主键的列C.包含太多NULL值的列D.表很大(4)有表student(学号, 姓名, 性别, 身份证号, 出生日期, 所在系号),在此表上使用_______语句能创建建视图vst。

A.CREATE VIEW vst AS SELECT * FROM studentB.CREATE VIEW vst ON SELECT * FROM studentC.CREATE VIEW AS SELECT * FROM studentD.CREATE TABLE vst AS SELECT * FROM student(5)下列_______属性不适合建立索引。

A.经常出现在GROUP BY字句中的属性B.经常参与连接操作的属性C.经常出现在WHERE字句中的属性D.经常需要进行更新操作的属性(6)下面关于索引的描述不正确的是_______。

A.索引是一个指向表中数据的指针B.索引是在元组上建立的一种数据库对象C.索引的建立和删除对表中的数据毫无影响D.表被删除时将同时删除在其上建立的索引(7)SQL的视图是_______中导出的。

A.基本表B.视图C.基本表或视图D.数据库(8)在视图上不能完成的操作是_______。

A.更新视图数据B.查询C.在视图上定义新的基本表D.在视图上定义新视图(9)关于数据库视图,下列说法正确的是_______。

A.视图可以提高数据的操作性能B.定义视图的语句可以是任何数据操作语句C.视图可以提供一定程度的数据独立性D.视图的数据一般是物理存储的(10)在下列关于视图的叙述中,正确的是_______。

数据库原理及应用第5章课后习题答案

数据库原理及应用第5章课后习题答案

习题51、 理解并给出下列术语的定义:1)设R(U)是一个属性集U 上的关系模式,X 和Y 是U 的子集。

若对于R(U)的任意一个可能的关系r ,r 中不可能存在两个元组在X 上的属性值相等, 而在Y 上的属性值不等, 则称 X 函数确定Y 或 Y 函数依赖于X ,记作X →Y 。

2) 完全函数依赖在R(U)中,如果X →Y ,并且对于X 的任何一个真子集X ’,都有Y 不函数依赖于X ’ ,则称Y 对X 完全函数依赖,记作Y X F −→−3) 部分函数依赖若X →Y ,但Y 不完全函数依赖于X ,则称Y 对X 部分函数依赖,记作Y X p −→−4) 传递函数依赖在R(U)中,如果X →Y ,(Y ⊆X) , Y →X ,Y →Z , 则称Z 对X 传递函数依赖。

记为:Z X T−→−注: 如果Y →X , 即X ←→Y ,则Z 直接依赖于X 。

5)候选码设K 为R (U,F )的属性或属性组合。

若U K F →, 则K 称为R 的侯选码。

6)主码:若候选码多于一个,则选定其中的一个作为主码。

7)外码:关系模式 R 中属性或属性组X 并非 R 的码,但 X 是另一个关系模式的码,则称 X 是R 的外部码(Foreign key )也称外码8)如果一个关系模式R 的所有属性都是不可分的基本数据项,则R ∈1NF.9)若R ∈1NF ,且每一个非主属性完全函数依赖于码,则R ∈2NF 。

10)如果R(U,F )∈2NF ,并且所有非主属性都不传递依赖于主码,则R(U,F )∈3NF 。

11)关系模式R (U ,F )∈1NF ,若X →Y 且Y ⊆ X 时X 必含有码,则R (U ,F ) ∈BCNF 。

12)关系模式R<U ,F>∈1NF ,如果对于R 的每个非平凡多值依赖X →→Y (Y ⊆ X ),X 都含有码,则R ∈4NF 。

2、 关系规范化的操作异常有哪些?1) 数据冗余大2) 插入异常3) 删除异常4) 更新异常3、 第一范式、第二范式和第三范式关系的关系是什么?4、 已知关系模式R(A,B,C,D,E)及其上的函数依赖集合F={A->D,B->C,E-> A},该关系模式的候选码是什么?候选码为:(E,B)5、 已知学生表(学号,姓名,性别,年龄,系编号,系名称),存在的函数依赖集合是{学号->姓名,学号->性别,学号->年龄,学号->系编号,系编号->系名称},判断其满足第几范式。

《数据库技术与应用》第5章 习题答案

《数据库技术与应用》第5章 习题答案

第5章数据库完整性与安全性1.什么是数据库的完整性?什么是数据库的安全性?两者之间有什么区别和联系?解:数据库的完整性是指数据库中数据的正确性、有效性和相容性,其目的是防止不符合语义、不正确的数据进入数据库,从而来保证数据库系统能够真实的反映客观现实世界。

数据库安全性是指保护数据库,防止因用户非法使用数据库造成数据泄露、更改或破坏。

数据的完整性和安全性是两个不同的概念,但是有一定的联系: 前者是为了防止数据库中存在不符合语义的数据,防止错误信息的输入和输出,即所谓垃圾进单位、束。

静态元组①INSERT③)执行该操作,或级连(CASCADE)执行其它操作,进行违约处理以保证数据的完整性。

4.现有以下四个关系模式:供应商(供应商编号,姓名,电话,地点),其中供应商编号为主码;零件(零件编号,零件名称,颜色,重量),其中零件编号为主码;工程(工程编号,工程名称,所在地点),其中工程编号为主码;供应情况(供应商编号,零件编号,工程编号,数量),其中供应商编号,零件编号,工程编号为主码用SQL语句定义这四个关系模式,要求在模式中完成以下完整性约束条件的定义:①定义每个模式的主码;②定义参照完整性;③定义零件重量不得超过100千克。

解:CREATESCHEMASupplier_schemaCREATETABLESupplier(SnoCHAR(5)PRIMARYKEY,SnameCHAR(20)NOTNULL,PhoneCHAR(13),AddressCHAR(30));5.在关系数据库系统中,当操作违反实体完整性、参照完整性和用户自定义的完整性约束条件时,一般是如何分别进行处理的。

解:(1)按实体完整性规则自动进行检查。

包括:①检查主码值是否唯一,如果不唯一则拒绝插入或修改。

②检查主码的各个属性是否为空,只要有一个为空就拒绝插入或修改。

(2)按参照完整性检查,违约处理的策略如下:①拒绝(NOACTION)执行。

数据库系统基础教程第五章答案

数据库系统基础教程第五章答案

Exercise 5.1.1 As a set:Average = 2。

37 As a bag:Average = 2.48 Exercise 5。

1。

2Average = 218 As a bag:Average = 215 Exercise 5.1。

3a As a set:18As a bag:bore1516141615151418Exercise 5。

1.3bπbore(Ships Classes)Exercise 5.1。

4aFor bags:On the left-hand side:Given bags R and S where a tuple t appears n and m times respectively, the union ofbags R and S will have tuple t appear n + m times。

The further union of bag T with the tuple t appearing o times will have tuple t appear n + m + o times in the final result。

On the right—hand side:Given bags S and T where a tuple t appears m and o times respectively, the union of bags R and S will have tuple t appear m + o times. The further union of bag R with the tuple tappearing n times will have tuple t appear m + o + n times in the final result。

For sets:This is a similar case when dealing with bags except the tuple t can only appear at most once in each set。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

关系规范化理论题目4.20 设关系模式R(ABC),F是R上成立的FD集,F={ B→A,C→A },ρ={ AB,BC }是R上的一个分解,那么分解ρ是否保持FD集F?并说明理由。

答:已知F={ B→A,C→A },而πAB (F)={ B→A },πBC(F)=φ,显然,分解ρ丢失了FD C→A。

4.21 设关系模式R(ABC),F是R上成立的FD集,F={ B→C,C→A },那么分解ρ={ AB,AC }相对于F,是否无损分解和保持FD?并说明理由。

答:①已知F={ B→C,C→A },而πAB (F)=φ,πAC(F)={ C→A }显然,这个分解丢失了FD B→C②用测试过程可以知道,ρ相对于F是损失分解。

4.22 设关系模式R(ABCD),F是R上成立的FD集,F={ A→B,B→C,A→D,D→C },ρ={ AB,AC,BD }是R的一个分解。

①相对于F,ρ是无损分解吗?为什么?②试求F在ρ的每个模式上的投影。

③ρ保持F吗?为什么?答:①用测试过程可以知道,ρ相对于F是损失分解。

②πAB (F)={ A→B },πAC(F)={ A→C },πBD(F)=φ。

③显然,分解ρ不保持FD集F,丢失了B→C、A→D和D→C等三个FD。

4.23 设关系模式R(ABCD),R上的FD集F={ A→C,D→C,BD→A},试说明ρ={ AB,ACD,BCD }相对于F是损失分解的理由。

答:据已知的F集,不可能把初始表格修改为有一个全a行的表格,因此ρ相对于F是损失分解。

4.24 设关系模式R(ABCD)上FD集为F,并且F={A→B,B→C,D→B}。

① R分解成ρ={ACD,BD},试求F在ACD和BD上的投影。

② ACD和BD是BCNF吗?如不是,望分解成BCNF。

解:① F在模式ACD上的投影为{A→C,D→C},F在模式BD上的投影为{D→B}。

②由于模式ACD的关键码是AD,因此显然模式ACD不是BCNF。

模式ACD应分解成{AC,AD}或{CD,AD}。

但是这个分解不保持FD,丢失了FD D→C或A→C。

另外,模式BD已是BCNF。

4.25设关系模式R(ABCD),ρ={AB,BC,CD}是R的一个分解。

设F1={A→B,B→C},F2={B→C,C→D}。

①如果F1是R上的FD集,此时ρ是否无损分解?若不是,试举出反例。

②如果F2是R上的FD集呢?解:①据chase过程可知,相对于F1,R分解成ρ是损失分解。

据构造初始表的规则,这个反例可以是下面的表格:r A B C D1 1 0 00 1 1 00 0 1 1(r)。

对于这个r而言,显然r≠ mρ②据chase过程可知,相对于F2,R分解成ρ是无损分解。

4.26 设关系模式R(ABCD),F是R上成立的FD集,F={ AB→CD,A→D }。

①试说明R不是2NF模式的理由。

②试把R分解成2NF模式集。

答:①从已知FD集F,可知R的候选键是AB。

另外,AB→D是一个局部依赖,因此R不是2NF模式。

②此时R应分解成ρ={ AD,ABC },ρ是2NF模式集。

4.28 设有关系模式R(职工编号,日期,日营业额,部门名,部门经理),该模式统计商店里每个职工的日营业额,以及职工所在的部门和经理信息。

如果规定:每个职工每天只有一个营业额;每个职工只在一个部门工作;每个部门只有一个经理。

试回答下列问题:(1)根据上述规定,写出模式R的基本FD和关键码;(2)说明R不是2NF的理由,并把R分解成2NF模式集;(3)进而分解成3NF模式集。

解:(1)基本的FD有三个:(职工编号,日期)→日营业额职工编号→部门名部门名→部门经理R的关键码为(职工编号,日期)。

(2)R中有两个这样的FD:(职工编号,日期)→(部门名,部门经理)职工编号→(部门名,部门经理)可见前一个FD是局部依赖,所以R不是2NF模式。

R应分解成R1(职工编号,部门名,部门经理)R2(职工编号,日期,日营业额)此处,R1和R2都是2NF模式。

(3)R2已是3NF模式。

在R1中,存在两个FD:职工编号→部门名部门名→部门经理因此,“职工编号→部门经理”是一个传递依赖,R1不是3NF模式。

R1应分解成R11(职工编号,部门名)R12(部门名,部门经理)这样,ρ= { R11,R12,R2 }是一个3NF模式集。

4.30 设关系模式R(ABCD),在R上有5个相应的FD集及分解:(1)F={ B→C,D→A },ρ={ BC,AD }(2)F={ AB→C,C→A,C→D },ρ={ ACD,BC }(3)F={ A→BC,C→AD },ρ={ ABC,AD }(4)F={ A→B,B→C,C→D },ρ={ AB,ACD }(5)F={ A→B,B→C,C→D },ρ={ AB,AD,CD }试对上述五种情况分别回答下列问题:①确定R的关键码。

②是否是无损分解?③是否保持FD集?④确定ρ中每一模式的范式级别。

解:(1)① R的关键码为BD。

②ρ不是无损分解。

③ρ保持FD集F。

④ρ中每一模式已达到BCNF级别。

(2)① R有两个关键码:AB和BC。

②ρ是无损分解。

③因为πACD (F)={ C→A ,C→D },πBC(F)=φ(没有非平凡的FD),所以ρ不保持FD,丢失了AB→C。

④ρ中两模式均已达到BCNF级别。

(3)① R有两个关键码:A和C②ρ是无损分解。

③因为πABC (F)={ A→BC,C→A },πAD(F)= { A→D },所以ρ保持FD。

④在模式ABC中,关键码是A或BC,属性全是主属性,但有传递依赖(A→BC,BC→A)。

因此模式ABC是3NF,但不是BCNF。

而模式AD显然已是BCNF。

(4)① R的关键码为A。

②ρ是无损分解。

③因为πAB (F)={ A→B },πACD(F)={ A→C,C→D },从这两个依赖集推不出原来的B→C,因此ρ不保持FD,丢失了B→C。

④模式AB是BCNF,模式ACD不是3NF,只达到2NF级别。

(5)① R的关键码为A。

②ρ不是无损分解。

③因为πAB (F)={ A→B },πAD(F)={ A→D },πCD(F)={ C→D },从这三个依赖集推不出原来的B→C,因此ρ不保持FD,丢失了B→C。

④ρ中每个模式均是BCNF级别。

数据库设计题目5.13 设某商业集团数据库中有3个实体集。

一是“商店”实体集,属性有商店编号、商店名、地址等;二是“商品”实体集,属性有商品号、商品名、规格、单价等;三是“职工”实体集,属性有职工编号、姓名、性别、业绩等。

商店与商品间存在“销售”联系,每个商店可销售多种商品,每种商品也可放在多个商店销售,每个商店销售一种商品,有月销售量;商店与职工间存在着“聘用”联系,每个商店有许多职工,每个职工只能在一个商店工作,商店聘用职工有聘期和月薪。

试画出ER图,并在图上注明属性、联系的类型。

再转换成关系模式集,并指出每个关系模式的主键和外键。

解:(1) ER图如图所示。

(2)这个ER图可转换4个关系模式:商店(商店编号,商店名,地址)职工(职工编号,姓名,性别,业绩,商店编号,聘期,月薪)商品(商品号,商品名,规格,单价)销售(商店编号,商品号,月销售量)5.14 设某商业集团数据库中有3个实体集。

一是“公司”实体集,属性有公司编号、公司名、地址等;二是“仓库”实体集,属性有仓库编号、仓库名、地址等;三是“职工”实体集,属性有职工编号、姓名、性别等。

公司与仓库间存在“隶属”联系,每个公司管辖若干仓库,每个仓库只能属于一个公司管辖;仓库与职工间存在“聘用”联系,每个仓库可聘用多个职工,每个职工只能在一个仓库工作,仓库聘用职工有聘期和工资。

试画出ER图,并在图上注明属性、联系的类型。

再转换成关系模式集,并指出每个关系模式的主键和外键。

解:(1) ER图如图所示。

(2)这个ER图可转换3个关系模式:公司(公司编号,公司名,地址)仓库(仓库编号,仓库名,地址,公司编号)职工(职工编号,姓名,性别,仓库编号,聘期,工资)5.15 设某商业集团数据库有3个实体集。

一是“商品”实体集,属性有商品号、商品名、规格、单价等;二是“商店”实体集,属性有商店号、商店名、地址等;三是“供应商”实体集,属性有供应商编号、供应商名、地址等。

供应商与商品之间存在“供应”联系,每个供应商可供应多种商品,每种商品可向多个供应商订购,每个供应商供应每种商品有个月供应量;商店与商品间存在“销售”联系,每个商店可销售多种商品,每种商品可在多个商店销售,每个商店销售每种商品有月计划数。

试画出ER图,并在图上注明属性、联系的类型。

再转换成关系模式集,并指出每个关系模式的主键和外键。

解:(1)ER图如图所示。

(2)这个ER图可转换5个关系模式:供应商(供应商编号,供应商名,地址)商店(商店号,商店名,地址)商品(商品号,商品名,规格,单价)供应(供应商编号,商品号,月供应量)销售(商店号,商品号,月计划数)5.18某体育运动锦标赛有来自世界各国运动员组成的体育代表团参赛各类比赛项目。

试为该锦标赛各个代表团、运动员、比赛项目、比赛情况设计一个ER模型。

解:如下图是ER图的一种设计方案5.19 假设某超市公司要设计一个数据库系统来管理该公司的业务信息。

该超市公司的业务管理规则如下:⑴该超市公司有若干仓库,若干连锁商店,供应若干商品。

⑵每个商店有一个经理和若干收银员,每个收银员只在一个商店工作。

⑶每个商店销售多种商品,每种商品可在不同的商店销售。

⑷每个商品编号只有一个商品名称,但不同的商品编号可以有相同的商品名称。

每种商品可以有多种销售价格。

⑸超市公司的业务员负责商品的进货业务。

试按上述规则设计ER模型解:如下图是ER图的一种设计方案。

5.21图5.30所示为一张交通违章处罚通知书,试根据这张通知书所提供的信息,设计一个ER模式,并将这个ER模式转换成关系数据模型,要求标明主键和外键。

注:一张违章通知书可能有多项罚款,例如警告+罚款。

交通违章通知书 编号:TZ22719图 5.30 交通违章处罚通知书ER 图如下 答:1 1 1NN N司机机动车警察通知书处罚收到涉及经办含有1 N这个ER图有5个实体类型,其属性如下:司机(驾照号,姓名,地址,邮编,电话)机动车(牌照号,型号,制造厂,生产日期)警察(警察编号,姓名)通知书(编号,日期,时间,地址,违章记录)处罚(编号,处罚方式)这个ER图有4个联系类型,都是1:N联系。

根据ER图和转换规则,5个实体类型转换成5个关系模式,如下:司机(驾照号,姓名,地址,邮编,电话)机动车(牌照号,型号,制造厂,生产日期)警察(警察编号,姓名)通知书(编号,日期,时间,地址,违章记录,驾照号,牌照号,警察编号)处罚(编号,处罚方式)(注:文档可能无法思考全面,请浏览后下载,供参考。

相关文档
最新文档