mysql数据库武洪萍版第五章习题与答案

合集下载

数据库第五章习题及答案

数据库第五章习题及答案

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

数据库应用答案第五章

数据库应用答案第五章
⑵ 什么数据类型可与 LIKE 关键字一起使用? 答: 字符串数据类型
⑶ 请 用 BETWEEN … … AND 形 式 改 写 条 件 子 句 WHRER mark>=560 AND mark<=600。
答: WHERE mark BETWEEN 560 AND 600
⑷ 什么聚合函数能对数值类型的列值进行求和?什么聚合函数能用来确定一个表中 包含多少行?在一个包含聚合函数的 SELECT 语句中,GROUP BY 子句有那些用途?
⑺ 首先显示“计算机工程系”、“计算机网络技术专业”、02 班全体学生的基本信息, 然后再统计“计算机工程系”、“计算机网络技术专业”、02 班的学生人数。
SELECT * FROM stud_info WHERE substring(stud_id,3,6)='010202' GO SELECT substring(stud_id,3,6) 专业编号, count(substring(stud_id,3,6)) 人数 FROM stud_info WHERE substring(stud_id,3,6)='010202' GROUP BY substring(stud_id,3,6)
C. 第 3 行
D. 第 4 行
⑹ 在一个查询中,哪一个子句将限制返回的行?[ B ]
A. ORDER BY B. WHERE
C. SELECT
D. FROM
⑺ 哪一个运算符可以替代 WHERE 子句中的 OR 运算符?[ A ]
A. IN
B. >=
C. LIKE
⑻ 下列 SQL 语句中哪一行有错?[ C ]
A. SUM
B. AVG
C. GREATEST

mysql数据库基础习题(分章节带答案)

mysql数据库基础习题(分章节带答案)

第一章一、选择题1.数据库系统的核心是________。

A.数据模型B.数据库管理系统C.数据库D.数据库管理员2.E-R图提供了表示信息世界中实体、属性和________的方法。

A.数据B.联系C.表D.模式3.E-R图是数据库设计的工具之一,它一般适用于建立数据库的________。

A.概念模型B.结构模型C.物理模型D.逻辑模型4.将E.R图转换到关系模式时,实体与联系都可以表示成________。

A.属性B.关系C.键D.域5.在关系数据库设计中,设计关系模式属于数据库设计的________。

A.需求分析阶段B.概念设计阶段C.逻辑设计阶段D.物理设计阶段6.从E—R模型向关系模型转换,一个M:N的联系转换成一个关系模式时,该关系模式的键是________。

A.M端实体的键B.Ⅳ端实体的键C.M端实体键与N端实体键组合D.重新选取其他属性二、填空题1.数据库系统的三级模式结构是指数据库系统是由________、________和________三级构成。

2.数据库系统的运行与应用结构有客户/服务器结构(C/S结构)和________两种。

3.在数据库的三级模式体系结构中,外模式与模式之间的映射实现了数据库的________独立性。

4.用二维表结构表示实体以及实体间联系的数据模型称为________数据模型。

5.数据库设计包括概念设计、________和物理设计。

6.在E-R图中,矩形表示________。

三、简答题1.请简述什么是数据库管理系统,以及它的主要功能有哪些?2.请简述什么是数据库系统?3.请简述什么是模式、外模式和内模式?4.请简述C/S结构与B/S结构的区别。

5.请简述关系规范化过程。

参考答案一、选择题1.A 2.B 3.A 4.B 5.C 6.C二、填空题1.模式外模式内模式2.浏览器/服务器结构(B/S结构) 3.逻辑4。

关系5.逻辑设计6.实体三、简答题1.略。

可参考第1章1.1节内容。

MySQL数据库原理设计与应用习题及答案

MySQL数据库原理设计与应用习题及答案

MySQL数据库原理设计与应用习题及答案一、单选题(共31题,每题1分,共31分)1.以下()只有完全符合给定的判断条件才返回1。

A、带ANY关键字的子查询B、带ALL关键字的子查询C、带IN关键字的子查询D、以上答案都不正确正确答案:B2.下列()可以在命令提示符下停止MySQL服务器。

A、netstopmysqlB、netstartmysqlC、stopmysqlD、netstop正确答案:A3.以下选项()可返回比较后最大的值。

A、GREATEST(10,1,98)B、LEAST(10,1,98)C、MAX(10,1,98)D、以上答案都不正确正确答案:A4.事务的()特性要求事务必须被视为一个不可分割的最小工作单元。

A、一致性B、持久性C、原子性D、隔离性正确答案:C5.下面用于存储二进制数据的是()。

A、INTB、FLOATC、DECIMALD、BIT正确答案:D6.MySQL提供的()语句可查看数据表的创建语句。

A、DESCRIBEB、SHOWFULLCOLUMNSC、SHOWCOLUMNSD、SHOWCREATETABLE正确答案:D7.若依据一个视图创建另一个视图,那么添加()选项,视图的数据操作会进行级联检查。

A、DEFINERB、CASCADEDC、LOCALD、以上选项都不正确正确答案:B8.下面关于联合查询描述错误的是()。

A、联合排序默认去除完全重复的记录B、联合查询必须保证查询的字段数量相同C、联合查询的SELECT语句添加LIMIT并使用圆括号包裹才能使排序生效D、以上说法全部不正确正确答案:D9.若视图是由调用视图的用户执行时,SQLSECURITY的值为()。

A、DEFINERB、INVOKERC、rootD、以上答案都不正确正确答案:B10.以下选项中,不属于MySQL特点的是()。

A、界面良好B、跨平台C、体积小D、速度快正确答案:A11.以下模式之间的映像能体现逻辑独立性的是()。

《数据库概论》第五章期末考试复习提纲+课后习题答案

《数据库概论》第五章期末考试复习提纲+课后习题答案

1.什么是数据库的完整性?
答:数据库的完整性是指数据的正确性和相容性。

2.什么是数据库的完整性约束条件?
答:完整性约束条件是指数据库中的数据应该满足的语义约束条件。

3.DBMS的完整性控制机制应具有哪三个方面的功能?
答:
①定义功能,即提供定义完整性约束条件的机制。

②检查功能,即检查用户发出的操作请求是否违背了完整性约束条件。

③违约处理功能:如果发现用户的操作请求使数据违背了完整性约束条件,则采取一定的动作来保证数据的完整性。

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

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

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)。

《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)。

(完整版)数据库原理第五版习题答案

(完整版)数据库原理第五版习题答案
9.定义并解释概念模型中以下术语:实体,实体型,实体集,属性,码,实体联系图(E一R图)
答:
实体:客观存在并可以相互区分的事物叫实体。实体型:具有相同属性的实体具有相同的特征和性质,用实体名及其属性名集合来抽象和刻画同类实体,称为实体型。实体集:同型实体的集合称为实体集。属性:实体所具有的某一特性,一个实体可由若干个属性来刻画。码:惟一标识实体的属性集称为码。实体联系图(E一R图):提供了表示实体型、属性和联系的方法:·实体型:用矩形表示,矩形框内写明实体名;·属性:用椭圆形表示,并用无向边将其与相应的实体连接起来;·联系:用菱形表示,菱形框内写明联系名,并用无向边分别与有关实体连接起来,同时在无向边旁标上联系的类型(1 : 1 , 1 : n或m : n)。
( 4)数据库管理系统(DataBase Management sytem,简称DBMs ):数据库管理系统是位于用户与操作系统之间的一层数据管理软件,用于科学地组织和存储数据、高效地获取和维护数据。DBMS的主要功能包括数据定义功能、数据操纵功能、数据库的运行管理功能、数据库的建立和维护功能。解析DBMS是一个大型的复杂的软件系统,是计算机中的基础软件。目前,专门研制DBMS的厂商及其研制的DBMS产品很多。著名的有美国IBM公司的DBZ关系数据库管理系统和IMS层次数据库管理系统、美国Oracle公司的orade关系数据库管理系统、s油ase公司的s油ase关系数据库管理系统、美国微软公司的SQL Serve,关系数据库管理系统等。
11.试给出一个实际部门的E一R图,要求有三个实体型,而且3个实体型之间有多对多联系。3个实体型之间的多对多联系和三个实体型两两之间的三个多对多联系等价吗?为什么?
答:
3个实体型之间的多对多联系和3个实体型两两之间的3个多对多联系是不等价,因为它们拥有不同的语义。3个实体型两两之间的三个多对多联系如下图所示。

MySQL数据库及应用题库附答案_最新版

MySQL数据库及应用题库附答案_最新版

MySQL数据库及应用题库附答案温馨提示:本文是笔者精心整理编制而成,有很强的的实用性和参考性,下载完成后可以直接编辑,并根据自己的需求进行修改套用。

【仅供学习参考, 切勿通篇使用!】MySQL数据库及应用题库附答案第一章测验1 单选计算机进行数据处理经历了从低级到高级的____________个发展阶段A. 2B. 3C. 4D. 5答案: C2 单选关系数据模型是以__________理论为基础的, 用二维表结构来表示实体以及实体之间联系的模型。

A. 关系B. 表C. 元组D. 记录答案: A3 单选关系中能唯一标识每个元组的最少属性或属性组称之为_________________。

A. 列B. 外关键字C. 索引D. 关键字(主码或主键)答案: D4 单选在同一个数据库中某个关系R1中的属性或属性组若在另一个关系R2中作为关键字(主码)使用, 则该属性或属性组为R1的___________。

A. 列B. 外关键字C. 索引D. 关键字(主码或主键)答案: B5 单选一个数据库中往往包含多个关系, 一个数据库中这些关系的集合称之为___________。

A. 关系组合B. 关系集合C. 数据库模式D. 关系模式答案: C6 单选关系代数的运算分为两大类, 第一类是传统的集合运算并、交、差运算, 另一类是专门的关系运算, 主要是选择、投影和___________。

A. 连接B. 自然连接C. 笛卡尔积D. 查询答案: A7 单选数据库、数据库管理和数据库系统之间的关系正确的是A. 数据库包括了数据库管理系统和数据库系统B. 数据库管理系统包括了数据库和数据库系统C. 数据库系统包括数据库和数据库管理系统D. 以上都不对答案: C8 单选目前, 商品化的数据库管理系统以__________型为主。

A. 关系B. 层次C. 网状D. 对象答案: A9 单选从给定关系中找出满足一定条件的元组的运算, 称为________运算。

数据库应用基础第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。

数据库武洪萍版习题答案

数据库武洪萍版习题答案

习题答案第一章习题12.填空题(1)物理数据独立性(2)数据库管理系统((DBMS)(3)现实世界、信息世界、数据世界(4)码(5)一对一(1:1)、一对多(1:n)、多对多(m:n)(6)概念数据模型 E-R模型(7)逻辑数据物理数据(8)DBMS(数据库管理系统) DBA(数据库管理员)(9)关系的参照(10)θ3.简答题(1)数据模型是对现实世界的数据特征进行的抽象,来描述数据库的结构与语义。

数据模型的三要素是:数据结构、数据操作、数据约束条件。

(2)逻辑数据独立性:当模式改变时(如增加新的关系、新的属性、改变属性的数据类型等),由数据库管理员对各个外模式/模式映像作相应改变,可以使外模式保持不变。

因而应用程序不必修改,保证了数据与程序的逻辑独立性,简称逻辑数据独立性。

物理数据独立性:当数据库的存储结构改变了(如选用了另一种存储结构),由数据库管理员对模式/内模式映像作相应改变,可以保证模式保持不变,因而应用程序也不必改变。

保证了数据与程序的物理独立性,简称物理数据独立性特定的应用程序是在外模式描述的数据结构上编制的,它依赖于特定的外模式,与数据库的模式和存储结构相独立。

不同的应用程序可以共用同一外模式。

数据库的两级映像保证了数据库外模式的稳定性,从而从底层保证了应用程序的稳定性,使得数据库系统具有数据与程序的独立性。

(3)数据库系统由计算机硬件、数据库、数据库管理系统(及其开发工具)、数据库应用系统、数据库用户构成。

(4)DBA的职责是对使用中的数据库进行整体维护和改进,负责数据库系统的正常运行,是数据库系统的专职管理和维护人员。

系统分析员负责应用系统的需求分析和规范说明,要和用户及DBA结合,确定系统的硬件软件配置,并参与数据库系统的概要设计。

数据库设计人员负责数据库中数据的确定、数据库各级模式的设计。

应用程序开发人员负责设计和编写应用程序的程序模块,并进行测试和安装。

(6)目前比较流行的DBMS有Visual FoxPro、Access、SQL Server、My SQL 、Oracle等。

MySQL数据库考试试题和答案

MySQL数据库考试试题和答案

专业: 移动通信科目: MySQL数据库一、单项选择题1. 以下聚合函数求数据总和的是( )A. MAXB. SUMC. COUNTD. AVG答案:B2. 可以用( )来声明游标A. CREATE CURSORB. ALTER CURSORC. SET CURSORD. DECLARE CURSOR答案:D3. SELECT语句的完整语法较复杂, 但至少包括的部分是( )A. 仅SELECTB. SELECT, FROMC. SELECT, GROUPD.SELECT, INTO答案:B4. SQL语句中的条件用以下哪一项来表达( )A. THENB. WHILEC. WHERED. IF答案:C5. 使用CREATE TABLE语句的( )子句, 在创建基本表时可以启用全文本搜索A. FULLTEXTB. ENGINEC. FROMD. WHRER答案:A6. 以下能够删除一列的是( )A. alter table emp remove addcolumnB. alter table emp drop column addcolumnC. alter table emp delete column addcolumnD. alter table emp delete addcolumn答案:B7. 若要撤销数据库中已经存在的表S, 可用()。

A. DELETE TABLE SB. DELETE SC. DROP SD. DROP TABLE S答案:D8. 查找表结构用以下哪一项( )A. FINDB. SELETEC. ALTERD. DESC答案:D9. 要得到最后一句SELECT查询到的总行数, 可以使用的函数是( )A. FOUND_ROWSB. LAST_ROWSC. ROW_COUNTD. LAST_INSERT_ID答案:A10. 在视图上不能完成的操作是( )A. 查询B. 在视图上定义新的视图C. 更新视图D. 在视图上定义新的表答案:D11. UNIQUE惟一索引的作用是( )A. 保证各行在该索引上的值都不得重复B. 保证各行在该索引上的值不得为NULLC.保证参加惟一索引的各列, 不得再参加其他的索引D. 保证惟一索引不能被删除答案:A12. 用于将事务处理写到数据库的命令是( )A. insertB. rollbackC. commitD. savepoint答案:C13. 查找条件为: 姓名不是NULL的记录( )A. WHERE NAME ! NULLB. WHERE NAME NOT NULLC. WHERE NAME IS NOT NULLD. WHERE NAME!=NULL答案:C14. 主键的建立有( )种方法A.一B.四C.二D.三答案:D15. 在视图上不能完成的操作是( )A. 更新视图数据B. 在视图上定义新的基本表C. 在视图上定义新的视图D. 查询答案:B16. 在SQL语言中, 子查询是()。

MySQL数据库基础与实例教程练习题参考答案

MySQL数据库基础与实例教程练习题参考答案

MySQL数据库基础与实例教程练习题参考答案由于时间仓促,中难免存在错误,不妥之处恳请读者批评指正!第一章答案1.数据库管理系统中常用的数学模型有哪些数据库管理系统通常会选择某种“数学模型”存储、组织、管理数据库中的数据,常用的数学模型包括“层次模型”、“网状模型”、“关系模型”以及“面向对象模型”等。

2.您听说过的关系数据库管理系统有哪些数据库容器中通常包含哪些数据库对象目前成熟的关系数据库管理系统主要源自欧美数据库厂商,典型的有美国微软公司的SQL Server、美国IBM公司的DB2和Informix、德国SAP公司的Sybase、美国甲骨文公司的Oracle。

数据库容器中通常包含表、索引、视图、存储过程、触发器、函数等数据库对象。

3.通过本章知识的讲解,SQL与程序设计语言有什么关系SQL并不是一种功能完善的程序设计语言,例如,不能使用SQL构建人性化的图形用户界面(Graphical User Interface,GUI),程序员需要借助Java、VC++等面向对象程序设计语言或者HTML的FORM表单构建图形用户界面(GUI)。

如果选用FORM表单构建GUI,程序员还需要使用JSP、PHP或者.NET编写Web应用程序,处理FORM表单中的数据以及数据库中的数据。

其他答案:1、首先SQL语言是数据库结构化查询语言,是非过程化编程语言。

而程序设计语言则有更多的面向对象及逻辑程序设计。

比如用SQL语言编写图形用户界面(例如窗口、进度条),是无法实现的。

2、SQL语言可以说是,程序设计语言和数据库之间的一个翻译官。

程序设计语言需要操作数据库时,需要借助(或者说调用)SQL语言来翻译给数据库管理系统。

3、不同数据库管理系统会有一些特殊的SQL规范,比如limit关键词在SQL Server中无法使用。

而这些规范与程序设计语言无关。

4.通过本章的学习,您了解的MySQL有哪些特点与题目2中列举的商业化数据库管理系统相比,MySQL具有开源、免费、体积小、便于安装,但功能强大等特点。

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

第五章一.选择题1.下列关于SQL语言索引(Index)的叙述中,哪一条是不正确的(C)。

A.索引是外模式B.一个基本表上可以创建多个索引C.索引可以加快查询的执行速度D.系统在存取数据时会自动选择合适的索引作为存取路径2.为了提高特定查询的速度,对SC(S#,C#,DEGREE)关系创建唯一性索引,应该创建在哪一个属性(组)上?(A)A.(S#,C#)B. (S#,DEGREE)C. (C#,DEGREE)D. DEGREE3.设S_AVG(SNO,AVG_GRADE)是一个基于关系SC 定义的学号和他的平均成绩的视图。

下面对该视图的操作语句中,(A)是不能正确执行的。

Ⅰ. UODATE 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.DELECTB. DROPC. CLEARD. UNION6.为了使索引建的值在基本表中唯一,在创建索引的语句中应使用保留字()。

A.UNIQUEB. COUNTC. DISTINCTD.UNION7.创建索引是为了(A)。

A.提高存取速度B. 减少I/OC. 节约空间D. 减少缓冲区个数8.在关系数据库中,视图(View )是三级模式结构中的(D)。

A.内模式B. 模式C. 存取模式D. 外模式9.视图是一个“虚表”,视图的构造基于(A)。

Ⅰ.基本表Ⅱ. 视图Ⅲ. 索引10.已知关系:STUDENT(Sno,Sname,Grade),以下关于命令”CREATE INDEX S index ON STUDENT(Grade)”的描述中,正确的是(B)。

A.按成绩降序创建了一个普通索引B.按成绩升序创建了一个普通索引C.按成绩降序创建了一个全文索引D.按成绩升序创建了一个全文索引11.在关系数据库中,为了简化用户的查询操作,而又增加数据的存储空间,则应该创建的数据库对象是(D)。

A.Table(表)B. Index(索引)C. Cursor(游标)D. View(视图)12.下面关于关系数据库视图的描述,不正确的是(A)A.视图是关系数据库三级模式中的内模式B.视图能够对机密数据提供安全保护视图对重构数据库提供了一定程度的逻辑独立性C.D.对视图的一切操作最终要转换为对基本表的操作。

13.触发器的触发事件有3种,下面哪一种是错误的?(C)A.UPDATEB. DELECTC. ALTERD. INSERT14.下列几种情况下,不适合创建索引的是(A)。

A.列的取值范围很少B. 用作查询条件的列C. 频繁搜索范围的列D. 连接中频繁使用的列15.CREATE UNIQUE INDEX writer_index ON 作者信息(作者编号)语句创建了一个(A)索引。

A.唯一性索引B. 全文索引C. 普通索引D. 空间索引16.存储过程和存储函数的相关信息是在(B)数据库中存放。

A.mysqlB. Information_schemaC. Performance_schemaD. Test17.一个触发器能定义在多少个表中?(A)A.只有一个B. 一个或多个C.一个到3个D. 任意多个18.下面选项中不属于存储过程和存储函数的优点的是(D)。

A.增强代码的重用性和共享性B. 可以加快运行速度,减少网络流量C. 可以作为安全性机制D. 编辑简单19.一个表上可以有(C)不同类型的触发器。

A.一种B. 两种C. 3种D. 无限制20.使用(D)语句删除触发器trig_Test。

A.DROP *FROM Trig_TestB.DROP trig_TestC.DROP TRIGGER WHERE NAME='trig_Test'D.DROP TRIGGER trig_Test二、填空题1.视图是从一个或几个基本表或者视图中导出的表,数据库中实际存放的是视图的定义,而不是视图对应的数据。

2.当对视图进行UPDATE、INSERT和DELETE 操作时,为了保证被操作的行为满足视图定义中子查询语句的谓词条件,应在视图定义语句中使用可选择项WITH CHECK OPTION。

3.SQL语言支持数据库3级模式结构。

在SQL中,外模式对应于视图和部分基本表,模式对应于基本表全体,内模式对应于存储文件。

4.如果在视图中删除或修改一条记录,则相应的基本表也随着视图更新。

5.在MySQL系统中,有两种基本类型的索引:普通和唯一。

6.创建唯一性索引时,应保证创建索引的列不包括重复的数据,并且没有两个或两个以上的空值。

如果有这种数据,必须先将其删除,否则索引不能成功创建。

7.存储过程和存储函数的相关信息在routines表中存放,触发器的相关信息在triggers。

8.在MySQL中,触发器的执行时间有两种,before 和after 。

三、简答题1.简述索引的作用。

答:索引依赖于表建立,提供了数据库中编排表中数据的内部方法,使用索引可以从大量的数据中迅速找到所需要的数据,不再检索整个数据库,大大提高检索的效率。

2.视图与表有何不同?答:并不直接存储数据库中只存放其定义,视图是从一个或多个基本表或视图中导出的虚拟表,视图对应的数据,直到用户使用视图时才去查找对应的数据。

视图可以像基本表一样实现数据的查询操作,对视图的插入、修改、删除操作最终转换为对相应的基本表的操作,但有一定的限制。

3.简述存储过程、触发器各自的特点,总结并讨论各适用于何处。

答:存储过程的特点:增强了SQL语言的功能和灵活性、允许标准组件式编程、能实现较快的执行速度、能减少网络流量、可作为一种安全机制来充分利用。

触发器的特点:触发器自动执行、可通过数据库中的相关表进行层叠更改、可以强制限制。

4什么是游标?为什么要使用游标?答:游标在MySQL中是一种数据访问机制,允许用户访问单独的数据行,而不是对整个行集进行操作。

5简述视图的优缺点。

答:优点:(1)简化对数据的操作。

视图可以简化用户操作数据的方式。

(2)自定义数据。

视图能够让不同用户以不同方式看到不同或相同的数据集,即使不同水平的用户公用同一数据时也是如此。

(3)数据集中显示。

(4)导入和导出数据。

可以使用视图将数据导入或导出。

(5)合并分割数据。

(6)安全机制。

视图可以作为一种安全机制。

缺点:66.通过视图修改数据需要遵循哪些准则?答:如果视图中有下面所述属性,则插入、更新或删除操作将失败。

1)视图定义中的FROM子句包含两个或多个表,且SELECT选择列表达式中的列包含来自多个表的列。

2)视图的列是从集合函数派生的。

3)视图中的SELECT语句包含GROUP BY子句或DISTINCT选项。

4)视图的列是从常量或表达式派生的。

7.利用索引检索数据有哪些优点?答:1)索引可以提高数据的访问速度。

2)索引可以确保数据的唯一性。

8. 如何创建一个存储过程和函数?答:1.创建存储过程1)利用CREATE PROCEDURE语句创建。

用户可以使用CREATE PROCEDURE语句创建存储过程,其基本语法如下。

eter[,…]])CREATE PROCEDURE procedure_name([proc_param[characteristic[,…]]Routine_body2)利用Navicat图形工具创建。

2.创建存储函数1)利用CREATE FUNCTION语句创建。

在MySQL中,创建存储函数的基本语法如下。

CREATE FUNCTION func_name([func_parameter[,…]])RETURNS type[characteristic[,…]]Routine_body3)利用Navicat图形工具创建。

9.在什么情况下要使用事务?事务有哪些属性?答:事务处理机制在程序开发过程中有着非常重要的作用,它可以使整个系统更加安全,能够保证数据库操作的一致性和完整性,可以确保同时发生的行为与数据的有效性不发生冲突。

10.各种触发器的触发顺序是什么?答:INSERT触发器:将新行插入表时激活触发程序。

UPDATE触发器:更改某一行时激活触发程序。

DELETE触发器:从表中删除某一行时激活触发程序。

相关文档
最新文档