数据库复习 第五章习题
- 格式:doc
- 大小:260.50 KB
- 文档页数:20
1.在视图上不能完成的操作是(C)A.更新视图B.查询C.在视图上定义新的表D.在视图上定义新的视图2.SQL语言中删除一个视图的命令是(B)A.DELETEB.D ROPC.C LEARD.REMOVE3.在SQL语言中的视图VIEW是数据库的(A)A.外模式B.模式C.内模式D.存储模式4.创建存储过程的语句是(C)A.CREA TE STOREB.C REA TE FUNCTIONC.C REA TE PROCEDURED.CREA TE TRIGGER5.执行存储命令的是(B)A.DOB.E XECUTEC.E XED.GO6.创建视图的语句是(A)A.CREA TE VIEWB.CREA TE FUNCTIONC.CREA TE PROCEDURED.CREA TE TRIGGER7.创建触发器的语句是(D)A.CREA TE STOREB.CREA TE FUNCTIONC.CREA TE PROCEDURED.CREA TE TRIGGER8.创建索引的语句是(B)A.CREA TE VIEWB.CREA TE INDEXC.CREA TE PROCEDURED.CREA TE TRIGGER9.下面关于存储过程的描述不正确的是(C)A.存储过程实际上是一组T-SQL语句B.存储过程预先被编译存放在服务器的系统表中C.存储过程独立于数据库而存在D.存储过程可以完成某一特定的业务逻辑10.系统存储过程在系统安装时就已创建,这些存储过程放在(A)系统数据库中A.masterB.tempdbC.modelD.pubs11.带有前缀名为sp_的存储过程属于(B)A.用户自定义的存储过程B.系统存储过程C.扩展存储过程D.以上都不是12.下面函数中不能进行数据类型转换的是(D)A.CONVERTB.STRC.CASTD.LTRIM13.T-SQL中的全局变量以(A)作前缀A.@@B.@C.#D.##14.下面关于唯一索引描述不正确的是(A)A.某列创建了唯一索引则这一列为主键B.不允许插入重复的列值C.某列创建为主键,则该列会自动创建唯一索引D.一个表中可以有多个唯一索引15.以下关于视图的描述中,错误的是(D)A.视图不是真实存在的基础表,而是一张虚表B.当对通过视图看到的数据进行修改时,相应的基本表的数据也要发生变化C.在创建视图时,若其中某个目标列是聚合函数,必须指明视图的全部列名D.在一个语句中,一次可以修改一个以上的视图对应的基表16.在使用T-SQL创建视图时,不能使用的关键字是(A)A.ORDER BYB.W HEREC.A SD.WITH CHECK OPTION17.视图是从(基本表或视图)中导出的表,数据库中实际存放的是视图的(定义)18.触发器和存储过程的区别在于,存储过程的是(命令调用)执行,触发器是(事件)执行的19.视图删除后,只会删除视图(定义),与其相关的基本表数据(不会)受到影响。
第5章数据库完整性与安全性1. 什么是数据库的完整性?什么是数据库的安全性?两者之间有什么区别和联系?解:数据库的完整性是指数据库中数据的正确性、有效性和相容性,其目的是防止不符合语义、不正确的数据进入数据库,从而来保证数据库系统能够真实的反映客观现实世界。
数据库安全性是指保护数据库,防止因用户非法使用数据库造成数据泄露、更改或破坏。
数据的完整性和安全性是两个不同的概念,但是有一定的联系:前者是为了防止数据库中存在不符合语义的数据,防止错误信息的输入和输出,即所谓垃圾进垃圾出所造成的无效操作和错误结果。
后者是保护数据库防止恶意的破坏和非法的存取。
也就是说,安全性措施的防范对象是非法用户和非法操作,完整性措施的防范对象是不合语义的数据.2。
什么是数据库的完整性约束条件?完整性约束条件可以分为哪几类?解:完整性约束条件是指数据库中的数据应该满足的语义约束条件。
一般可以分为六类:静态列级约束、静态元组约束、静态关系约束、动态列级约束、动态元组约束、动态关系约束.静态列级约束是对一个列的取值域的说明,包括以下几个方面:①数据类型的约束,包括数据的类型、长度、单位、精度等;②对数据格式的约束;③对取值范围或取值集合的约束;④对空值的约束;⑤其他约束.静态元组约束就是规定组成一个元组的各个列之间的约束关系,静态元组约束只局限在单个元组上。
静态关系约束是在一个关系的各个元组之间或者若干关系之间常常存在各种联系或约束。
常见的静态关系约束有:①实体完整性约束;②参照完整性约束;③函数依赖约束.动态列级约束是修改列定义或列值时应满足的约束条件,包括下面两方面:①修改列定义时的约束;②修改列值时的约束。
动态元组约束是指修改某个元组的值时需要参照其旧值,并且新旧值之间需要满足某种约束条件.动态关系约束是加在关系变化前后状态上的限制条件,例如事务一致性、原子性等约束条件。
3. 试述DBMS如何实现完整性控制.解:为了维护数据库的完整性,DBMS提供了以下三种机制:①完整性约束条件定义完整性约束条件也称为完整性规则,是数据库中的数据必须满足的语义约束条件.SQL标准使用了一系列概念来描述完整性,包括关系模型的实体完整性、参照完整性和用户定义完整性。
数据库第五章习题及答案本文档为数据库第五章的习题及答案,帮助读者巩固数据库相关知识。
习题1. 数据库的优点有哪些?数据库具有以下优点: - 数据共享:多个用户可以同时访问和共享数据库中的数据。
- 数据一致性:数据库提供事务管理能力,保证了数据的一致性。
- 数据持久性:数据在数据库中是永久存储的,不会因为系统关机或程序结束而丢失。
- 数据冗余度低:数据库通过规范化设计,减少了数据的冗余性,提高了数据的存储效率。
- 数据独立性:数据库支持数据与应用程序的独立性,提高了系统的灵活性和维护性。
- 数据安全性:数据库提供了用户权限管理和数据备份机制,保证了数据的安全性。
2. 数据库的三级模式结构是什么?数据库的三级模式结构包括: - 外模式(视图层):外模式是用户所看到的数据库的子集,用于描述用户对数据库的逻辑视图。
每个用户可以有不同的外模式来满足自己的需求。
- 概念模式(逻辑层):概念模式是全局数据库的逻辑结构和组织方式,描述了数据的总体逻辑视图。
概念模式独立于具体的应用程序,是数据库管理员的角度来看待数据库的。
- 内模式(物理层):内模式是数据库的存储结构和物理组织方式,描述了数据在存储介质上的实际存储方式。
3. 数据库的完整性约束有哪些?数据库的完整性约束包括: - 实体完整性约束:确保表的主键不为空,每个实体都能够唯一标识。
- 参照完整性约束:确保外键的引用关系是有效的,即外键值必须等于被引用表中的主键值或者为空。
- 用户定义完整性约束:用户可以自定义额外的完整性约束,如检查约束、唯一约束、默认约束等。
4. 数据库的关系模型有哪些特点?数据库的关系模型具有以下特点: - 数据用二维表的形式进行组织,表由行和列组成,每一行表示一个实体,每一列表示一个属性。
- 表与表之间通过主键和外键建立关联关系,形成关系。
- 关系模型提供了一种数据独立性的设计方法,使得应用程序与数据的逻辑结构相分离,提高了系统的灵活性和可维护性。
中南大学数据库习题复习第五章习题一、选择题(1) 下列关于SQL语言中索引(Index)的叙述中,哪一条是不正确的?( A )A.索引是外模式B.一个基本表上可以创建多个索引C.索引可以加快查询的执行速度D.系统在存取数据时会自动选择合适的索引作为存取路径(2) 为了提高特定查询的速度,对SC(S#, C#, DEGREE)关系创建唯一性索引,应该创建在哪一个(组)属性上?( A )A.(S#, C#) B.(S#, DEGREE)C.(C#, DEGREE) D.DEGREE(3) 设S_AVG(SNO,AVG_GRADE)是一个基于关系SC定义的学号和他的平均成绩的视图。
下面对该视图的操作语句中,( A )是不能正确执行的。
Ⅰ.UPDATE S_AVG SET AVG_GRADE=90 WHERE SNO='2004010601'Ⅱ.SELECT SNO, AVG_GRADE FROM S_AVG WHERE SNO='2004010601'A.仅ⅠB.仅ⅡC.都能D.都不能(4) 在视图上不能完成的操作是( C )。
A.更新视图B.查询C.在视图上定义新的基本表D.在视图上定义新视图(5) 在SQL语言中,删除一个视图的命令是( B )。
A.DELETE B.DROP C.CLEAR D.REMOVE(6) 为了使索引键的值在基本表中唯一,在创建索引的语句中应使用保留字( A )。
A.UNIQUE B.COUNT C.DISTINCT D.UNION(7) 创建索引是为了( A )。
A.提高存取速度B.减少I/O C.节约空间D.减少缓冲区个数(8) 以下关于视图的描述中,错误的是( A )。
A.可以对任何视图进行任意的修改操作B.视图能够简化用户的操作C.视图能够对数据库提供安全保护作用D.视图对重构数据库提供了一定程度的独立性(9) 在关系数据库中,视图(view)是三级模式结构中的( D )。
第五、六章练习题一、选择题1、在关系数据库设计中,子模式设计是在__________阶段进行。
[ B]A.物理设计B.逻辑设计C.概念设计D.程序设计2、设有关系R(A,B,C)的值如下:A B C2 2 32 3 43 3 5下列叙述正确的是(B)A.函数依赖A→B在上述关系中成立B.函数依赖BC→A在上述关系中成立C.函数依赖B→A在上述关系中成立D.函数依赖A→BC在上述关系中成立3、数据库设计阶段分为(D )A. 物理设计阶段、逻辑设计阶段、编程和调试阶段B. 模型设计阶段、程序设计阶段和运行阶段C. 方案设计阶段、总体设计阶段、个别设计和编程阶段D. 概念设计阶段、逻辑设计阶段、物理设计阶段、实施和调试阶段4、下列说法中不正确的是(C)。
A. 任何一个包含两个属性的关系模式一定满足3NFB. 任何一个包含两个属性的关系模式一定满足BCNFC. 任何一个包含三个属性的关系模式一定满足3NFD. 任何一个关系模式都一定有码5、设有关系模式R(A,B,C,D),F是R上成立的函数依赖集,F={B→C,C→D},则属性C的闭包C+为( C )A.BCDB.BDC.CDD.BC6、在数据库设计中,将ER图转换成关系数据模型的过程属于( B )A.需求分析阶段B.逻辑设计阶段C.概念设计阶段D.物理设计阶段7、下述哪一条不是由于关系模式设计不当而引起的?(B)A) 数据冗余B) 丢失修改C) 插入异常D) 更新异常8、下面关于函数依赖的叙述中,不正确的是(B)A) 若X→Y,X→Z,则X→YZB) 若XY→Z,则X→Z,Y→ZC) 若X→Y,Y→Z,则X→ZD) 若X→Y,Y′ Y,则X→Y′9、设U是所有属性的集合,X、Y、Z都是U的子集,且Z=U-X-Y。
下面关于多值依赖的叙述中,不正确的是(C)A) 若X→→Y,则X→→ZB) 若X→Y,则X→→YC) 若X→→Y,且Y′⊂Y,则X→→Y′D) 若Z=Φ,则X→→Y第(10)至(12)题基于以下的叙述:有关系模式A(C,T,H,R,S),基中各属性的含义是:C:课程T:教员H:上课时间R:教室S:学生根据语义有如下函数依赖集:F={C→T,(H,R)→C,(H,T)→R,(H,S)→R}10、关系模式A的码是(D)A) C B) (H,R)C)(H,T)D)(H,S)11、关系模式A的规范化程度最高达到(B)A) 1NF B) 2NF C) 3NF D) BCNF12、现将关系模式A分解为两个关系模式A1(C,T),A2(H,R,S),则其中A1的规范化程度达到(D)A) 1NF B) 2NF C) 3NF D) BCNF13、下述哪一条不属于概念模型应具备的性质?(D)A) 有丰富的语义表达能力B) 易于交流和理解C) 易于变动D) 在计算机中实现的效率高14、在下面列出的条目中,哪个(些)是当前应用开发工具的发展趋势?(D)Ⅰ.采用三层或多层Client/Server结构Ⅱ.支持Web应用Ⅲ.支持开放的、构件式的分布式计算环境A) Ⅰ和ⅡB) 只有ⅡC) 只有ⅢD) 都是15、下面所列的工具中,不能用于数据库应用系统界面开发的工具是(C)A) Visual Basic B) DelphiC) PowerDesigner D) PowerBuilder16、设关系模式R{A,B,C,D,E},其上函数依赖集F={AB→C,DC→E,D→B},则可导出的函数依赖是(A)。
第五章一、填空题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一起使用,对分组后的结果进行过滤。
第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)·软件工程:研究如何用科学知识、工程方面的纪律指导软件开发的过程,以提高软件质量和开发效率,降低开发成本,这样的一门学科称为“软件工程”。
·软件生存期:软件生存期是指从软件的规划、研制、实现、投入运行后的维护,直到它被新的软件所取代而停止使用的整个期间。
软件生存期通常分为六个阶段:规划阶段,需求分析阶段,设计阶段,程序编制阶段,调试阶段,运行维护阶段。
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)。
第五章习题一、选择题: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.候选码中的属性称为____。
A.非主属性B.主属性C.复合属性D.关键属性答案:B13.关系模式中各级模式之间的关系为____。
A.3NF⊂2NF⊂ 1NF B.3NF⊂ 1NF⊂2NFC.1NF⊂2NF⊂3NF D.2NF⊂1NF⊂3NF答案:A14.关系模式中,满足2NF的模式____。
A.可能是1NF B.必定是1NFC.必定是3NF D.必定是BCNF答案:B15.关系模式R中的属性全部是主属性,则R的最高范式必定是____。
A.2NF B.3NF C.BCNF D.4NF答案:B16.消除了部分函数依赖的INF的关系模式,必定是____。
A.1NF B.2NF C.3NF D.4NF答案:B17.关系模式的候选码可以有①,主码有②。
A.0个B.l个C.l个或多个D.多个答案:①C ②B18.候选码中的属性可以有____。
A.0个B.l个C.l个或多个D.多个答案:C19.关系模式的分解____。
A.惟一B.不惟一答案:B20.根据关系数据库规范化理论,关系数据库中的关系要满足第一范式。
下面“部门”关系中,因哪个属性而使它不满足第一范式?部门(部门号,部门名,部门成员,部门总经理)A.部门总经理B.部门成员C.部门名D.部门号答案:B21.图5.5中给定关系R____。
图5.5 关系RA.不是3NF B.是3NF但不是2NFC.是3NF但不是BCNF D.是BCNF答案:D22.设有如图5.6所示的关系R,它是____。
A.1NF B.2NF C.3NF D.4NF答案:B图5.6 关系R23.设有关系W(工号,姓名,工种,定额),将其规范化到第三范式正确的答案是____。
A.W1(工号,姓名)W2(工种,定额)B.W1(工号,工种,定额)W2(工号,姓名)C.W1(工号,姓名,工种)W2(工号,定额)D.以上都不对答案:C24.设有关系模式W(C,P,S,G,T,R),其中各属性的含义是:C为课程,P为教师,S为学生,G为成绩,T为时间,R为教室,根据定义有如下函数依赖集:F={C→G,(S,C)→G,(T,R)→C,(T,P)→R,(T,S)→R}关系模式W的一个候选码是①,W的规范化程度最高达到②。
若将关系模式W分解为3个关系模式W1(C,P),W2(S,C,G),W3(S,T,R,C),则W1的规范化程度最高达到③,W2的规范化程度最高达到④,W3的规范化程度最高达到⑤。
①A.(S,C)B.(T,R)C.(T,P)D.(T,S)E.(T,S,P)②③④⑤A.1NF B.2NF C.3NF D.BCNF E.4NF答案:①E ②B ③E ④E ⑤B1.属于BCNF的关系模式________。
A.已消除了插入、删除异常B.已消除了插入、删除异常和数据冗余C.仍然存在插入、删除异常D.在函数依赖范畴内,已消除了插入和删除的异常答案:D2.设R(U)是属性集U上的关系模式。
X,Y是U的子集。
若对于R(U)的任意一个可能的关系r,r中不可能存在两个元组在X上的属性值相等,而在Y上的属性值不等,则称________。
A.Y函数依赖于X B.Y对X完全函数依赖C.X为U的候选码D.R属于2NF答案:A3.在R(U)中,如果X→Y,并且对于X的任何一个真子集X',都有X' Y,则________。
A.Y函数依赖于X B.Y对X完全函数依赖C.X为U的候选码D.R属于2NF答案:B4.包含在_______中的属性,叫做主属性。
A.主码B.候选码C.外码D.全码答案:B5.多值依赖的毛病在于________。
A.插入异常B.删除异常C.数据冗余太大D.插入异常、删除异常、及数据冗余太大答案:C6.1NF________规范为2NF。
A.消除非主属性对码的部分函数依赖B.消除非主属性对码的传递函数依赖C.消除主属性对码的部分和传递函数依赖D.消除非平凡且非函数依赖的多值依赖答案:A7.2NF________规范为3NF。
A.消除非主属性对码的部分函数依赖B.消除非主属性对码的传递函数依赖C.消除主属性对码的部分和传递函数依赖D.消除非平凡且非函数依赖的多值依赖答案:B8.3NF________规范为BCNF。
A.消除非主属性对码的部分函数依赖B.消除非主属性对码的传递函数依赖C.消除主属性对码的部分和传递函数依赖D.消除非平凡且非函数依赖的多值依赖答案:C9.3NF________规范为4NF。
A.消除非主属性对码的部分函数依赖B.消除非主属性对码的传递函数依赖C.消除主属性对码的部分和传递函数依赖D.消除非平凡且非函数依赖的多值依赖答案:D二、填空题:1.关系规范化的目的是____。
答案:控制冗余,避免插入异常和删除异常,从而增强数据库结构的稳定性和灵活性。
2.在关系A(S,SN,D)和B(D,CN,NM)中,A的主码是S,B的主码是D,则D 在S中称为____。
答案:外码3.对于非规范化的模式,经过①转变为1NF,将1NF经过②转变为2NF,将2NF经过③转变为3NF。
答案:①使属性域变为简单域。
②消除非主属性对码的部分函数依赖③消除非主属性对码的传递函数依赖4.在一个关系R中,若每个数据项都是不可分割的,那么R一定属于____。
答案:1NF5.1NF,2NF,和3NF之间,相互是一种____关系。
答案:3NF2NF lNF成立6.若关系为1NF,且它的每一非主属性都____候选码,则该关系为2NF。
答案:不部分函数依赖于7.在关系数据库的规范化理论中,在执行“分解”时,必须遵守规范化原则:保持原有的函数依赖和____。
答案:无损连接三、简答题和综合题:1.指出下列关系模式是第几范式?并说明理由。
⑴R(X,Y,Z)F = { XY→Z}。
⑵R(X,Y,Z)F = { Y→Z,XZ→Y}。
⑶R(X,Y,Z)F = { Y→Z,Y→X,X→YZ }。
⑷R(X,Y,Z)F = { X→Y,X→Z }。
⑸R(W,X,Y,Z)F = { X→Z,WX→Y }。
解:(1)R是BCNF。
侯选码为XY,F中只有一个函数依赖,而该函数依赖的左部包含了R 的侯选码XY 。
(2)R 是3NF 。
侯选码为XY 和XZ ,R 中所有属性都是主属性,不存在非主属性对码的传递依赖。
(3)R 是BCNF 。
侯选码为X 和Y ,⊥ X →YZ ,⎽ X →Y ,X →Z ,由于F 中有Y →Z ,Y →X ,因此Z 是直接函数依赖于X ,而不是传递依赖于X 。
又⊥ F 的每一函数依赖的左部都包含了任一侯选码,⎽ R 是BCNF 。
(4)R 是BCNF 。
侯选码为X ,而且F 中每一个函数依赖的左部都包含了侯选码X 。
(5)R 是1NF 。
侯选码为WX ,则Y ,Z 为非主属性,又由于X →Z ,因此F 中存在非主属性对侯选码的部分函数依赖。
2.设有关系模式R (U ,F),其中:U={A ,B ,C ,D ,E ,P},F={ A →B ,C →P ,E →A ,CE →D}。
求R 的所有候选码。
解:根据候选码的定义:如果函数依赖X →U 在R 上成立,且不存在任何X ′⊆X ,使得X ′→U 也成立,则称X 是R 的一个候选码。
由此可知,候选码只可能由A 、C 、E 组成,但有E →A ,所以组成候选码的属性可能是CE 。
计算可知:(CE)+=ABCDEP ,即CE →U而:C +=CP ,E +=ABE⎽ R 只有一个候选码CE 。
3.设有关系模式R(U,F),其中U={C,T,S,N,G},F={C→T,CS→G,S→N}。
求R的所有候选码。
解:根据候选码的定义,R的候选码只可能由F中各个函数依赖的左边属性组成,即C,S,所以组成候选码的属性可能是CS。
①所有只在函数依赖的左边出现的属性一定是某一候选码的成员,②所有只在函数依赖的右边出现的属性一定不在任何候选码中,③所有在函数依赖的左右两边均未出现的属性一定是某一候选码的成员,④所有在函数依赖的左右两边均出现的属性则需要进一步判断。
计算可知:(CS)+=CGNST,即CS→U而:C+=CT,S+=NS⎽ R只有一个候选码CS。
4.设有关系模式R(U,F),其中:U={A,B,C,D,E},F = { A→BC,CD→E,B→D,E→A}。
⑴计算B+。
⑵求R的所有候选码。
解:⑴令X={B},X(0)=B,X(1)=BD,X(2)=BD,故B+=BD。
⑵根据候选码的定义,R的候选码只可能由F中各个函数依赖的左边属性组成,即A,B,C,D,E,由于A→BC(A→B,A→C),B→D,E→A,故:可除去A,B,C,D,⎽组成候选码的属性可能是E。
计算可知:E+=ABCDE,即E→U,⎽ E是一个候选码。
可除去A,B,E,⎽组成候选码的属性可能是CD。