数据库第四章作业
第四章SQL查询语言
一. 简答题
1.SQL语言的使用方式?
答:可以独立的交互式使用,也可以通过与宿主语言结合起来使用。
2.完整的SQL包括哪三个子语言分类?
答:DDL、DML、DCL
3.SQL语言中定义的数据库与第一章中的数据库概念之异同?
答:SQL数据库:数据库是一个存储空间,用于存放数据库中的数据库对象。
第一章中的数据库是相互关联的数据集合。
相同点:都包含有对数据进行组织、管理等操作的意思;
不同点:SQL“数据库”,是指存在于物理磁盘上的一个存储空间,用于存放相关数据集合;而第一章中的“数据库”,是一个宽泛的概念,任何东西都可以。
4.标准的SQL语言与实际数据库产品中的SQL数据库语言的关系?
答:绝大多数RDBMS产品并不完全支持标准SQL,也就是SQL中的某些功能在实际数据库产品中可能没有得到支持。
5.SQL语言对数据库对象的定义使用哪三个SQL命令关键字?
答:Create、Alter、Drop
6.定义索引的目的?定义的索引由谁使用?
答:索引是数据位置信息的关键字表,利用索引,系统可以较快地在磁盘上定位所需数据。
用户不能在取数据时选择索引,索引的选择是由系统自动进行的。
也就是索引建立后,有DBMS根据需要自动选择使用。
7.在什么情况下,SELECT查询中的ORDER BY子句、COMPUTER和COMPUTER BY子
句可以不要?
答:ORDER BY子句为排序而设置、COMPUTER子句为整个结果汇总而设置、COMPUTER BY子句为分组统计而设置。
SELECT...INTO不能与COMPUTER一起使用;当用UNION时,各个SELECT语句不能有ORDER BY子句、COMPUTER子句,
8.在SELECT查询中,哪三个子句可以实施对数据的过滤或筛选?
答:WHERE用于对FROM子句结果设置过滤条件;
GROUP BY用于对WHERE子句的结果分组;
HA VING用于对分组数据集合的再筛选。
二. 单项选择题
1.某WHERE子句中有'X%[^a-f]_[4-8]'串表达,下列(C )可以正确匹配。
A. XTa_9
B. XTb_8
C. XTTTr7
D. XSSSe3
2.视图中的数据来自所基于的( C )。
A. 列
B. 行
C. 表
D. 自身
二. 根据教材P390的表11-1~11-4和P393~P394表11-5~11-8,完成如下任务:
1.写出创建Salary表的代码;
答:CREATE TABLE salary20122735
( P_no char(6) PRIMARY KEY,
Base Dec(5) NULL,
Bonus Dec(5) NULL,
Fact AS Base + Bonus,
CONSTRAINT person20122735_contr
FOREIGN KEY (P_no)
REFERENCES person20122735(P_no)
ON DELETE CASCADE
ON UPDATE CASCADE,
)
2.写出向表Person插入一行(表11-5中的任一行均可)数据的代码;
答:INSERT INTO person20122735
V ALUES('000001','林峰','男','1973-04-07','2003-08-03','销售部','000007')
3.将表11-5中“000006”的Deptname由“销售部”改为“财务部”,写出代码;
答:UPDATE person20122735
set Deptname='财务部'
WHERE P_no='000006'
4.删除表11-5中“000002”员工,写出代码;
答:DLETE FROM person20122735 WHERE P_no='000002'
5.查询“销售部”基本工资在2000以上的员工姓名及基本工资,写出查询代码。
答:SELECT P_name 姓名,Base 基本工资
FROM person20122735,salary20122735
WHERE Deptname='销售部'AND Base>'2000'AND
person20122735.P_no=salary20122735.P_no。
04数据库应用技术2023年国开,第一次形考作业,第4章
一.单项选择题(每小题2分,共10个题,20分)1.所述功能中,不属于SQL语言功能的是( D )。
单选题(2 分)A.数据表的定义功能B.数据查询功能C.数据增、删、改功能D.提供方便的用户操作界面功能2.设某职工表中有用于存放年龄(整数)的列,最适合年龄列的是( C )。
单选题(2 分)A.intB.smallintC.tinyintD.bit3.用于限制列的取值范围的约束是( B )。
单选题(2 分)A.PRIMARY KEYB.CHECKDEFAULTD.UNIQUE4.用于限制列取值不重的约束是( D )。
单选题(2 分)A.PRIMARY KEYB.CHECKC.DEFAULTD.UNIQUE5.用于实现实体完整性的是( A )。
单选题(2 分)A.PRIMARY KEYB.CHECKC.DEFAULTD.UNIQUE6.关于DEFAULT约束的说法,错误的是( D )。
单选题(2 分)A.一个DEFAULT约束只能约束表中的一个列B.在一个表上可以定义多个DEFAULT约束C.DEFAULT只能定义在列级完整性约束处D.在列级完整性约束和表级完整性约束处都可以定义DEFAULT约束7.为变量赋值的语句中,错误的是( D )。
单选题(2 分)A.SET @X = 10B.SELECT @X = 10C.SET @X = 10 + 10D.SET @X = 10 + 'A'8.设有表T,现要在该表新增加一个列,列名为:c1,类型为int。
能正确实现该功能的语句是( A )。
单选题(2 分)A.ALTER TABLE T ADD c1 intB.ALTER TABLE T ADD (c1 int)C.ALTER TABLE T ADD COLUMN c1 intD.ALTER TABLE T ADD COLUMN (c1 int)9.属于普通编码可变长字符串类型的是( D )。
数据库原理 第四章关系数据库理论期末习题与答案
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章_关系数据库设计理论
第4章关系数据库设计理论习题一、选择题1、C2、B3、C4、C5、A6、B7、A8、B9、D10、B二、填空题1、数据依赖主要包括_函数_依赖、_多值_依赖和连接依赖。
2、一个不好的关系模式会存在_插入异常_、_删除异常_和__修改复杂_等弊端。
3、设X→Y为R上的一个函数依赖,若_对任意X的真子集X’,均无X’→Y 存在__,则称Y完全函数依赖于X。
4、设关系模式R上有函数依赖X→Y和Y→Z成立,若_Y不包含于X_且_Y→X不成立_,则称Z传递函数依赖于X。
5、设关系模式R的属性集为U,K为U的子集,若_K→U为完全函数依赖_,则称K为R的候选键。
6、包含R中全部属性的候选键称_主属性_。
不在任何候选键中的属性称__非主属性_。
7、Armstrong公理系统是_有效__的和_完备__的。
8、第三范式是基于_函数_依赖的范式,第四范式是基于_多值_依赖的范式。
9、关系数据库中的关系模式至少应属于_第一_范式。
10、规范化过程,是通过投影分解,把_一个范式级别较低的_的关系模式“分解”为_若干个范式级别较高__的关系模式。
三、简答题1、解释下列术语的含义:函数依赖、平凡函数依赖、非平凡函数依赖、部分函数依赖、完全函数依赖、传递函数依赖、范式、无损连接性、依赖保持性。
解:函数依赖:设关系模式R(U,F),U是属性全集,F是U上的函数依赖集,X和Y 是U 的子集,如果对于R(U)的任意一个可能的关系r,对于X的每一个具体值,Y都有唯一的具体的值与之对应,则称X函数决定Y,或Y函数依赖于X,记X→Y。
我们称X为决定因素,Y为依赖因素。
当Y不函数依赖于X时,记作:X Y。
当X→Y且Y→X时,则记作:X Y。
平凡函数依赖:当属性集Y是属性集X的子集时,则必然存在着函数依赖X→Y,这种类型的函数依赖称为平凡的函数依赖。
非平凡函数依赖:如果Y不是X子集,则称X→Y为非平凡的函数依赖。
完全函数依赖与部分函数依赖:设有关系模式R(U),U是属性全集,X和Y是U的子集,X→Y,并且对于X的任何一个真子集X',都有X'Y,则称Y对X完全函数依赖(Full−f Y。
数据库系统基础教程课后答案第四章
4.2.7 In 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 Babies and Mothers. Hence a baby has a unique mother while a birth can allow more than one baby.
4.1.7
4.1.8 a)
(b)
4.1.9
Assumptions A 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.2.4 The entity sets should have single attribute. a) Stars: starName b) Movies: movieName c) Studios: studioName. However there exists a many-to-many relationship between Studios and Contracts. Hence, in addition, we need more information about studios involved. If a contract always involves two studios, two attributes such as producingStudio and starStudio can replace the Studios 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章 习题答案
第4章关系数据库标准语言SQL1. 试述关系数据库标准语言SQL的特点。
解:SQL语言是一种功能强大、通用性好又简单易学的语言,主要特点包括:●综合统一:SQL语言将数据定义语言DDL、数据操纵语言DML、数据控制语言DCL的功能集于一体,语言风格统一,可以独立完成数据库生命周期中的全部活动。
●高度非过程化:用SQL语言进行数据操作只要提出“做什么”,而无需指明“怎么做”。
●面向集合操作:SQL采用集合操作方式。
不仅查询的结果可以是元组的集合,而且一次插入、更新、删除操作的对象也可以是元组的集合。
●支持关系数据库的三级模式结构:外模式对应于视图(View),模式对应于基本表,内模式对应于存储文件。
●简洁易学,灵活易用:SQL语言简洁,只有少量关键字,语法简单。
SQL的使用方式非常灵活,可以直接通过SQL实现人机交互,还可以将SQL语句嵌入到其他高级语言程序中来使用。
2. 概述SQL的基本功能。
解:SQL语言是一种综合的、通用的、功能强大的关系数据库语言,集数据查询(Data Query)、数据操纵(Data Manipulation)、数据定义(Data Definition)和数据控制(Data Control)功能于一体。
数据定义功能。
主要用于定义、删除和修改关系数据库中的对象,数据库对象主要包括基本表、视图等。
数据查询功能。
运用SELECT语句来实现查询数据的功能。
数据操纵功能。
主要用于增加、删除和修改数据库中的数据。
数据控制功能。
主要用来控制用户对数据库的操作权限,包括数据库安全控制和事物管理两部分。
3. 什么是基本表?什么是视图?两者的区别和联系是什么?解:基本表是本身独立存在的表,在SQL中一个关系就对应一个表。
试图是从一个或几个基本表导出的表。
试图本身不独立存储在数据库中,是一个虚表。
即数据库中只存放视图的定义而不存放视图对应的数据。
视图与表的区别:概念不同。
表是使用DBMS的DDL定义的,拥有真实数据的,以独立文件的形式存储在计算机外存上的具有固定模式结构和相应元组数据的实表。
数据库系统原理教程课后习题及答案(第四章)
第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 ) ,按系统可靠或可信程度逐渐增高。
这些安全级别之间具有一种偏序向下兼容的关系,即较高安全性级别提供的安全保护包含较低级别的所有保护要求,同时提供更多或更完善的保护能力。
数据库第六版第四章答案
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.。
mysql数据库武洪萍版第四章习题与答案
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个关系。
数据仓库与数据挖掘教程(第2版)课后习题答案 第四章
第四章作业1.数据仓库的需求分析的任务是什么?P67需求分析的任务是通过详细调查现实世界要处理的对象(企业、部门用户等),充分了解源系统工作概况,明确用户的各种需求,为设计数据仓库服务。
概括地说,需求分析要明确用那些数据经过分析来实现用户的决策支持需求。
2.数据仓库系统需要确定的问题有哪些?P67、、(1)确定主题域a)明确对于决策分析最有价值的主题领域有哪些b)每个主题域的商业维度是那些?每个维度的粒度层次有哪些?c)制定决策的商业分区是什么?d)不同地区需要哪些信息来制定决策?e)对那个区域提供特定的商品和服务?(2)支持决策的数据来源a)那些源数据与商品的主题有关?b)在已有的报表和在线查询(OLTP)中得到什么样的信息?c)提供决策支持的细节程度是怎么样的?(3)数据仓库的成功标准和关键性指标a)衡量数据仓库成功的标准是什么?b)有哪些关键的性能指标?如何监控?c)对数据仓库的期望是什么?d)对数据仓库的预期用途有哪些?e)对计划中的数据仓库的考虑要点是什么?(4)数据量与更新频率a)数据仓库的总数据量有多少?b)决策支持所需的数据更新频率是多少?时间间隔是多长?c)每种决策分析与不同时间的标准对比如何?d)数据仓库中的信息需求的时间界限是什么?3.实现决策支持所需要的数据包括哪些内容?P68(1)源数据(2)数据转换(3)数据存储(4)决策分析4.概念:将需求分析过程中得到的用户需求抽象为计算机表示的信息结构,叫做概念模型。
特点:(1)能真实反映现实世界,能满足用户对数据的分析,达到决策支持的要求,它是现实世界的一个真实模型。
(2)易于理解,便利和用户交换意见,在用户的参与下,能有效地完成对数据仓库的成功设计。
(3)易于更改,当用户需求发生变化时,容易对概念模型修改和扩充。
(4)易于向数据仓库的数据模型(星型模型)转换。
5.用长方形表示实体,在数据仓库中就表示主题,椭圆形表示主题的属性,并用无向边把主题与其属性连接起来;用菱形表示主题之间的联系,用无向边把菱形分别与有关的主题连接;若主题之间的联系也具有属性,则把属性和菱形也用无向边连接上。
数据库第4章习题参考答案
第4章习题解答1.选择题(1)在SELECT语句中,需显示的内容使用“*”,则表示()。
BA.选择任何属性B.选择所有属性C.选择所有元组D.选择主键(2)查询时要去掉重复的元组,则在SELECT语句中使用()。
DA.All B.UNION C.LIKE D.DISTINCT (3)在SELECT语句中使用GROUP BY NO时,NO必须()。
CA.在WHERE子句中出现B.在FROM子句出现C.在SELECT子句中出现D.在HAVING子句中出现(4)使用SELECT语句进行分组检索时,为了去掉不满足条件的分组,应当()。
BA.使用WHERE子句B.在GROUP BY后面使用HAVING子句C.先使用WHERE子句,再使用HA VING子句D.先使用HA VING子句,再使用WHERE子句(5)在SQL语句中,与表达式“仓库号Not In("wh1","wh2")”功能相同的表达式是()。
DA.仓库号="wh1" And 仓库号="wh2" B.仓库号<>"wh1" Or 仓库号<>"wh2"C.仓库号<>"wh1" Or 仓库号="wh2" D.仓库号<>"wh1" And 仓库号<>"wh2"第6~10题使用如下3个表:部门:部门号Char (8),部门名Char (12),负责人Char (6),电话Char (16)职工:部门号Char (8),职工号C har(10),姓名Char (8),性别Char (2),出生日期Datetime工资:职工号Char (10),基本工资Numeric (8,2),津贴Numeric (8,2),奖金Numeric (8,2),扣除Numeric (8,2)(6)查询职工实发工资的正确命令是()。
