数据库第六版第四章答案4.doc

合集下载

第四章 作业参考答案

第四章 作业参考答案

第四章作业参考答案4. 用推广的Euclid算法求67 mod 119的逆元解:初始化:(1,0,119), (0,1,67)1:Q=119/67=1,(0,1,67) , (1,-1,52)2:Q=67/52=1,(1,-1,52), (-1,2,15)3:Q=52/15=3,(-1,2,15), (4,-7,7)4:Q=15/7=2,(4,-7,7), (-9,16,1)所以67-1 mod 119=1610.设通信双方使用RSA加密体制,接收方的公开钥是(e,n)=(5,35),接收到的密文是C =10,求明文M。

解:由n=35,易知35=5×7,进而ϕ(n)=ϕ(35)=24,由RSA加密体制可知,ed≡1 mod ϕ(n),即5d≡1 mod 24,所以d=5∴M=C d mod n=105 mod 35=511. 已知c d mod n的运行时间是O(log3n),用中国剩余定理改进RSA的解密运算。

如果不考虑中国剩余定理的计算代价,证明改进后的解密运算速度是原解密运算速度的4倍。

证明:RSA的两个大素因子p,q的长度近似相等,约为模数n的比特长度log n的一半,即(log n)/2,而在中国剩余定理中要计算模p和模q两个模指数运算,与c d mod n的运行时间规律相似,每一个模指数运算的运行时间仍然是其模长的三次幂,即O[((log n)/2)3]= O(log3n)/8,这样在不考虑中国剩余定理计算代价的情况下,总的运行时间为两个模指数的运行时间之和,即O(log3n)/8+O(log3n)/8=O(log3n)/4,得证。

12. 设RSA加密体制的公开钥是(e,n)=(77,221)。

(1) 用重复平方法加密明文160,得中间结果为1602(mod 221)=185,1604(mod 221)=191,1608(mod 221)=16,16016(mod 221)=35,16032(mod 221)=120,16064(mod 221)=35,16072(mod 221)=118,16076(mod 221)=217,16077(mod 221)=23,若敌手得到以上中间结果就很容易分解n,问敌手如何分解n解:由以上中间结果得16016(mod 221)=35=16064(mod 221),此即16064-16016=0 (mod 221)即(16032-1608) (16032+1608)=0 (mod 221)(120-16)(120+16)=0 (mod 221)104×136=0 (mod 221)由gcd(104,221)=13及gcd(136,221)=17,可知221的分解为221=13×17(2) 求解密密钥dd=e-1mod ϕ(221)=77-1 mod 12×16由扩展Eucild算法可得d=5。

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

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

习题61、说明数据库设计的特点。

1)三分技术,七分管理,十二分基础数据2)综合性3)结构(数据)设计和行为(处理)设计相结合2、试述数据库设计的过程3、试述数据库设计过程的各个阶段设计内容。

1)需求分析阶段需求分析是对用户提出的各种要求加以分析,对各种原始数据加以综合、整理,是形成最终设计目标的首要阶段。

需求分析是整个设计过程的基础,是最困难、最耗费时间的一步。

2)概念结构设计阶段概念结构设计是对用户需求进行进一步抽象、归纳,并形成独立于DBMS和有关软、硬件的概念数据模型的设计过程。

3)逻辑结构设计阶段逻辑结构设计是将概念结构转换为某个DBMS所支持的数据模型,并对其进行优化的设计过程。

4)物理设计阶段数据库物理设计阶段,是将逻辑结构设计阶段所产生的逻辑数据模型,转换为某种计算机系统所支持的数据库物理结构的实现过程。

5)数据库实施阶段数据库实施阶段,即数据库调试、试运行阶段。

一旦数据库的物理结构形成,就可以用已选定的DBMS来定义、描述相应的数据库结构,装入数据库数据库,以生成完整的数据库,编制有关应用程序,进行联机调试并转入试运行,同时进行时间、空间等性能分析。

6)数据库运行和维护阶段数据库实施阶段结束,标志着数据库系统投入正常运行工作的开始。

在数据库系统运行过程中必须不断地对其进行评价、调整与修改。

4、需求分析中发现事实的方法有哪些?1)跟班作业。

通过亲身参加业务工作来观察和了解业务活动的情况。

2)开调查会。

通过与用户座谈来了解业务活动的情况及用户需求。

3)检查文档。

通过检查与当前系统有关的文档、表格、报告和文件等,进一步理解原系统,并有利于提供与原系统问题相关的业务信息。

4)问卷调查。

5、需求分析阶段的设计目标是什么?调查的内容是什么?需求分析阶段的目标是通过详细调查现实世界要处理的对象(组织、部门、企业等),充分了解原系统(手工系统或计算机系统)工作概况,确定企业的组织目标,明确用户的各种需求,进而确定新系统的功能,并把这些要求写成用户和数据库设计者都能够接受的文档。

数据库第六版第四章答案

数据库第六版第四章答案

Intermediate SQLPractice Exercises4.1Write the following queries in SQL:a.Display a list of all instructors,showing their ID,name,and the num-ber of sections that they have taught.Make sure to show the numberof sections as0for instructors who have not taught any section.Yourquery should use an outerjoin,and should not use scalar subqueries.b.Write the same query as above,but using a scalar subquery,withoutouterjoin.c.Display the list of all course sections offered in Spring2010,alongwith the names of the instructors teaching the section.If a section hasmore than one instructor,it should appear as many times in the resultas it has instructors.If it does not have any instructor,it should stillappear in the result with the instructor name set to“—”.d.Display the list of all departments,with the total number of instructorsin each department,without using scalar subqueries.Make sure tocorrectly handle departments with no instructors.Answer:a.Display a list of all instructors,showing their ID,name,and the num-ber of sections that they have taught.Make sure to show the numberof sections as0for instructors who have not taught any section.Yourquery should use an outerjoin,and should not use scalar subqueries.select ID,name,count(course id,section id,year,semester)as’Number of sections’from instructor natural left outer join teachesgroup by ID,nameThe above query should not be written using count(*)since count*counts null values also.It could be written using count(section id),or1920Chapter4Intermediate SQLany other attribute from teaches which does not occur in instructor,which would be correct although it may be confusing to the reader.(Attributes that occur in instructor would not be null even if the in-structor has not taught any section.)b.Write the same query as above,but using a scalar subquery,withoutouterjoin.select ID,name,(select count(*)as’Number of sections’from teaches T where T.id=I.id)from instructor Ic.Display the list of all course sections offered in Spring2010,alongwith the names of the instructors teaching the section.If a section hasmore than one instructor,it should appear as many times in the resultas it has instructors.If it does not have any instructor,it should stillappear in the result with the instructor name set to“−”.select course id,section id,ID,decode(name,NULL,’−’,name)from(section natural left outer join teaches)natural left outer join instructorwhere semester=’Spring’and year=2010The query may also be written using the coalesce operator,by re-placing decode(..)by coalesce(name,’−’).A more complex versionof the query can be written using union of join result with anotherquery that uses a subquery tofind courses that do not match;refer toexercise4.2.d.Display the list of all departments,with the total number of instructorsin each department,without using scalar subqueries.Make sure tocorrectly handle departments with no instructors.select dept name,count(ID)from department natural left outer join instructorgroup by dept name4.2Outer join expressions can be computed in SQL without using the SQLouter join operation.To illustrate this fact,show how to rewrite each of thefollowing SQL queries without using the outer join expression.a.select*from student natural left outer join takesb.select*from student natural full outer join takesAnswer:a.select*from student natural left outer join takescan be rewritten as:Exercises21 select*from student natural join takesunionselect ID,name,dept name,tot cred,NULL,NULL,NULL,NULL,NULLfrom student S1where not exists(select ID from takes T1where T1.id=S1.id)b.select*from student natural full outer join takescan be rewritten as:(select*from student natural join takes)union(select ID,name,dept name,tot cred,NULL,NULL,NULL,NULL,NULLfrom student S1where not exists(select ID from takes T1where T1.id=S1.id))union(select ID,NULL,NULL,NULL,course id,section id,semester,year,gradefrom takes T1where not exists(select ID from student S1where T1.id=S1.id))4.3Suppose we have three relations r(A,B),s(B,C),and t(B,D),with allattributes declared as not null.Consider the expressions•r natural left outer join(s natural left outer join t),and•(r natural left outer join s)natural left outer join ta.Give instances of relations r,s and t such that in the result of thesecond expression,attribute C has a null value but attribute D has anon-null value.b.Is the above pattern,with C null and D not null possible in the resultof thefirst expression?Explain why or why not.Answer:a.Consider r=(a,b),s=(b1,c1),t=(b,d).The second expression wouldgive(a,b,NULL,d).b.It is not possible for D to be not null while C is null in the result of thefirst expression,since in the subexpression s natural left outer join t,it is not possible for C to be null while D is not null.In the overallexpression C can be null if and only if some r tuple does not have amatching B value in s.However in this case D will also be null.4.4Testing SQL queries:To test if a query specified in English has been cor-rectly written in SQL,the SQL query is typically executed on multiple test22Chapter4Intermediate SQLdatabases,and a human checks if the SQL query result on each test databasematches the intention of the specification in English.a.In Section Section3.3.3The Natural Joinsubsection.3.3.3we saw an ex-ample of an erroneous SQL query which was intended tofind whichcourses had been taught by each instructor;the query computed thenatural join of instructor,teaches,and course,and as a result uninten-tionally equated the dept name attribute of instructor and course.Givean example of a dataset that would help catch this particular error.b.When creating test databases,it is important to create tuples in refer-enced relations that do not have any matching tuple in the referencingrelation,for each foreign key.Explain why,using an example queryon the university database.c.When creating test databases,it is important to create tuples with nullvalues for foreign key attributes,provided the attribute is nullable(SQL allows foreign key attributes to take on null values,as long asthey are not part of the primary key,and have not been declared asnot null).Explain why,using an example query on the universitydatabase.Hint:use the queries from Exercise Exercise4.1Item.138.Answer:a.Consider the case where a professor in Physics department teaches anElec.Eng.course.Even though there is a valid corresponding entryin teaches,it is lost in the natural join of instructor,teaches and course,since the instructors department name does not match the departmentname of the course.A dataset corresponding to the same is:instructor={(12345,’Guass’,’Physics’,10000)}teaches={(12345,’EE321’,1,’Spring’,2009)}course={(’EE321’,’Magnetism’,’Elec.Eng.’,6)}b.The query in question0.a is a good example for this.Instructors whohave not taught a single course,should have number of sections as0in the query result.(Many other similar examples are possible.)c.Consider the queryselect*from teaches natural join instructor;In the above query,we would lose some sections if teaches.ID is al-lowed to be NULL and such tuples exist.If,just because teaches.ID isa foreign key to instructor,we did not create such a tuple,the error inthe above query would not be detected.4.5Show how to define the view student grades(ID,GP A)giving the grade-point average of each student,based on the query in Exercise??;recallthat we used a relation grade points(grade,points)to get the numeric pointsExercises23 associated with a letter grade.Make sure your view definition correctly handles the case of null values for the grade attribute of the takes relation.Answer:We should not add credits for courses with a null grade;further to to correctly handle the case where a student has not completed any course, we should make sure we don’t divide by zero,and should instead return a null value.We break the query into a subquery thatfinds sum of credits and sum of credit-grade-points,taking null grades into account The outer query divides the above to get the average,taking care of divide by0.create view student grades(ID,GP A)asselect ID,credit points/decode(credit sum,0,NULL,credit sum)from((select ID,sum(decode(grade,NULL,0,credits))as credit sum,sum(decode(grade,NULL,0,credits*points))as credit pointsfrom(takes natural join course)natural left outer join grade pointsgroup by ID)unionselect ID,NULLfrom studentwhere ID not in(select ID from takes))The view defined above takes care of NULL grades by considering the creditpoints to be0,and not adding the corresponding credits in credit sum.The query above ensures that if the student has not taken any course with non-NULL credits,and has credit sum=0gets a gpa of NULL.This avoid the division by0,which would otherwise have resulted.An alternative way of writing the above query would be to use student natural left outer join gpa,in order to consider students who have not taken any course.4.6Complete the SQL DDL definition of the university database of Figure Fig-ure4.8Referential Integrityfigcnt.50to include the relations student,takes, advisor,and prereq.Answer:create table student(ID varchar(5),name varchar(20)not null,dept name varchar(20),tot cred numeric(3,0)check(tot cred>=0),primary key(ID),foreign key(dept name)references departmenton delete set null);24Chapter4Intermediate SQLcreate table takes(ID varchar(5),course id varchar(8),section id varchar(8),semester varchar(6),year numeric(4,0),grade varchar(2),primary key(ID,course id,section id,semester,year),foreign key(course id,section id,semester,year)references sectionon delete cascade,foreign key(ID)references studenton delete cascade);create table advisor(i id varchar(5),s id varchar(5),primary key(s ID),foreign key(i ID)references instructor(ID)on delete set null,foreign key(s ID)references student(ID)on delete cascade);create table prereq(course id varchar(8),prereq id varchar(8),primary key(course id,prereq id),foreign key(course id)references courseon delete cascade,foreign key(prereq id)references course);4.7Consider the relational database of Figure Figure4.11figcnt.53.Give an SQLDDL definition of this database.Identify referential-integrity constraintsthat should hold,and include them in the DDL definition.Answer:create table employee(person name char(20),street char(30),city char(30),primary key(person name))Exercises25create table works(person name char(20),company name char(15),salary integer,primary key(person name),foreign key(person name)references employee,foreign key(company name)references company)create table company(company name char(15),city char(30),primary key(company name))pp create table manages(person name char(20),manager name char(20),primary key(person name),foreign key(person name)references employee,foreign key(manager name)references employee)Note that alternative datatypes are possible.Other choices for not nullattributes may be acceptable.4.8As discussed in Section Section4.4.7Complex Check Conditions and Assertionssubsection.4.4we expect the constraint“an instructor cannot teach sections in two differ-ent classrooms in a semester in the same time slot”to hold.a.Write an SQL query that returns all(instructor,section)combinationsthat violate this constraint.b.Write an SQL assertion to enforce this constraint(as discussed in Sec-tion Section4.4.7Complex Check Conditions and Assertionssubsection.4.4.7,current generation database systems do not support such assertions,although they are part of the SQL standard).Answer:a.select ID,name,section id,semester,year,time slot id,count(distinct building,room number)from instructor natural join teaches natural join sectiongroup by(ID,name,section id,semester,year,time slot id)having count(building,room number)>1Note that the distinct keyword is required above.This is to allow twodifferent sections to run concurrently in the same time slot and are26Chapter4Intermediate SQLtaught by the same instructor,without being reported as a constraintviolation.b.create assertion check not exists(select ID,name,section id,semester,year,time slot id,count(distinct building,room number)from instructor natural join teaches natural join sectiongroup by(ID,name,section id,semester,year,time slot id)having count(building,room number)>1)4.9SQL allows a foreign-key dependency to refer to the same relation,as in thefollowing example:create table manager(employee name char(20),manager name char(20),primary key employee name,foreign key(manager name)references manageron delete cascade)Here,employee name is a key to the table manager,meaning that each em-ployee has at most one manager.The foreign-key clause requires that everymanager also be an employee.Explain exactly what happens when a tuplein the relation manager is deleted.Answer:The tuples of all employees of the manager,at all levels,getdeleted as well!This happens in a series of steps.The initial deletion willtrigger deletion of all the tuples corresponding to direct employees ofthe manager.These deletions will in turn cause deletions of second levelemployee tuples,and so on,till all direct and indirect employee tuples aredeleted.4.10SQL-92provides an n-ary operation called coalesce,which is defined asfollows:coalesce(A1,A2,...,A n)returns thefirst nonnull A i in the listA1,A2,...,A n,and returns null if all of A1,A2,...,A n are null.Let a and b be relations with the schemas A(name,address,title)and B(name,address,salary),respectively.Show how to express a natural full outer joinb using the full outer-join operation with an on condition and the coalesceoperation.Make sure that the result relation does not contain two copiesof the attributes name and address,and that the solution is correct even ifsome tuples in a and b have null values for attributes name or address.Answer:Exercises27 select coalesce(,)as name,coalesce(a.address,b.address)as address,a.title,b.salaryfrom a full outer join b on = anda.address=b.address4.11Some researchers have proposed the concept of marked nulls.A markednull⊥i is equal to itself,but if i=j,then⊥i=⊥j.One application of marked nulls is to allow certain updates through views.Consider the view instructor info(Section Section4.2Viewssection.4.2).Show how you can use marked nulls to allow the insertion of the tuple(99999,“Johnson”,“Music”) through instructor info.Answer:To insert the tuple(99999,“(”Johnson),“Music”)into the view instructor info,we can do the following:instructor←(99999,“Johnson”,⊥k,⊥)∪instructordepartment←(⊥k,“Music′′,⊥)∪departmentsuch that⊥k is a new marked null not already existing in the database.Note:“Music”here is the name of a building and may or may not be related to Music department.。

数据库技术与应用——VisualFoxPro6.0篇课后练习题答案

数据库技术与应用——VisualFoxPro6.0篇课后练习题答案

<<数据库技术与应用——Visual FoxPro 6.0篇>>练习题答案第1章练习题答案1、数据定义、数据操纵2、文件系统阶段、数据库系统阶段3、属性、字段、元组、记录4、关键字5、域6、属性、属性到域的映象7、关系、关系8、投影9、选择运算、投影运算10、连接运算、自然连接三、简答题1、答:数据库(DataBase)是被长期存放在计算机内、有组织的、可以表现为多种形式的可共享的数据集合。

数据库管理系统(DataBase Management System,简称DBMS)是计算机系统软件,它的职能是有效地组织和存储数据、获取和管理数据,接受和完成用户提出的访问数据的各种请求。

数据库系统是指拥有数据库技术支持的计算机系统,它可以实现有组织地、动态地存储大量相关数据,提供数据处理和信息资源共享服务。

数据库系统是指在计算机系统中引入数据库后的系统,一般由数据库、数据库管理系统(及其开发工具)、应用系统、数据库管理员和用户构成。

2、答:关系是一张二维表,每个关系有一个关系名。

在计算机中,一个关系可以存储为一个文件。

在Visual FoxPro中,一个关系就是一个表文件。

元组是二维表中水平方向的行,有时也叫做一条记录。

属性是二维表中垂直方向的列,有时也叫做一个字段。

3、答:数据库系统由四部分组成:硬件系统、系统软件、数据库应用系统和各类人员。

4、答:常用的数据模型有层次模型、网状模型和关系模型。

层次模型的特点是:形状象一棵倒立的树,有且仅有一个结点无父结点,这个结点称为根结点,其他结点有且仅有一个父结点。

网状模型的特点是:形状象一张网,允许一个以上的结点无父结点,一个结点可以有多于一个的父结点。

关系模型的特点是:在关系中,数据的逻辑结构是一张二维表。

该表满足每一列中的分量是类型相同的数据;列的顺序可以是任意的;行的顺序可以是任意的;表中的分量是不可再分割的最小数据项,即表中不允许有子表;表中的任意两行不能完全相同。

数字逻辑(第六版 白中英)课后习题答案

数字逻辑(第六版 白中英)课后习题答案

第四章习题答案1.设计4个寄存器堆。

解:2. 设计具有4个寄存器的队列。

解:3.设计具有4个寄存器的堆栈解:可用具有左移、右移的移位寄存器构成堆栈。

4.SRAM、DRAM的区别解:DRAM表示动态随机存取存储器,其基本存储单元是一个晶体管和一个电容器,是一种以电荷形式进行存储的半导体存储器,充满电荷的电容器代表逻辑“1”,“空”的电容器代表逻辑“0”。

数据存储在电容器中,电容存储的电荷一般是会慢慢泄漏的,因此内存需要不时地刷新。

电容需要电流进行充电,而电流充电的过程也是需要一定时间的,一般是0.2-0.18微秒(由于内存工作环境所限制,不可能无限制的提高电流的强度),在这个充电的过程中内存是不能被访问的。

DRAM拥有更高的密度,常常用于PC中的主存储器。

SRAM是静态的,存储单元由4个晶体管和两个电阻器构成,只要供电它就会保持一个值,没有刷新周期,因此SRAM 比DRAM要快。

SRAM常常用于高速缓冲存储器,因为它有更高的速率;5. 为什么DRAM采用行选通和列选通解:DRAM存储器读/写周期时,在行选通信号RAS有效下输入行地址,在列选通信号CAS有效下输入列地址。

如果是读周期,此位组内容被读出;如果是写周期,将总线上数据写入此位组。

由于DRAM需要不断刷新,最常用的是“只有行地址有效”的方法,按照这种方法,刷新时,是在RAS有效下输入刷新地址,存储体的列地址无效,一次选中存储体中的一行进行刷新。

每当一个行地址信号RAS有效选中某一行时,该行的所有存储体单元进行刷新。

6. 用ROM实现二进制码到余3码转换解:真值表如下:8421码余三码B B2B1B0G G2G1G00 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 00 1 1 11 0 0 0 1 0 0 10 0 1 1 0 1 0 0 0 1 0 1 0 1 1 00 1 1 11 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0最小项表达式为:G=∑)9,8,7,6,5(G2=∑)9,4,3,2,1(G1=∑)8,7,4,3,0(G0=∑)8,6,4,2,0(333阵列图为:7. 用ROM实现8位二进制码到8421码转换解:输入为8位二进制数,输出为3位BCD码,12位二进制数,所以,所需ROM的容量为:28*12=30728.ROM、EPROM和EEPROM的区别解:ROM 指的是“只读存储器”,即Read-Only Memory。

数据库系统及应用(第六版)第4章数据库及表的操作

数据库系统及应用(第六版)第4章数据库及表的操作

4.2 数据表操作
4.2.1 表的基本操作
1 表的打开、关闭和浏览
(1)菜单方式
4.2 数据表操作
4.2.1 表的基本操作
1 表的打开、关闭和浏览
(1)菜单方式
4.2 数据表操作
4.2.1 表的基本操作
1 表的打开、关闭和浏览
(2)“数据工作期”方式
4.2 数据表操作
4.2.1 表的基本操作
4.1 数据库操作
4.1.3 创建数据库表
4
修改表结构
(2)打开数据库修改数据表 如果数据库已经打开,则可以使用“数据库设计器”修改当前数据 库内所有的数据表。方法是首先在“数据库设计器”内单击选中某个数 据库表,然后执行【数据库】|【修改】菜单命令。或者右击数据库表 打开快捷菜单,执行【修改】菜单命令。还可以单击“数据库设计器” 工具栏内的“修改表”工具按钮。上述三种操作的目的都是为了打开 “表设计器”。
删除触发器:用于指定一个规则,每当用户对表中的记录进行删 除时触发该规则并进行相应的检查。如果表达式值为“假”,则记录 将不能被删除。
4.1 数据库操作
4.1.3 创建数据库表
4
修改表结构
(1)直接修改数据表 执行【文件】|【打开】菜单命令,打开表文件,然后执行【显示】| 【表设计器】菜单命令。使用这种方式可以在不打开数据库的情况下直接 修改数据库中的表,它等同于使用了以下两条命令: USE<表名> MODIFY STRUCTURE
4.1 数据库操作
4.1.4 添加和移去数据表
1 向数据库中添加表
当一个数据库被打开后,用户可以单击“数据库设计器”工具栏的 【添加表】按钮,或者执行【数据库】|【添加表(A)】菜单命令,显示 “打开”对话框,选择被添加的数据表,然后单击【确定】按钮,将该 表添加到数据库内。用户也可以使用命令方式向当前数据库添加数据表。

数据库系统基础教程第四章答案(完整资料).doc

数据库系统基础教程第四章答案(完整资料).doc

【最新整理,下载后即可编辑】SolutionsChapter 4 4.1.14.1.2a)b)c)In c we assume that a phone and address can only belong to a single customer (1-m relationship represented by arrow into customer).d)In d we assume that an address can only belong to one customer and a phone can exist at only one address.If the multiplicity of above relationships were m-to-n, the entity set becomes weak and the key ssNo of customers will be needed as part of the composite key of the entity set.In c&d, we convert attributes phones and addresses to entity sets. Since entity sets often become relations in relational design,we must consider more efficient alternatives.Instead of querying multiple tables where key values are duplicated, we can also modify attributes:(i) Phones attribute can be converted into HomePhone, OfficePhone and CellPhone.(ii) A multivalued attribute such as alias can be kept as an attribute where a single column can be used in relational design i.e. concatenate all values. SQL allows a query "like '%Junius%'" to search the multiple values in a column alias.4.1.34.1.4a)b)c)The relationship "played" between Teams and Players is similar to relationship "plays" between Teams and Players.4.1.54.1.6 The information about children can be ascertained from motherOf and fatherOf relationships. Attribute ssNo is required since names are not unique.4.1.74.1.8a)(b)4.1.9AssumptionsA Professor only works in at most one department.A course has at most one TA.A course is only taught by one professor and offered by one department. Students and professors have been assigned unique email ids.A course is uniquely identified by the course no, section no, and semester (e.g. cs157-3 spring 09).4.1.10Given that for each movie, a unique studio exists that produces the movie. Each star is contracted to at most one studio.But stars could be unemployed at a given time. Thus the four-way relationship in fig 4.6 can be easily into converted equivalent relationships.4.2.1Redundancy: The owner address is repeated in AccSets and Addresses entity sets. Simplicity: AccSets does not serve any useful purpose and the design can be more simply represented by creating many-to-many relationship between Customers and Accounts.Right kind of element: The entity set Addresses has a single attribute address. A customer cannot have more than one address.Hence address should be an attribute of entity set Customers. Faithfulness: Customers cannot be uniquely identified by their names. In real world Customers would have a unique attribute such as ssNo or customerNo 4.2.2Studios and Presidents can be combined into one entity set Studios with Presidents becoming an attribute of Studios under following circumstances:1. The Presidents entity set only contains a simple attribute viz. presidentName. Additional attributes specific to Presidents might justify making Presidents into an entity set.4.2.34.2.4 The entity sets should have single attribute.a) Stars: starNameb) Movies: movieNamec) Studios: studioName. However there exists a many-to-many relationship between Studios and Contracts. Hence, in addition, we need more informationabout studios involved. If a contract always involves two studios, two attributes such as producingStudio and starStudio can replace theStudios entity set. If a contact can be associated with at most five studios, it may be possible to replace the Studios entity set by five attributes viz. studio1, studio2, studio3, studio4, and studio5. Alternately, a composite attribute containing concatenation of all studio names in a contact can be considered. A separator character such as "$" can be used. SQL allows searching of such an attribute using query like '%keyword%'4.2.5From Augmentation rule of Functional Dependency,givenB -> M (B=Baby, M=Mother)thenBND -> M (N=Nurse, D=Doctor)Hence we can just put an arrow entering mother.a) Put an arrow entering entity set Mothers for the simplest solution (As in fig. 4.4, where a multi-way relationship was allowed, even though Movies alone could identify the Studio). However, we can display more accurate information with below figure.b)c)Again from Augmentation rule of Functional Dependency, givenBM -> DthenBMN -> DThus we can just add an arrow entering Doctors to fig 4.15. Below figure represents more accurate information however.4.2.6a)b) Transitivity and Augmentation rules of Functional Dependency allow arrow entering Mothers from Births. However, a new relationship in below figure represents more accurate information.c)Design flaws in abc above 1. As suggested above, using Transitivity and Augmentation rules of Functional Dependency, much simpler design is possible.4.2.7In below figure there exists a many-to-one relationship between Babies and Births and another many-to-one relationship between Births and Mothers. From transitivity of relationships, there is a many-to-one relationship between Babiesand Mothers. Hence a baby has a unique mother while a birth can allow more than one baby.4.3.1a)b)A captain cannot exist without a team. However a player can (free agent). A recently formed (or defunct) team can exist without players or colors.c)Children can exist without mother and father (unknown).4.3.2a)The keys of both E1 and E2 are required for uniquely identifying tuples in R b)The key of E1c)The key of E2d)The key of either E1 or E24.3.3Special Case: All entity sets have arrows going into them i.e. all relationships are 1-to-1Any KiOtherwise: Combination of all Ki's where there does not exist an arrow going from R to Ei.4.4.1No, grade is not part of the key for enrollments. The keys of Students and Courses become keys of the weak entity set Enrollments.4.4.2It is possible to make assignment number a weak key of Enrollments but this is not good design (redundancy since multiple assignments correspond to a course).A new entity set Assignment is created and it is also a weak entity set. Hence the key attributes of Assignment will come from the strong entity sets to which Enrollments is connected i.e. studentID, dept, and CourseNo.4.4.3a)b)c)4.4.4a)b)4.5.1Customers(SSNo,name,addr,phone)Flights(number,day,aircraft)Bookings(custSSNo,flightNo,flightDay,row,seat)Relations for toCust and toFlt relationships are not required since the weak entity set Bookings already contains the keys of Customers and Flights.4.5.2(a)(b)Schema is changed. Since toCust is no longer an identifying relationship, SSNo is no longer a part of Bookings relation.Bookings(flightNo,flightDay,row,seat)ToCust(custSSNO,flightNo,flightDay,row,seat)The above relations are merged intoBookings(flightNo,flightDay,row,seat,custSSNo)However custSSNo is no longer a key of Bookings relation. It becomes a foreign key instead.4.5.3Ships(name, yearLaunched)SisterOf(name, sisterName)4.5.4(a)Stars(name,addr)Studios(name,addr)Movies(title,year,length,genre)Contracts(starName,movieTitle,movieYear,studioName,salary)Depending on other relationships not shown in ER diagram, studioName may not be required as a key of Contracts (or not even required as an attribute of Contracts).(b)Students(studentID)Courses(dept,courseNo)Enrollments(studentID,dept,courseNo,grade)(c)Departments(name)Courses(deptName,number)(d)Leagues(name)Teams(leagueName,teamName)Players(leagueName,teamName,playerName)4.6.1The weak relation Courses has the key from Depts along with number. Hence there is no relation for GivenBy relationship.(a)Depts(name, chair)Courses(number, deptName, room)LabCourses(number, deptName, allocation)(b) LabCourses has all the attributes of Courses.Depts(name, chair)Courses(number, deptName, room)LabCourses(number, deptName, room, allocation)(c) Courses and LabCourses are combined into one relation.Depts(name, chair)Courses(number, deptName, room, allocation)4.6.2(a)Person(name,address)ChildOf(personName,personAddress,childName,childAddress)Child(name,address,fatherName,fatherAddress,motherName,motherAddresss) Father(name,address,wifeName,wifeAddresss)Mother(name,address)Since FatherOf and MotherOf are many-one relationships from Child, there is no need for a separate relation for them. Similarly the one-one relationship Married can be included in Father (or Mother). ChildOf is a many-many relationship and needs a separate relation.However the ChildOf relation is not required since the relationship can be deduced from FatherOf and MotherOf relationships contained in Child relation.(b)A person cannot be both Mother and Father.Person(name,address)PersonChild(name,address)PersonChildFather(name,address)PersonChildMother(name,address)PersonFather(name,address)PersonMother(name,address)ChildOf(personName,personAddress,childName,childAddress)FatherOf(childName,childAddress,fatherName,fatherAddress)MotherOf(childName,childAddress,motherName,motherAddress)Married(husbandName,husbandAddress,wifeName,wifeAddress)The many-many ChildOf relationship again requires a relation.An entity belongs to one and only one class when using object-oriented approach. Hence, the many-one relations MotherOf and FatherOf could be added as attributes to PersonChild,PersonChildFather, and PersonChildMother relations.Similarly the Married relation can be added as attributes to PersonChildMother and PersonMother (or the corresponding father relations).(c) For the Person relation at least one of husband and wife attributes will be null. Person(personName,personAddress,fatherName,fatherAddress,motherName,mot herAddresss,wifeName,wifeAddresss,husbandName,husbandAddress) ChildOf(personName,personAddress,childName,childAddress)4.6.3(a)People(name,fatherName,motherName)Males(name)Females(name)Fathers(name)Mothers(name)ChildOf(personName,childName)(b)People(name)PeopleMale(name)PeopleMaleFathers(name)PeopleFemale(name)PeopleFemaleMothers(name)ChildOf(personName,childName)FatherOf(childName,fatherName)MotherOf(childName,motherName)People cannot belong to both male and female branch of the ER diagram. Moreover since an entity belongs to one and only one class when using object-oriented approach, no entity belongs to People relation.Again we could replace MotherOf and FatherOf relations by adding as attributes to PeopleMale,PeopleMaleFathers,PeopleFemale, and PeopleFemaleMothers relations.(c)People(name,fatherName,motherName)ChildOf(personName,childName)4.6.4(a)Each entity set results in one relation. Thus both the minimum and maximum number of relations is e.The root relation has a attributes including k keys. Thus the minimum number of attributes is a. All other relations include the k keys from root along with their a attributes. Thus the maximum number of attributes is a+k.(b)The relation for root will have a attributes. The relation representing the whole tree will have e*a attributes.The number of relations will depend on the shape of the tree. A tree of e entities where only one child exists(say left child only) would have the minimum number of relations. Thus below figure will only contain 4 subtrees that contain rootE1,E1E2,E1E2E3, and E1E2E3E4. With e entity sets, minimum e relations are possible.The maximum number of subtrees result when all the entities(except root) are at depth 1. Thus below figure will contain 8 subtrees that contain rootE1,E1E2,E1E3,E1E4,E1E2E3,E1E3E4,E1E2E4,and E1E2E3E4. With e entity sets, maximum 2^(e-1) relations are possible.The nulls method always results in one relation and contains attributes from all e entities i.e. e*a attributes.Summarizing for a,b, and c above;#Components #RelationsMin Max Min MaxMethodstraight-E/R a a e eobject-oriented a e*a e 2^(e-1)nulls e*a e*a 1 14.7.14.7.2a)b)c)d)4.7.34.7.44.7.5Males and Females subclasses are complete. Mothers and Fathers are partial. All subclasses are disjoint.4.7.6。

数据库原理习题与答案 第4章关系数据库方法

数据库原理习题与答案 第4章关系数据库方法

第四章.关系数据库方法习题:一.填空题1.关系操作的特点是。

2.一个关系模式的定义格式为。

3.在一个实体的表示信息中,称为关键字。

4.关系代数运算中,传统的集合运算有、、和。

5.关系代数使用对关系的运算来表达查询的,而关系演算是用查询的,它又分为演算和演算两种。

二.选择题1.关系数据库管理系统应能实现的专门关系运算包括。

A.排序、索引、统计B.选择、投影、连接C.关联、更新、排序D.显示、打印、制表2.通常情况下,下面的关系中不可以作为关系数据库的关系是。

A.R1(学生号,学生名,性别)B.R2(学生号,学生名,班级号)C.R3(学生号,学生名,宿舍号)D.R4(学生号,学生名,简历)3.自然连接是构成新关系的有效方法,一般情况下,当对关系R和S使用自然连接时,要求R和S含有一个或多个共有的。

A.元组B.行C.记录D.属性4.设有如图所示的关系R,经操作ΠA,B(σB=b(R))的运算结果是______。

关系R:三.简答题1. 试述关系模型的三个组成部分。

2. 试述关系数据语言的特点和分类。

3. 试述关系模型的完整性规则。

在参照完整性中,为什么外部码属性的值也可以为空?什么情况下才可以为空?四.设有如图所示的三个关系S 、C 和SC ,将下列关系代数表达式用汉语表示出来,并求其结果。

A BCD1.∏学号,姓名,课程号(σ籍贯=‘上海’(S∞SC))2.∏姓名,课程号,成绩(S∞SC∞σ课程名=‘操作系统’(C))3.∏姓名,年龄(S∞(∏学号,课程号(SC)÷∏课程号(C)))参考答案:一.填空题1.集合2.关系名(属性名1,属性名2,……属性名n)3.能唯一标识实体的属性或属性组4.笛卡尔积,并,交,差5.谓词表达,元组关系,域关系二.选择题1. B2. D3. D4. C三.简答题1.关系模型由关系数据结构、关系操作集合和关系完整性约束三部分组成。

2.关系数据语言可以分为三类:(1)关系代数语言(2)关系演算语言,分为关系演算语言和域关系演算语言(3)具有关系代数和关系演算双重特点的语言,例如SQL这些关系数据语言的共同特点是:具有完备的表达能力,是非过程化的集合操作语言,功能强,能够嵌入高级语言中使用。

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

4.1
a.
select ID,name,count(year) as num_of_sections
from instructor natural left outer join teaches
group by ID,name;
b. select ID, name,
(select count(*) as num_of_sections
from teaches T where t.ID = I.ID)
from instructor I;
c. select course_id,section_id ,ID ,
decode(name,null,'-',name )
from (section natural left outer join teaches natural left outer join instructor)
natural left outer join instructor
where semester='Spring' and year='2010';
d. select dept_name ,count(ID)
from department natural left outer join instructor
group by dept_name;
4.2
a. select * from student natural join takes
union
select ID,name,dept_name,tot_cred,null,null,null,null,null
from student S1 where not exists
(select ID from takes T1 where T1.id = S1.id);
b. //
4.4
a. Consider that,if a teacher teaches ELec.Eng. in physics department,
but it will be lost in the natural join of instructor,teaches and course.because the instructor department name does not match the name of department.The example is :
instructor={(12345,'math','physics',10000)}
teaches={(12345,'EE123',1,'spring',2009)}
course={('EE123','Eng.','Elec.Eng.',6)}
b.Instructors who have not taught a signal course should have a number of sections
c. Consider the query:
select * from teaches natural join instructor.
in this query, some sections will be lost if teaches.id is allowed to be null,and thus tuples do exist. And if teaches.id is o foregin key to instructor ,such tuples will not be created and sush error won't be detected in the query above.
4.8
a.//
b.//
4.14
create view tot_credits(year,tot_credits)
as
(select year,sum(credits)
from takes natural join course
group by year)。

相关文档
最新文档