数据库第四章练习题答案
- 格式:doc
- 大小:34.00 KB
- 文档页数:3
1、设计性能较优的关系模式称为规范化,规范化主要的理论依据是()。
A.关系规范化理论B.关系运算理论C.关系代数理论D.数理逻辑正确答案:A2、下列关于规范化理论各项中正确的是()。
A.对于一个关系模式来说,规范化越深越好B.满足第二范式的关系模式一定满足第一范式C.第一范式要求---非主码属性完全函数依赖关键字D.规范化一般是通过分解各个关系模式实现的,但有时也有合并正确答案:B3、X→Y能从推理规则导出的充分必要条件是()。
A.B.C.D.正确答案:D4、两个函数依赖集F和G等价的充分必要条件是()。
A.B.C.D.5、设有关系模式R(A,B,C,D,E),函数依赖集F={A→B,B→C,C→D,D→A},ρ={AB,BC,AD}是R上的一个分解,那么分解ρ相对于F()。
A.既是无损连接分解,又是保持函数依赖的分解B.是无损连接分解,但不是保持函数依赖的分解C.不是无损连接分解,但是保持函数依赖的分解D.既不是无损连接分解,也不是保持函数依赖的分解正确答案:D6、关系模式中,满足2NF的模式()。
A.可能是1NFB.必定是1NFC.必定是3NFD.必定是BCNF正确答案:B7、不能使一个关系从第一范式转化为第二范式的条件是()。
A.每一个非主属性都完全函数依赖于主码B.每一个非主属性都部分函数依赖于主码C.关系中没有非主属性D.主码由一个属性构成正确答案:B二、判断题1、关系模式的分解是唯一的。
(错)2、一个关系模式属于BC范式,它一定属于第三范式。
(对)3、在关系模式R(U,F)中,如果X→Y,且存在X的真子集X1,使X1→Y,称函数依赖X→Y为完全函数依赖。
(错)4、函数依赖集F={A→B,B→C,C→A,C→B,A→C,BC→A},它的最小函数依赖集Fmin={A→B,B→C,A→C}。
(错)三、填空题1、被函数依赖集F逻辑蕴涵的函数依赖的全体构成的集合,称为。
正确答案:函数依赖集F的闭包2、设有关系模式R(A,B,C,D,M,N),函数依赖集F={N→D,M→D,D→B,BC→D,DC→N},R的候选码为。
第4章数据库安全性1 .什么是数据库的安全性?答:数据库的安全性是指保护数据库以防止不合法的使用所造成的数据泄露、更改或破坏。
2 .数据库安全性和计算机系统的安全性有什么关系?答:安全性问题不是数据库系统所独有的,所有计算机系统都有这个问题。
只是在数据库系统中大量数据集中存放,而且为许多最终用户直接共享,从而使安全性问题更为突出。
系统安全保护措施是否有效是数据库系统的主要指标之一。
数据库的安全性和计算机系统的安全性,包括操作系统、网络系统的安全性是紧密联系、相互支持的,3 .试述可信计算机系统评测标准的情况,试述TDI / TCSEC 标准的基本内容。
答:各个国家在计算机安全技术方面都建立了一套可信标准。
目前各国引用或制定的一系列安全标准中,最重要的是美国国防部(DoD )正式颁布的《DoD 可信计算机系统评估标准》(伽sted Co 哪uter system Evaluation criteria ,简称TcsEc ,又称桔皮书)。
(TDI / TCSEC 标准是将TcsEc 扩展到数据库管理系统,即《可信计算机系统评估标准关于可信数据库系统的解释》(Tmsted Database Interpretation 简称TDI , 又称紫皮书)。
在TDI 中定义了数据库管理系统的设计与实现中需满足和用以进行安全性级别评估的标准。
TDI 与TcsEc 一样,从安全策略、责任、保证和文档四个方面来描述安全性级别划分的指标。
每个方面又细分为若干项。
4 .试述T csEC ( TDI )将系统安全级别划分为4 组7 个等级的基本内容。
答:根据计算机系统对安全性各项指标的支持情况,TCSEC ( TDI )将系统划分为四组(division ) 7 个等级,依次是D 、C ( CI , CZ )、B ( BI , BZ , B3 )、A ( AI ) ,按系统可靠或可信程度逐渐增高。
这些安全级别之间具有一种偏序向下兼容的关系,即较高安全性级别提供的安全保护包含较低级别的所有保护要求,同时提供更多或更完善的保护能力。
第四章一、填空题1.椭圆框2.属性3.关系4.物理设计5.一个或多个二、判断题1.错2.对3.错4.对5.错三、选择题1. B2. B3. C4.B、C、D5.B、C四、简答题1.请简述数据库设计规范化的必要性。
答:数据库设计对数据的存储性能、数据的操作都有很大的关系。
为了避免不规范的数据库出现数据冗余,造成插入、删除、更新操作异常等情况,就要进行数据库设计规范化。
2.请分析数据库范式1NF、2NF、3NF的区别。
答:(1)1NF:数据库表的每一列都是不可分割的基本数据项。
(2)2NF:在满足1NF基础上,遵从唯一性,非主键字段需完全依赖主键(3)3NF:在满足2NF基础上,非主键字段不能相互依赖。
五、实训题1.请完成电子商务网站用户等级的数据表设计。
CREATE TABLE sh_user_level (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '等级id',name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '等级名称',config VARCHAR(255) NOT NULL DEFAULT '' COMMENT '满足条件') DEFAULT CHARSET=utf8;12.请完成电子商务网站用户订阅、用户收藏的数据表设计。
# 订阅CREATE TABLE sh_user_subscribe (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '订阅id',email VARCHAR(60) COMMENT '邮箱地址',status INT COMMENT '是否确认,0未确认,1已确认',code VARCHAR(10) COMMENT '邮箱确认的验证码',add_time INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '开始订阅时间') DEFAULT CHARSET=utf8;# 收藏CREATE TABLE sh_user_favorite (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '收藏id',user_id INT UNSIGNED NOT NULL COMMENT '用户id',goods_id INT UNSIGNED NOT NULL COMMENT '商品id',add_time INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '收藏时间') DEFAULT CHARSET=utf8;2。
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. Sinceentity 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。
第四章练习题一、选择题1、设有两个关系R(A,B)与S(B,C),与下列SELECT语句SELECT A,B FROM RWHERE B NOT IN(SELECT B FROM S WHERE C='C56');等价的关系代数表达式就是 [ C]A.πA,B(σC≠'C56'(R⋈S))B.πA,B(R ⋈ S)C≠'C56'C.R-πA,B(σC= 'C56'(R⋈S))D.R-πA,B(σC≠'C56'(R⋈S))2、嵌入式SQL的预处理方式,就是指[ B]A.识别出SQL语句,加上前缀标识与结束标志B.把嵌入的SQL语句处理成函数调用形式C.对源程序进行格式化处理D.把嵌入的SQL语句编译成目标程序3、SQL中,“DELETE FROM 表名”表示 [ A]A.从基本表中删除所有元组B.从基本表中删除所有属性C.从数据库中撤消这个基本表D.从基本表中删除重复元组4、SQL中,聚合函数COUNT(列名)用于[ C]A.计算元组个数B.计算属性的个数C.对一列中的非空值计算个数D.对一列中的非空值与空值计算个数5、在传统SQL技术中,使用“ORDER BY”子句的SELECT语句查询的结果,实际上为 [B]A.数组B.列表C.包D.集合6、在数据库中,能提高查询速度的就是(C )A、数据依赖B、视图C、索引D、数据压缩7、语句 delete from sc 表明( A )A、删除sc中的全部记录B、删除基本表scC、删除基本表sc中的列数据D、删除基本表sc中的部分行8、在DB应用中,一般一条SQL 语句可产生或处理一组记录,而DB主语言语句一般一次只能处理一条记录,其协调可通过( B )实现。
A、指针B、游标C、数组D、栈9、在下列基本表的定义中,数值5表示( C )CREATE TABLE student (Sno char(5) not null unique,Sname char(2));A、表中有5条记录B、表中有5列C、表中字符串Sno 的长度D、表格的大小10、在视图上不能完成的操作就是( C )A、更新视图B、查询C、在视图上定义新的基本表D、在视图上定义新视图11、下列聚合函数中不忽略空值 (null) 的就是 (C)A、SUM (列名)B、MAX (列名)C、COUNT ( * )D、AVG (列名)12、SQL的查询语句的where子句中,对空值的操作,不正确的就是 ( C )A、where AGE IS NULLB、where AGE IS NOT NULLC、where AGE = NULLD、where NOT (AGE IS NULL)13、Transact-SQL对标准SQL的扩展主要表现为( A )。
数据库原理关系运算习题答案数据库系统原理第四章关系运算课后习题答案4.1 名词解释(1)关系模型:用二维表格结构表示实体集,外键表示实体间联系的数据模型称为关系模型。
(2)关系模式:关系模式实际上就是记录类型。
它的定义包括:模式名,属性名,值域名以及模式的主键。
关系模式不涉及到物理存储方面的描述,仅仅是对数据特性的描述。
(3)关系实例:元组的集合称为关系和实例,一个关系即一张二维表格。
(4)属性:实体的一个特征。
在关系模型中,字段称为属性。
(5)域:在关系中,每一个属性都有一个取值范围,称为属性的值域,简称域。
(6)元组:在关系中,记录称为元组。
元组对应表中的一行;表示一个实体。
(7)超键:在关系中能唯一标识元组的属性集称为关系模式的超键。
(8)候选键:不含有多余属性的超键称为候选键。
(9)主键:用户选作元组标识的一个候选键为主键。
(单独出现,要先解释“候选键”)(10)外键:某个关系的主键相应的属性在另一关系中出现,此时该主键在就是另一关系的外键,如有两个关系S和SC,其中S#是关系S的主键,相应的属性S#在关系SC中也出现,此时S#就是关系SC 的外键。
(11)实体完整性规则:这条规则要求关系中元组在组成主键的属性上不能有空值。
如果出现空值,那么主键值就起不了唯一标识元组的作用。
(12)参照完整性规则:这条规则要求“不引用不存在的实体”。
其形式定义如下:如果属性集K是关系模式R1的主键,K也是关系模式R2的外键,那么R2的关系中, K的取值只允许有两种可能,或者为空值,或者等于R1关系中某个主键值。
这条规则在使用时有三点应注意:1)外键和相应的主键可以不同名,只要定义在相同值域上即可。
2)R1和R2也可以是同一个关系模式,表示了属性之间的联系。
3)外键值是否允许空应视具体问题而定。
(13)过程性语言:在编程时必须给出获得结果的操作步骤,即“干什么”和“怎么干”。
如Pascal和C语言等。
(14)非过程性语言:编程时只须指出需要什么信息,不必给出具体的操作步骤。
习题61、说明数据库设计的特点。
1)三分技术,七分管理,十二分基础数据2)综合性3)结构(数据)设计和行为(处理)设计相结合2、试述数据库设计的过程3、试述数据库设计过程的各个阶段设计内容。
1)需求分析阶段需求分析是对用户提出的各种要求加以分析,对各种原始数据加以综合、整理,是形成最终设计目标的首要阶段。
需求分析是整个设计过程的基础,是最困难、最耗费时间的一步。
2)概念结构设计阶段概念结构设计是对用户需求进行进一步抽象、归纳,并形成独立于DBMS和有关软、硬件的概念数据模型的设计过程。
3)逻辑结构设计阶段逻辑结构设计是将概念结构转换为某个DBMS所支持的数据模型,并对其进行优化的设计过程。
4)物理设计阶段数据库物理设计阶段,是将逻辑结构设计阶段所产生的逻辑数据模型,转换为某种计算机系统所支持的数据库物理结构的实现过程。
5)数据库实施阶段数据库实施阶段,即数据库调试、试运行阶段。
一旦数据库的物理结构形成,就可以用已选定的DBMS来定义、描述相应的数据库结构,装入数据库数据库,以生成完整的数据库,编制有关应用程序,进行联机调试并转入试运行,同时进行时间、空间等性能分析。
6)数据库运行和维护阶段数据库实施阶段结束,标志着数据库系统投入正常运行工作的开始。
在数据库系统运行过程中必须不断地对其进行评价、调整与修改。
4、需求分析中发现事实的方法有哪些?1)跟班作业。
通过亲身参加业务工作来观察和了解业务活动的情况。
2)开调查会。
通过与用户座谈来了解业务活动的情况及用户需求。
3)检查文档。
通过检查与当前系统有关的文档、表格、报告和文件等,进一步理解原系统,并有利于提供与原系统问题相关的业务信息。
4)问卷调查。
5、需求分析阶段的设计目标是什么?调查的内容是什么?需求分析阶段的目标是通过详细调查现实世界要处理的对象(组织、部门、企业等),充分了解原系统(手工系统或计算机系统)工作概况,确定企业的组织目标,明确用户的各种需求,进而确定新系统的功能,并把这些要求写成用户和数据库设计者都能够接受的文档。
(完整版)数据库练习题(答案)..第四章练习题⼀、选择题1、设有两个关系R(A,B)和S(B,C),与下列SELECT语句SELECT A,B FROM RWHERE B NOT IN(SELECT B FROM S WHERE C='C56');等价的关系代数表达式是 [ C]A.πA,B(σC≠'C56'(R?S))B.πA,B(R ? S)C≠'C56' C.R-πA,B(σC= 'C56'(R?S))D.R-πA,B(σC≠'C56'(R?S))2、嵌⼊式SQL的预处理⽅式,是指[ B]A.识别出SQL语句,加上前缀标识和结束标志B.把嵌⼊的SQL语句处理成函数调⽤形式C.对源程序进⾏格式化处理D.把嵌⼊的SQL语句编译成⽬标程序3、SQL中,“DELETE FROM 表名”表⽰ [ A]A.从基本表中删除所有元组B.从基本表中删除所有属性C.从数据库中撤消这个基本表D.从基本表中删除重复元组4、SQL中,聚合函数COUNT(列名)⽤于[ C]A.计算元组个数B.计算属性的个数C.对⼀列中的⾮空值计算个数D.对⼀列中的⾮空值和空值计算个数5、在传统SQL技术中,使⽤“ORDER BY”⼦句的SELECT语句查询的结果,实际上为 [B]A.数组 B.列表 C.包 D.集合6、在数据库中,能提⾼查询速度的是(C )A. 数据依赖B. 视图C. 索引D. 数据压缩7、语句 delete from sc 表明( A )A. 删除sc中的全部记录B. 删除基本表scC. 删除基本表sc中的列数据D. 删除基本表sc中的部分⾏8、在DB应⽤中,⼀般⼀条SQL 语句可产⽣或处理⼀组记录,⽽DB主语⾔语句⼀般⼀次只能处理⼀条记录,其协调可通过(B )实现。
A. 指针B. 游标C. 数组D. 栈9、在下列基本表的定义中,数值5表⽰( C )CREATE TABLE student (Sno char(5) not null unique,Sname char(2));A. 表中有5条记录B. 表中有5列C. 表中字符串Sno 的长度D. 表格的⼤⼩10、在视图上不能完成的操作是( C )A. 更新视图B. 查询C. 在视图上定义新的基本表D. 在视图上定义新视图11、下列聚合函数中不忽略空值 (null) 的是(C)A、SUM (列名)B、MAX (列名)C、COUNT ( * )D、AVG (列名)12、SQL的查询语句的where⼦句中,对空值的操作,不正确的是( C )A、where AGE IS NULLB、where AGE IS NOT NULLC、where AGE = NULLD、where NOT (AGE IS NULL)13、Transact-SQL对标准SQL的扩展主要表现为( A )。
mysql数据库武洪萍版第四章习题与答案一.选择题1.下面哪种数字数据类型不可以存储数据256?(D)A.bigintB.intC.SmallintD.tinyint2.下面是有关主键和外键之间的关系描述,正确的是(AC)A.一个表最多只能有一个主键约束,多个外键约束。
B.一个表中最多只有一个外键约束,一个主键约束。
C.在定义主键外键约束时,应该首先定义主键约束,然后定义外键约束。
D.在定义主键外键约束时,应该首先定义主键约束,然后定义主键约束。
3.下面关于数据库中表的行和列的叙述正确的是(D)A.表中的行是有序的,列是无序的B.表中的列是有序的,行是无序的C.表中的行和列都是有序的D.表中的行和列都是无序的4.SQL语言的数据操作语句包括SELECT、INSERT、UPDATE、DELETE 等。
其中最重要的,也是使用最频繁的语句是(A)A.SELECTB.INSERTC.UPDATED.DELETE5.在下列SQL语句中,修改表结构的语句是(A)。
A.ALTERB.CREATEC.UPDATED.INSERT6.设有关系R(A,B,C)和S(C,D),与关系代数表达式πA,B,D(σR.C=S.C(R∞S)等价的SQL语句是(B)。
A.SELECT某FROMR,SWHERER.C=S.CB.SELECTA,B,DFROMR,SWHERER.C=.SELECTA,B,D FROMR,SWHERER=SD.SELECTA,BFROMRWHERE(SELECTDFROMSWHERER.C=S.C)7.设关系R(A,B,C)与SQL语句“SELECTDISTINSTAFROMRWHEREB=17”等价的关系代数表达式是(A)A.πA(σB=17(R))B.σB=17(πA(R))C.σB=17(πA.C(R))D.πA.C(σB=17(R))下面第(8)-(12)题,基于“学生-选课-课程”数据库中的3个关系。
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.。