03第三章关系大数据库答案详解详解
数据库第三章所有例题参考答案
11级信管,保密,图档上机考试题目与参考答案3.3Simple Select Statements1.EXAMPLE 3.3.1find the aid values and names of agents that are based in New York. select aid, aname from agents where city=’New York’;2.EXAMPLE3.3.3Retrieve all pid values of parts for which orders are placed.select distinct pid from orders;3.EXAMPLE 3.3.4retrieve all customer-agent name pairs, (cname, aname), where the customer places an order through the agent.select distinct ame,agents.anamefrom customers,orders,agentswhere customers.cid=orders.cid and orders.aid=agents.aid;4.EXAMPLE 3.3.6all pairs of customers based in the same city.select c1.cid, c2.cidfrom customers c1, customers c2where c1.city = c2.city and c1.cid < c2.cid;5.EXAMPLE 3.3.7find pid values of products that have been ordered by at least twocustomers.select distinct x1.pidfrom orders x1, orders x2where x1.pid = x2.pid and x1.cid < x2.cid;6.EXAMPLE 3.3.8Get cid values of customers who order a product for which an order is also placed by agent a06.select distinct y.cidfrom orders x, orders ywhere y.pid = x,pid and x.aid = ‘a06’;3.4Subqueries7.EXAMPLE 3.4.1Get cid values of customers who place orders with agents in Duluth or Dallas.select distinct cid from orderswhere aid in (select aid from agentswhere city= ‘Duluth’ or city = ‘Dallas’)8.EXAMPLE 3.4.2to retrieve all information concerning agents based in Duluth or Dallas (very close to the Subquery in the previous example).select * from agentswhere city in (‘Duluth’, ‘Dallas’ );or select *from agentswhere city = ‘Duluth’ or city = ‘Dallas’;9.EXAMPLE 3.4.3to determine the names and discounts of all customers who place orders through agents in Duluth or Dallas.select distinct cname, discnt from customerswhere cid in (select cid from orders where aid in(select aid from agents where city in (‘Duluth’, ‘Dallas’ ))); 10.EXAMPLE 3.4.4to find the names of customers who order product p05.select distinct cname from customers, orderswhere customers.cid = orders.cid and orders.pid = ‘p05’or select disti nct cname from customers where ‘p05’ in(select pid from orders where cid = customers.cid);11.EXAMPLE 3.4.5Get the names of customers who order product p07 from agent a03. select distinct cname from customerswhere cid in (select cid from orders where pid = ‘p07’ and aid = ‘a03’) 12.EXAMPLE 3.4.6to retrieve ordno values for all orders placed by customers in Duluth through agents in New York.select ordno from orders x where exists(select * from customers c, agents awhere c.cid = x.cid and a.aid = x.aid and c.city = ‘Duluth’ anda.city=‘New York’);13.EXAMPLE 3.4.7find aid values of agents with a minimum percent commission.select aid from agents where percent = (select min(percent) from agents);14.EXAMPLE 3.4.8find all customers who have the same discount as that of any of the customers in Dallas or Boston.select cid, cname from customerswhere discnt = some (select discnt from customerswhere city = ‘Dallas’ or city = ‘Boston’);15.EXAMPLE 3.4.9Get cid values of customers with discnt smaller than those of any customers who live in Duluth.select cid from customerswhere discnt <all (select discnt from customerswhere city = ‘Duluth’);16.EXAMPLE 3.4.10Retrieve all customer names where the customer places an order through agent a05.select distinct ame from customers cwhere exists (select * from orders xwhere c.cid = x.cid and x.aid = ‘a05’);or select distinct ame from customers c, orders xwhere c.cid = x.cid and x.ai d = ‘a05’ ;17.EXAMPLE 3.4.11Get cid values of customers who order both products p01 and p07. select distinct cid from orders xwhere pid = ‘p01’ and exsits (select * from orderswhere cid = x.cid and pid = ‘p07’);orselect distinct x.cid from orders x, orders ywhere x.pid = ‘p01’ and x.cid = y.cid and y.pid = ‘p07’;18.EXAMPLE 3.4.12Retrieve all customer names where the customer does not place an order through agent a05.select distinct ame from customers cwhere not exists (select * from orders xwhere c.cid = x.cid and x.aid = ‘a05’);19.EXAMPLE 3.4.13retrieving all customer names where the customer does not place an order through agent a05, but using the two equivalent NOT IN and <>ALLpredicates in place of NOT EXISTS.select distinct ame from customers cwhere c.cid not in (select cid from orders where aid = ‘a05’);or select ame from customers cwhere c.cid <>all (select cid from orders where aid = ‘a05’);20.EXAMPLE 3.4.14Find cid values of customers who do not place any order through agent a03.select distinct cid from orders xwhere not exists (select * from orderswhere cid = x.cid and aid = ‘a03’);orselect cid from customers cwhere not exists (select * from orderswhere cid = c.cid and aid = ‘a03’);21.EXAMPLE 3.4.15Retrieve the city names containing customers who order product p01. select distinct city from customers where cid in(select cid from orders where pid = ‘p01’);or select distinct city from customers where cid =some(select cid from orders where pid = ‘p01’);or select distinct city from customers c where exsits(select * from orders where cid = c.cid and pid = ‘p01’);or select distinct city from customers c, orders xwhere x.cid = c.cid and x.pid = ‘p01’;or select distinct city from customers c where ‘p01’ in(select pid from orders where cid = c.cid);3.5UNION Operators and FOR ALL Conditions 22.EXAMPLE 3.5.1to create a list of cities where either a customer or an agent, or both, is based.select city from customersunion select city from agents;23.EXAMPLE 3.5.2Get the cid values of customers who place orders with all agents based in New York.select c.cid from customers cwhere not exsits(select * from agents awhere a.city = ‘New York’ and not exsits(select * from orders xwhere x.cid = c.cid and x.aid = a.aid));24.EXAMPLE 3.5.3Get the aid values of agents in New York or Duluth who place orders forall products costing more than a dollar.select aid from agents awhere (a.city = ‘New York’ or a.city = ‘Duluth’)and not exsits(select p.pid from products pwhere p.price > 1.00 and not exsits(select * from orders xwhere x.pid = p.pid and x.aid = a.aid));25.EXAMPLE 3.5.4Find aid values of agents who place orders for product p01 as well as for all products costing more than a dollar.select a.aid from agents a where a.aid in(select aid from orders where pid = ‘p01’)and not exsits (select p.pid from products pwhere p.price > 1.00 and not exsits (select * from orders xwhere x.pid = p.pid and x.aid = a.aid));or select distinct y.aid from orders ywhere y.pid = ‘p01’ and not exsits(select p.pid from products pwhere p.price > 1.00 and not exsits(select * from orders xwhere x.pid = p.pid and x.aid = y.aid));26.EXAMPLE 3.5.6Find pid values of products supplied to all customers in Duluth.select pid from products pwhere not exsits(select c.cid from customers cwhere c.city = ‘Duluth’and not exists(select * from orders xwhere x.pid = p.pid and x.cid = c.cid));3.7 Set Functions in SQL27.EXAMPLE 3.7.1determine the total dollar amount of all orders.select sum(dollars) as totaldollars from orders28.EXAMPLE 3.7.2To determine the total quantity of product p03 that has been ordered. select sum(qty) as TOTAL from orders where pid=’p03’29.EXAMPLE 3.7.4Get the number of cities where customers are based.select count(distinct city) from customers30.EXAMPLE 3.7.5List the cid values of alt customers who have a discount less than the maximum discount.select cid from customerswhere discnt < (select max(discnt) from customers)31.EXAMPLE 3.7.6Find products ordered by at least two customers.select p.pid from products pwhere 2 <=(select count(distinct cid) from orders where pid=p.pid)图档的学生的上机考查的考题到此为止___________________________________________________________ ___________________________________________________________ 信管,保密的学生上机考查还包括下面的题目32.EXAMPLE 3.7.7Add a row with specified values for columns cid, cname, and city (c007, Windix, Dallas, null)to the customers table.insert into customers(cid, cname, city)values (‘c007’, ‘Windix’, ‘Dallas’)33.EXAMPLE 3.7.9After inserting the row (c007, Windix, Dallas, null) to the customers table in Example 3.7.7, assume that we wish to find the average discount of all customers.select avg(discnt) from customers3.8 Groups of Rows in SQL34.EXAMPLE 3.8.1to calculate the total product quantity ordered of each individual product by each individual agent.select pid, aid, sum(qty) as TOTAL from ordersgroup by pid, aid35.EXAMPLE 3.8.2Print out the agent name and agent identification number, and the product name and product identification number, together with the total quantity each agent supplies of that product to customers c002 and c003.select aname, a.aid, pname, p.pid, sum(qty)from orders x, products p, agents awhere x.pid = p.pid and x.aid = a.aid and x.cid in (‘c002’, ‘c003’)group by a.aid, a.aname, p.pid, p.pname36.EXAMPLE 3.8.3Print out all product and agent IDs and the total quantity ordered of the product by the agent, when this quantity exceeds 1000.select pid, aid, sum(qty) as TOTAL from ordersgroup by pid, aidhaving sum(qty) > 100037.EXAMPLE 3.8.4Provide pid values of all products purchased by at least two customers. select distinct pid from ordersgroup by pidhaving count(distinct cid) >= 23.9 A Complete Description of SQL Select38.EXAMPLE 3.9.1List all customers, agents, and the dollar sales for pairs of customers and agents, and order the result from largest to smallest sales totals. Retain only those pairs for which the dollar amount is at least equal to 900.00. select ame, c.cid, a.aname, a.aid, sum(dollars) as casalesfrom customers c, orders o, agents awhere c.cid = o.cid, and a.aid = o.aidgroup by ame, c.cid, a.aname, a.aidhaving sum(o.dollars) >= 900.00order by casales desc39.EXAMPLE 3.9.2listed the cid values of all customers with a discount less than the maximum discount.select cid from customerswhere discnt < (select max(discnt) from customers)40.EXAMPLE 3.9.3Retrieve the maximum discount of all customers.select max(discnt) from customers;select distinct discnt from customers cwhere discnt >= all (select discnt from customers dwhere d.cid<>c.cid)41.EXAMPLE 3.9.4Retrieve all data about customers whose cname begins with the letter “A”.select * from customers where cname like ‘A%’42.EXAMPLE 3.9.5Retrieve cid values of customers whose cname does not have a third letter equal to “%”.select cid from customers where cname not like ‘__[%]’43.EXAMPLE 3.9.6Retrieve cid values of customers whose cname begins “Tip_” and has an arbitrary number of characters following.select cid from customers where cname like ‘TIP\[_]%’44.EXAMPLE 3.9.7Retrieve cid values of customers whose cname starts with the sequence “ab\”.select cid from customers where cname like ‘ab\%’3.10 Insert, Update, and Delete Statements 45.EXAMPLE 3.10.1Add a row with specified values to the orders table, setting the qty and dollars columns null.insert into orders (ordno, month, cid, aid, pid)values (1107, ‘aug’, ‘c006’, ‘a04’, ‘p01’)46.EXAMPLE 3.10.2Create a new table called swcusts of Southwestern customers, and insert into it all customers from Dallas and Austin.create table swcusts (cid char(4) not null,cname varchar(13),city varchar(20),discnt real);insert into swcustsselect * from customerswhere city in (‘Dallas’, ‘Austin’)47.EXAMPLE 3.10.3Give all agents in New York a 10% raise in the percent commission they earn on an order.update agents set percent = 1.1 * percent where city = ‘New York’48.EXAMPLE 3.10.4Give all customers who have total orders of more than $1000 a 10% increase in the discnt.update agents set percent = 1.1 * discntwhere cid in(select cid from orders group by cid having sum(dollars) > 1000) 49.EXAMPLE 3.10.6Delete all agents in New York.delete from agents where city = ‘New York’50.EXAMPLE 3.10.7Delete all agents who have total orders of less than $600.Delete from agents where aid in(select aid from ordersGroup by aidHaving sum(dollars)<600)51.EXAMPLE 3.11.2Retrieve the names of customers who order products costing $0.50. delete from agents where aid in(select aid from orders group by aid having sum(dollars)<600)(完)。
数据库原理与应用第3章答案解析主编肖海蓉,任民宏
第3章数据库设计3.1数据库设计概述3.1.1数据库设计的基本任务和目标3.1.2数据库设计的特点与方法3.1.3数据库设计步骤3.2需求分析3.2.1需求分析的任务3.2.2用例建模3.2.3对象模型3.2.4需求分析案例3.3数据库概念结构设计3.3.1概念结构设计概述3.3.2概念结构设计的任务3.3.3概念结构设计案例3.3.4概念结构设计的其他问题3.4数据库逻辑结构设计3.4.1逻辑结构设计的任务3.4.2概念模型转换为关系模型的方法3.4.3关系模型优化3.4.4逻辑结构设计案例3.5数据库的物理结构设计3.5.1影响物理结构的主要因素3.5.2物理结构设计的任务3.5.3物理结构设计案例3.6数据库的实施3.7数据库的运行和维护本章小结习题3第3 章数据库设计课后习题参考答案1、选择题(1)~(5):D、A、B、D、B(6)~(10):C、A、B、C、D(11)~(15):D、C、D、D、C(16)~(20):B、C、D、C、C(21)~(25):A、B、D、B、D2、简答题(1)简述数据库设计的主要步骤和每一个阶段的具体任务?数据库设计划分的 6 个阶段。
①需求分析。
此阶段的任务准确了解与分析用户的需求,弄清系统要达到的目标和实现的功能。
面向对象方法是通过用例模型描述系统功能需求的。
为了满足用户功能需求,还需要获取关于问题域本质内容的对象、对象的特征以及对象之间存在哪些关系和操作,从而确定系统的对象模型。
②概念结构设计阶段。
概念结构设计的主要任务是根据系统分析建立的业务对象模型形成信息世界的实体、属性和实体标识符,确定实体之间的联系类型,即设计E-R 模型。
③数据库逻辑结构设计。
逻辑结构设计阶段的主要任务是将概念结构转换为某个DBMS 所支持的数据模型,对关系数据库来说,就是将E-R 模型转化为关系模型,最终生成表,并确定表中的列,并根据数据存取的性能要求优化关系模型。
④数据库物理结构设计。
《数据库系统原理》04735课后习题答案(2018版)
答案仅供参考第一章数据库系统概述选择题B、B、A简答题1.请简述数据,数据库,数据库管理系统,数据库系统的概念。
P27数据是描述事物的记录符号,是指用物理符号记录下来的,可以鉴别的信息。
数据库即存储数据的仓库,严格意义上是指长期存储在计算机中的有组织的、可共享的数据集合。
数据库管理系统是专门用于建立和管理数据库的一套软件,介于应用程序和操作系统之间。
数据库系统是指在计算机中引入数据库技术之后的系统,包括数据库、数据库管理系统及相关实用工具、应用程序、数据库管理员和用户。
2.请简述早数据库管理技术中,与人工管理、文件系统相比,数据库系统的优点。
数据共享性高数据冗余小易于保证数据一致性数据独立性高可以实施统一管理与控制减少了应用程序开发与维护的工作量3.请简述数据库系统的三级模式和两层映像的含义。
P31答:数据库的三级模式是指数据库系统是由模式、外模式和内模式三级工程的,对应了数据的三级抽象。
两层映像是指三级模式之间的映像关系,即外模式/模式映像和模式/内模式映像。
4.请简述关系模型与网状模型、层次模型的区别。
P35使用二维表结构表示实体及实体间的联系建立在严格的数学概念的基础上概念单一,统一用关系表示实体和实体之间的联系,数据结构简单清晰,用户易懂易用存取路径对用户透明,具有更高的数据独立性、更好的安全保密性。
第二章关系数据库选择题C、C、D简答题1.请简述关系数据库的基本特征。
P48答:关系数据库的基本特征是使用关系数据模型组织数据。
2.请简述什么是参照完整性约束。
P55答:参照完整性约束是指:若属性或属性组F是基本关系R的外码,与基本关系S的主码K 相对应,则对于R中每个元组在F上的取值只允许有两种可能,要么是空值,要么与S中某个元组的主码值对应。
3.请简述关系规范化过程。
答:对于存在数据冗余、插入异常、删除异常问题的关系模式,应采取将一个关系模式分解为多个关系模式的方法进行处理。
一个低一级范式的关系模式,通过模式分解可以转换为若干个高一级范式的关系模式,这就是所谓的规范化过程。
完整版陶宏才数据库原理及设计第3版课后习题答案
第一章一、解做题1、解释术语:数据、数据库、数据治理系统、数据库系统、数据库应用系统、视图、数据字典.P19-20数据:是描述现实世界中各种具体事物或抽象概念的、可存储并具有明确意义的信息.数据库:是相互关联的数据集合.数据治理系统:是一个通用的软件系统,由一组计算机程序构成.数据库系统:是一个用户的应用系统得以顺利运行的环境.数据库应用系统:主要指实现业务逻辑的应用程序.视图:指不同的用户对同一数据库的每一种理解称为视图.数据字典:用于存储数据库的一些说明信息的特殊文件.2、简述数据抽象、数据模型及数据模式之间的关系P26数据模型是数据抽象的工具,是数据组织和表示的方式;数据模式是数据抽象利用数据模型,将数据组织起来后得到的结果;总而言之,数据模式是数据抽象的结果.3、 DBMS应具备的根本功能有哪些P9数据独立性、平安性、完整性、故障恢复、并发限制4、数据库中对数据最根本的4种操作是什么P24增加、删除、修改、查询5、评价数据模型的 3个要素是什么P121〕能够真实地描述现实系统2〕能够容易为业务用户所理解3〕能够容易被计算机实现6、数据模型的3个要素是什么P24数据结构、数据操作、数据约束7、简述SQL语言的使用方式.P13一般有两种方式:SQL的交互式使用;用户通过开发应用系统与RDBMS交互.8、在数据库设计时,为什么涉及到多种数据模型P12由于目前商用化 DBMS没有一个能够同时满足3项要求,为此,人们不得不走折中路线,设计一些中间的数据模型.9、数据库系统中的用户类型有哪些P28-29最终用户、数据库应用开发人员、数据库治理员、其他与数据库系统有关的人员. 11、简述 OLTP与OLAP间的区别.P42-43OLTP联机事务处理〕主要面向日常的业务数据治理,完成用户的事务处理,提升业务处理效率,通常要进行大量的更新操作,同时对响应时间要求比较高.OLAP眠机分析处理〕注重数据分析,主要对用户当前及历史数据进行分析,辅助领导决策,通常要进行大量的查询操作,对时间的要求不太严格.二、单项选择题1、〔 A 〕不是SQL语言的标准.P156A.SQL-84B.SQL-86C.SQL-89D.SQL-922、〔 D 〕数据模型没有被商用 DBM S实现.P26A.关系模型B.层次模型C.网状模型D.E-R模型3、〔 C卬是数据模型应满足的要求.P12A.真实描述现实世界B.用户易理解C.有相当理论根底D.计算机易实1.一个数据库系统设计中,概念模式只有一个,而外模式那么可有多个.〔/〕 P112.每一种DBMS 的实现,均是建立在某一种数据模型根底之上.〔/〕 P7 二,某学院有根本实体集:系、教师、学生和课程.它们各有属性:系:系编号、系名、位置课程:课程号、课程名称、开课学期学生:学生学号、学生姓名、性别、地址教师:员工号、教师姓名、办公室有如下语义设定每个系有一位系主任,有多位教师; 一个教师仅在一个系任职; 每个系开设多门不同课程;每门课程各由多位教师授课;一位教师可教多门课程;一个学生可以在不同的系选修多门课程.P90 一、简做题4、〔 B 〕最早使用 A.DB2C.Oracl e SQL 语言.P156B. SystemR D.Ingres1.名词解释(1 )实体,实体型,属性,键,联系,联系型,二元联系和三元联系;(P55-58)实体:是现实世界或客观世界中有别于其他对象的对象实体型:是同类实体的集合属性:是实体型的特征或者性质键:具有唯一标识的一个或一组属性联系:是两个或多个的实体间的关联联系型:相似的一组联系二元联系:两个实体间的联系三元联系:三个实体间的联系(2 ) 1 : 1联系型,1: n联系型和m:n联系型;P59设联系型R关联实体A和B,如果A中的一个实体只与 B中的一个实体关联,反过来, B中的一个实体也只与 A中的一个实体关联,称 R为一对一联系型.(3 )键约束和参与约束.P61-62键约束:一个联系 R的实例中,一个关联的实体 A最多只出现在一个联系实例中;参与约束:是实体和联系之间的约束,即实体型中的实体如何参与到联系中.(4 )子类,超类,演绎,归纳和聚集; P67-69将实体分成子类,最上层为超类,下层即为子类;先定义子类,再定义超类,特殊到一般的方法为归纳先定义超类,再定义子类,一般到特殊的方法为演绎将联系和该联系所关联的实体一起作为一个高层实体来对待,该高层实体就是聚集2.简述属性按结构的分类,以及按取值的分类.P55按结构分:简单属性、复合属性、子属性按取值分:单值属性、多值属性、导出属性、空值属性3.一般情况下,联系用什么来唯一标识P57由所参与实体的键共同唯一确定4.在开发较大型的数据库应用系统中,为什么会涉及到多种数据模型P53由于目前商用化DBMS没有一个能够同时满足3项要求,为此,人们不得不走折中路线, 设计一些中间的数据模型.P1534、 SQL-92标准支持的完整性约束是否一定会在SQL Server中实现举例说明.P99不一定,例如:断言是SQL-92标准支持的,但SQL Server就不支持断言;触发器是SQL-92 标准不支持的,但 SQL Server支持断言;5、SQL Server中规那么的目的.P103当该列值变化时,RDBMS将检查变化的值是否在该规那么规定的范围内,如是的,那么接受新列值,否那么,拒绝该列值,并返回该列值违反的规那么名称及相关信息.6、SQL Server中在定义某些约束时分列级与表级,其分类的原那么是什么针对的是表中的一列还是多列7、简述外键约束定义的条件.P106-107定义外键约束的列,必须是另一个表中的主键或者候选键8、一张表上可定义的触发器个数是多少P109主表只需2个触发器:’删除’和’修改’触发器从表也只需2个触发器:’插入’和’修改’触发器9、简述关系代数的根本操作符.P132SELECTION^〕, PROJECTION® 影〕,UNION〔并或者联合〕,INTERSECTION〕,DIFFERENCE〕, CROSS PRODUCT^〕10、关系代数中对结果有重复元组时,如何处理 P134去掉重复元组,SQL中用 DISTINCT11、简述联结的分类.P137条件联结〔condition join〕、等联结〔equijoin〕自然联结〔natural join〕、外联结〔outer join〕12、简述关系运算的种类.P148TRC〔tuple relational calculus〕元组关系运算DRC〔domain relational calculus〕域关系运算四、设有如下3个关系:S〔Sid命号〕,Sname〔姓名〕,Age〔年龄〕,Sex〔性别〕〕SC〔Sid律号〕,Cid〔课程号〕,Score〔成绩〕〕C 〔Cid〔课程号〕,Cname〔课程名〕,Teacher〔教师〕〕试用关系代数式表达以下查询,并且写出前4个的SQL查询语句:1、检索LIU老师所授课程的课程号和课程名.select Cid, Cname from C where Teacher = ' LIU'2、检索年龄大于 23岁的男学生的学号和姓名.select Sid, Sname from S where Age>23 and Sex = ' male'3、检索学号为S3学生所学课程的课程名与任课教师名.select C. Cname, C. Teacher from C,SC where C.Cid = SC.Cid and S.Sid = S3'4、检索至少选修 LIU老师所授课程中一门课的女学生的姓名.select Sname from S where Sid in 〔select Sid from SC where Cid in 〔select Cid from C whereTeacher= ' LIU'〕〕S〔Sid命号〕,Sname〔姓名〕,Age〔年龄〕,Sex〔性别〕〕SC〔Sid律号〕,Cid〔课程号〕,Score〔成绩〕〕C〔Cid佩程号〕,Cname〔课程名〕,Teacher〔教师〕〕P2151、简述SQL语言的使用方式.P13一般有两种方式:SQL的交互式使用;用户通过开发应用系统与RDBMS交互.2、完整的SQL包括哪三个子语言分类P157数据定义子语言〔DDL〕、数据操纵子语言〔DML〕、数据限制子语言〔DCL〕3、简述SQL语言中定义的数据库与第一章中的数据库概念之异同.P159SQL语言定义的是指存储空间,用于存放相关数据集合,这个是理论概念具体化,与第 1章的应用概念上的有差异4、简述标准的SQL语言与实际数据库产品中的SQL数据库语言的关系.P157-158绝大多数RDBMS产品不是完全支持 SQL-92标准的;RDBMS产品的SQL语言,也有可能是 SQL-92中所没有的功能或特性.5、 SQL语言对数据库对象的定义使用哪3个SQL命令关键字 P158CREATED〕, DROP删除〕,ALTER^改〕6、简述定义索引的目的.P166-167利用索引,系统可以较快地在磁盘上定位所需数据,从而加快了数据查询速度.8、简述T-SQL中游标的作用.P191既为SQL Server的存储过程、触发器和函数提供了按行处理查询结果集合的途径,也为高级编程语言提供了按行处理查询结果集合的途径.9、简述T-SQL中存储过程的好处.P1941〕执行速度快2〕可用于实现经常使用的数据操作3〕实现较复杂的完整性约束4〕可在程序中被反复调用,有助于程序的模块化5〕有助于提供平安性6〕实现复杂、敏感事务的自动化7〕减少网络流量第三章关系数据库系统RDBS.简做题1.表问数据完整性的实现方式⑴外键约束,即在从表上定义外键约束.⑵利用触发器,即主表的触发器维护主表到从表方向的数据完整性,从表的触发器维护从表到主表方向的参照完整性.2.唯一限制的要求唯一约束针对候选键而言,并且值唯一,允许有且只有一个空值.3.SQL-92B准支持的完整性限制是否一定会在SQL SERVER实现,举例说明?不一定.比方 SOL SERVE不支持“断言〞,虽然SOL-92标准是支持的.4.SQL SERVER规那么的目的指明表中某一列的取值范围.5.SQL SERVER在定义某些限制时,分列级与表级,其分类的原那么是什么列级方式是:在要定义约束的列本身定义完后,紧接其后定义其约束.表级方式:表中所有的列都定义往后,再定义所要的约束.6.外键限制定义的条件定义外键的列必须是另一个表中的主键或候选键.7.主键和候选键分别通过什么限制来维护各自的完整性⑴主表到从表方向,表示“主表〞中的主键值在修改或删除是,“从表〞中与该主键值相同的外键值可“级联〞修改或删除;或者“禁止〞“主表〞主键值的修改和删除.⑵从表到主表方向,表示“从表〞中的外键值在插入和修改时,其值应“参照〞“主表〞中的主键值.8.关系代数的根本操作符selection〔选择〕:o-projection〔投影〕:兀union 〔并〕intersection (交)difference (差)cross-product (积)9. 关系代数中对结果有重复元组时,如何处理?只保存其中一个,另外的被去掉.10. 连接的分类条件连接连接自然连接外连接:左外连接,右外连接,全外连接二.单项选择题1. (①)不是关系代数的根本操作Intersection2. (③)用唯一限制来约束'①主键②外键 3. (②)与“列〞不同义.①字段②元组 三.判断题(正确打错误打X)1. ( V )关系代数中的改名操作既可用于改名也可用于存放临时关系模式结果.① Selection2D Projection ③Join④ ③候选键④简单键③域④届性2.( X )对主表,插入操作可能会违背参照完整性限制, 但删除和更新不会3.( X )等连接是自然连接的特例.4.( V )关系代数是与关系模型有关的查询语言.第四章SQL查询语言一.简做题1.SQL语言的使用方式既可以独立的交互式使用,也可以通过与宿主语言结合起来,嵌入式使用.2.完整的SQL包括哪三个子语言分类数据定义子语言DDL数据操纵子语言DML数据限制子语言DCL3.SQL语言中定义的数据库与第一章中的数据库概念之异同SQL语言中定义的数据库:在中/大型数据库系统中,数据库是一个存储空间,用丁存放数据库中的数据库对象,包括表、视图、索引、存储过程、触发器、与数据库平安性有关的限制机制以及其它对象等. 第一章中的数据库(Database, DB)是相互关联的数据集合.相同点:都包含有对数据进行组织、治理等操作的意思;不同点:SQL语言中定义的“数据库〞,是指存在丁物理磁盘上的一个存储空间,用丁存放相关数据集合;而第一章中的“数据库〞,是一个宽泛的概念, 可以说它是一种组织、治理数据的手段、方法,也可以说它是一个实际的产品.4.标准的SQL语言与实际数据库产品中的SQL数据库语言的关系绝大多数RDBM笋品并不完全支持标准SQL也就是SQL中的某些功能在实际数据库产品中可能没有得到支持. 反过来,实际RDBM样品的SQL语言也有可能出现标准SQL中没有出现的功能或特性,也就是商用RDBMSK现了超越了SQL标准的功能和特性,而且这种差异也会在命令和语法上得到表达.5.SQL语言对数据库对象的定义使用哪三个SQL命令关键字Create (创立)、Alter (修改)、Drop (删除)6.定义索引的目的定义的索引由谁使用索引是数据位置信息的关键字表,利用索引,系统可以较快地在磁盘上定位所需数据,而不需要从磁盘上从头到尾或从后向前, 一个数据一个数据地匹配和查找,从而加快数据查询的速度.用户不能在取数据时选择索引,索引的选择是由系统自动进行的.也就是索引建立后,有DBMS艮据需要自动选择使用.7.在什么情况下,SELEC查询中的ORDER B奇句、COMPUTEFW COMPUTER BY子句可以不要ORDER B仔句为排序而设置、COMPUTER句为整个结果汇总而设置、COMPUTER 咐为分组统计而设置.SELECT...INTO不能与COMPUTE^使用;当用UNION寸,各个SELECTS 句不能有ORDER BY句、COMPUTER^.8.在SELECT^询中,哪三个子句可以实施对数据的过滤或筛选WHERE于对FROMF句结果设置过滤条件;GROUP BY于对WHERE句的结果分组;HAVING!于对分组数据集合的再筛选.9.触发器在维护表问数据完整性时与外键约束的差异10.游标的作用为SQL Serve的储存过程,触发器,和函数,也为高级编程语言提供了按行处理查询结果集合的途径.11.使用存储过程的好处1:可以查看某个〔或所有〕数据库的相关信息,如不带后面的参数“数据库名〞,那么表示查看所有数据库的信息,否那么表示查看指定的数据库信息.2:可以用来修改数据库名.二.单项选择题1.某WHERE子句中有'X%[〞-f]_[4-8]'申表达,以下〔C 〕可以正确匹配.A. XTa_9B. XTb_8C. XTTTr7D. XSSSe32.视图中的数据来自所基于的〔C 〕.A.列B.行C.表D.自身。
《数据库技术与应用》第3章习题答案
第3章关系数据库1. 试述关系模型的三个组成部分。
解:关系模型的三个组成部分(1) 关系数据模型的数据结构(2) 关系数据模型的操纵与完整性约束(3) 关系数据模型的存储结构2. 解释下列术语的含义:①笛卡尔积;②主码;③候选码;④外码;⑤关系;⑥关系模式;⑦关系数据库解:①笛卡尔积:两个分别为n目和m目的关系R和S的笛卡尔积是一个(n+m)列的元组的集合。
元组的前n列是关系R的一个元组,后m列是关系S的一个元组。
若R有k1个元组,S有K2个元组,则关系R和关系S的笛卡尔积有k1×k2个元组。
记作:R×S={trts|tr∈R⋀ts∈S}②主码:若关系中的某一属性组的值能唯一的标识一个元组,则称该属性组为候选码。
若一个关系有多个候选码,则选定其中一个为主码。
③候选码:若关系中的某一属性组的值能唯一的标识一个元组,则称该属性组为候选码。
④外码:如果关系模式R中的某属性集是另一个关系模式S的主码,则该属性集为关系模式R的外码。
⑤关系:关系是集合论的一个概念,也是关系模型的数据结构,它只包含单一的数据结构——关系。
在关系模型中,现实世界的实体以及实体间的各种联系均用关系来表示。
在用户看来,一个关系就是一张二维表,这种简单的数据结构能够表达丰富的语义。
⑥关系模式:关系的描述称为关系模式。
它可以形式化地表示为R(U,D,DOM,F)其中R为关系名,U为组成该关系的属性名集合,D为属性组U中属性所来自的域,DOM为属性向域的映像集合,F为属性间数据的依赖关系集合。
⑦关系数据库:在关系模型中,实体以及实体之间的联系都是通过关系来表示的。
因此,在一个给定的应用领域中,所有实体以及实体之间的联系所对应的关系的集合就构成一个关系数据库。
3.关系数据库的三个完整性约束是什么?各是什么含义?解:关系模式中有3类完整性约束:实体完整性、参照完整性和用户自定义完整性。
实体完整性:若属性(指一个或一组属性)A是基本关系R的主属性,则A不能取空值。
数据库第1-3章 课后习题答案
第1章数据库系统概论三、简答题1. 答:数据库DB是长期存储在计算机内、有组织的、统一管理的相关数据的集合。
DB能为各种用户共享,具有较小冗余度、数据间联系紧密而又有较高的数据独立性等特点。
2. 答:数据库管理系统DBMS是位于用户与操作系统(OS)之间的一层数据管理软件,它为用户或应用程序提供访问DB的方法,包括DB的建立、查询、更新及各种数据控制。
DBMS 总是基于某种数据模型,可以分为层次型、网状型、关系型和面向对象型等。
3. 答:①数据定义语言及其翻译处理程序;②数据操纵语言及其编译(或解释)程序;③数据库运行控制程序;④实用程序。
4. 答:文件系统中的文件是面向应用的,一个文件基本上对应于一个应用程序,文件之间不存在联系,数据冗余大,数据共享性差,数据独立性差;数据库系统中的文件不再面向特定的某个或多个应用,而是面向整个应用系统,文件之间是相互联系的,减少了数据冗余,实现了数据共享,数据独立性高。
5. 答:①实现数据的集中化控制;②数据的冗余度小,易扩充;③采用一定的数据模型实现数据结构化;④避免了数据的不一致性;⑤实现数据共享;⑥提供数据库保护;⑦数据独立性;⑧数据由DBMS统一管理和控制。
6. 答:数据独立性是指数据库中的数据独立于应用程序,即数据的逻辑结构、存储结构与存取方式的改变不影响应用程序。
数据独立性一般分为数据的逻辑独立性和数据的物理独立性。
数据逻辑独立性是指数据库总体逻辑结构的改变(如修改数据定义、增加新的数据类型、改变数据间的联系等)不需要修改应用程序。
数据物理独立性是指数据的物理结构(存储结构、存取方式等)的改变,如存储设备的更换、物理存储格式和存取方式的改变等不影响数据库的逻辑结构,因而不会引起应用程序的改变。
7. 答:数据库系统中数据不是面向单个应用组织的,而是直接面向数据本身及数据间的内在联系来组织的,因此可以方便地供多用户多应用共享,这样,数据的冗余度就大幅度降低了。
数据库第三章习题及答案
第3章关系数据库标准语言SQL一、选择题1、SQL语言是的语言,易学习。
A.过程化 B.非过程化 C.格式化 D.导航式答案:B2、SQL语言是语言。
A.层次数据库 B.网络数据库 C.关系数据库 D.非数据库答案:C3、SQL语言具有的功能。
A.关系规范化、数据操纵、数据控制 B.数据定义、数据操纵、数据控制C.数据定义、关系规范化、数据控制 D.数据定义、关系规范化、数据操纵答案:B4、SQL语言具有两种使用方式,分别称为交互式SQL和。
A.提示式SQL B.多用户SQL C.嵌入式SQL D.解释式SQL 答案:C5、假定学生关系是S(S#,SNAME,SEX,AGE),课程关系是C(C#,CNAME,TEACHER),学生选课关系是SC(S#,C#,GRADE)。
要查找选修“COMPUTER”课程的“女”学生姓名,将涉及到关系。
A.S B.SC,C C.S,SC D.S,C,SC 答案:D6、若用如下的SQL语句创建一个student表:CREATE TABLE student(NO C(4) NOT NULL,NAME C(8) NOT NULL,SEX C(2),AGE N(2))可以插入到student表中的是。
A.(‘1031’,‘曾华’,男,23) B.(‘1031’,‘曾华’,NULL,NULL)C.(NULL,‘曾华’,‘男’,‘23’) D.(‘1031’,NULL,‘男’,23) 答案:B7、当两个子查询的结果时,可以执行并,交,差操作.A.结构完全不一致 B.结构完全一致C.结构部分一致D.主键一致答案:B第8到第10题基于这样的三个表即学生表S、课程表C和学生选课表SC,它们的结构如下:S(S#,SN,SEX,AGE,DEPT)C(C#,CN)SC(S#,C#,GRADE)其中:S#为学号,SN为姓名,SEX为性别,AGE为年龄,DEPT为系别,C#为课程号,CN为课程名,GRADE为成绩。
数据库第三章习题参考答案范文大全
数据库第三章习题参考答案范文大全第一篇:数据库第三章习题参考答案3-2 对于教务管理数据库的三个基本表S(SNO,SNAME, SEX, AGE,SDEPT) SC(SNO,CNO,GRADE)C(CNO,CNAME,CDEPT,TNAME) 试用SQL的查询语句表达下列查询:⑴ 检索LIU老师所授课程的课程号和课程名。
⑵ 检索年龄大于23岁的男学生的学号和姓名。
⑶ 检索学号为200915146的学生所学课程的课程名和任课教师名。
⑷ 检索至少选修LIU老师所授课程中一门课程的女学生姓名。
⑸ 检索WANG同学不学的课程的课程号。
⑹ 检索至少选修两门课程的学生学号。
⑺ 检索全部学生都选修的课程的课程号与课程名。
⑻ 检索选修课程包含LIU老师所授课程的学生学号。
解:⑴ SELECT C#,CNAME FROM C WHERE TEACHER=’LIU’; ⑵ SELECT S#,SNAME FROM S WHERE AGE>23 AND SEX=’M’; ⑶ SELECT CNAME,TEACHER FROM SC,C WHERE SC.C#=C.C# AND S#=’200915146’ ⑷ SELECT SNAME (连接查询方式) FROM S,SC,C WHERE S.S#=SC.S# AND SC.C#=C.C# AND TEACHER=’LIU’;或:SELECT SNAME (嵌套查询方式) FROM S WHERE SEX=’F’AND S# IN (SELECT S# FROM SC WHERE C# IN (SELECT C# FROM C WHERE TEACHER=’LIU’)) 或:SELECT SNAME (存在量词方式)SEX=’F’ AND FROM S WHERE SEX=’F’ AND EXISTS(SELECT* FROM SC WHERE SC.S#=S.S# AND EXISTS(SELECT * FROM C WHERE C.C#=SC.C# AND TEACHER=’LIU’)) ⑸ SELECT C# FROM C WHERE NOT EXISTS(SELECT * FROM S,SC WHERE S.S#=SC.S# AND SC.C#=C.C# AND SNAME=’WANG)); ⑹ SELECT DISTINCT X.S# FROM SC AS X,SC AS Y WHERE X.S#=Y.S# AND X.C#!=Y.C#; ⑺ SELECT C#.CNAME FROM C WHERE NOT EXISTS (SELECT * FROM S WHERE NOT EXISTS (SELECT * FROM SC WHERE S#=S.S# AND C#=C.C#)); ⑻ SELECT DISTINCT S# FROM SC AS X WHERE NOT EXISTIS (SELECT * FROM C WHERE TEACHER=’LIU’ AND NOT EXISTS (SELECT * FROM SC AS Y WHERE Y.S#=X.S# AND Y.C#=C.C#)); 3-3 试用SQL查询语句表达下列对3.2题中教务管理数据库的三个基本表S、SC、C查询:⑴ 统计有学生选修的课程门数。
数据库第三章部分习题答案
3.2 对于教学数据库的三个基本表S(S#,SNAME,AGE,SEX)SC(S#,C#,GRADE)C(C#,CNAME,TEACHER)试用SQL的查询语句表达下列查询:3.2.1检索年龄小于17岁的女学生的学号和姓名select s#,sname from Swhere age<17 and sex=F;3.2.2检索男生所学课程的课程号和课程名select c#,cname from Cwhere c# in (select distinct c#from SCwhere s# in (select s# from S where sex=M)) 3.2.3检索男生所学课程的任课老师的工号和姓名实用文档select t#,tname from Twhere t# in(select distinct t#from C实用文档where c# in(select distinct c#from SCwhere s# in(select s#from Swhere sex=1)));3.2.4检索至少选修两门课程的学生的学号select s#from SCgroup by s#having count(c#)>=2;3.2.5检索至少有学号为S2和S4所学的课程和课程名select c#,cnamefrom C实用文档where c# in((select c#from sc where s#='S2')intersect实用文档(select c# from sc where s#='S4') );3.2.6检索‘WANG’同学不学的课程号select c# from cexcept(select distinct c#from scwhere s# =(select s# from s where sname='WANG'));3.2.7检索全部学生都选修的课程号和课程名select c#,cnamefrom cwhere not exists(select s#from swhere c.c# not in (select c# from sc where sc.s#=s.s# ));实用文档3.2.8检索选修课程包含'LIU'老师所授课程的全部课程的学生的学号和姓名select s#,snamefrom s实用文档where not exists((select c#from cwhere t#=(select t#from twhere tname='LIU')) except(select c# from sc wheresc.s#=s.s#) );3.4 设有两个基本表R(A,B,C)和S(A,B,C),试用SQL查询语句表达下列关系代数表达式:① R∪S ② R∩S ③ R-S ④R×S ⑤πA,BπB,C(S)⑥π1,6(σ3=4(R×S)⑦π1,2,3(R S)⑧R÷πC(S)解:①(SELECT * FROM R)UNION(SELECT * FROM S);②(SELECT * FROM R)3=3实用文档INTERSECT(SELECT * FROM S);③(SELECT * FROM R)MINUS(SELECT * FROM S);④SELECT *实用文档FROM R, S;⑤SELECT R.A, R.B, S.CFROM R, SWHERE R.B=S.B;⑥SELECT R.A, S.CFROM R, SWHERE R.C=S.A;⑦SELECT R.* (R.*表示R中全部属性)FROM R, SWHERE R.C=S.C;⑧R÷πC(S)的元组表达式如下:{ t |(∃u)(∀v)(∃w)(R(u)∧S(v)∧R(w)∧w[1]=u[1] ∧w[2]=u[2] ∧w[3]=v[3] ∧t[1]=u[1] ∧t[2]=u[2])}据此,可写出SELECT语句:SELECT A, BFROM R RXWHERE NOT EXISTS实用文档( SELECT *FROM SWHERE NOT EXISTS( SELECT *FROM R RY实用文档WHERE RY.A=RX.A AND RY.B=RX.B ANDRY.C=S.C));3.6 试叙述SQL语言的关系代数特点和元组演算特点。
数据库原理与应用(第3版)答案
《数据库原理与应用》(第三版)习题参考答案第 1 章数据库概述1.试说明数据、数据库、数据库管理系统和数据库系统的概念。
答:数据是描述事物的符号记录。
数据库是长期存储在计算机中的有组织的、可共享的大量数据的集合。
数据库管理系统是一个专门用于实现对数据进行管理和维护的系统软件。
数据库系统是指在计算机中引入数据库后的系统,一般由数据库、数据库管理系统(及相关的实用工具)、应用程序、数据库管理员组成。
2.数据管理技术的发展主要经历了哪几个阶段?答:文件管理和数据库管理。
3.与文件管理相比,数据库管理有哪些优点?答:与文件系统管理数据相比,数据库系统管理数据带来了如下好处:将相互关联的数据集成在一起,较少的数据冗余,程序与数据相互独立,保证数据的安全可靠,最大限度地保证数据的正确性,数据可以共享并能保证数据的一致性。
4.在数据库管理方式中,应用程序是否需要关心数据的存储位置和存储结构?为什么?答:不需要。
因为数据库管理系统提供了逻辑独立性和物理独立性。
5.在数据库系统中,数据库的作用是什么?答:数据库是数据的汇集,它以一定的组织形式保存在存储介质上。
6.在数据库系统中,应用程序可以不通过数据库管理系统而直接访问数据文件吗?答:不能7.数据独立性指的是什么?它能带来哪些好处?答:数据独立性是指应用程序不会因数据的物理表示方式和访问技术的改变而改变,即应用程序不依赖于任何特定的物理表示方式和访问技术,它包含两个方面:逻辑独立性和物理独立性。
物理独立性是指当数据的存储位置或存储结构发生变化时,不影响应用程序的特性;逻辑独立性是指当表达现实世界的信息内容发生变化时,不影响应用程序的特性。
8.数据库系统由哪几部分组成,每一部分在数据库系统中的作用大致是什么?答:数据库系统一般包括数据库、数据库管理系统(及相应的实用工具)、应用程序和数据库管理员四个部分。
数据库是数据的汇集,它以一定的组织形式保存在存储介质上;数据库管理系统是管理数据库的系统软件,它可以实现数据库系统的各种功能;应用程序专指以数据库数据为基础的程序,数据库管理员负责整个数据库系统的正常运行。
第三章数据库习题答案
第三章数据库习题答案第三章数据库习题答案数据库是现代信息系统的核心组成部分,它以其高效、可靠和灵活的特性,成为许多企业和组织存储和管理数据的首选。
在学习数据库的过程中,习题是检验知识掌握程度的重要方式。
本文将为大家提供第三章数据库习题的详细答案,希望能够帮助大家更好地理解和应用数据库知识。
1. 什么是关系数据库?答:关系数据库是一种基于关系模型的数据库,它以表格的形式存储数据,表格由行和列组成,每一行表示一个记录,每一列表示一个属性。
关系数据库通过建立表之间的关系,实现数据的组织和管理。
2. 什么是关系模型?答:关系模型是一种用于描述和组织数据的模型,它基于数学集合论的概念,将数据组织成表格的形式。
关系模型通过定义表格之间的关系,实现数据的一致性和完整性。
3. 什么是主键?答:主键是关系数据库中用于唯一标识记录的属性或属性组合。
主键具有唯一性和非空性的特点,它能够确保每一条记录都能够被唯一标识。
4. 什么是外键?答:外键是关系数据库中用于建立表之间关系的属性或属性组合。
外键与其他表的主键相关联,通过外键可以实现表之间的关联和数据的一致性。
5. 什么是实体完整性?答:实体完整性是指数据库中的每一条记录都能够被唯一标识。
实体完整性通过主键来实现,它能够确保每一条记录都具有唯一性和非空性。
6. 什么是参照完整性?答:参照完整性是指数据库中的外键与其他表的主键相关联,确保数据之间的一致性和完整性。
参照完整性能够防止无效的外键值和数据的不一致。
7. 什么是范式?答:范式是一种用于设计关系数据库的规范化方法。
它通过分解表和消除冗余数据,提高数据库的一致性和性能。
常用的范式包括第一范式、第二范式和第三范式。
8. 什么是第一范式?答:第一范式是指关系数据库中的每一列都具有原子性,即每一列都不可再分。
第一范式能够消除表中的重复数据,提高数据库的一致性。
9. 什么是第二范式?答:第二范式是在第一范式的基础上,要求关系数据库中的每一条记录都能够被唯一标识。
(完整版)第三章数据库习题答案
4.针对上题中建立的4 个表试用sQL 语言完成第二章习题5 中的查询。
( l )求供应工程Jl 零件的供应商号码SNO ;SELECT DIST SNO FROM SPJ WHERE JNO=’J1’( 2 )求供应工程Jl 零件Pl 的供应商号码SNO ;SELECT DIST SNO FROM SPJ WHERE JNO='J1' AND PNO='P1'( 3 )求供应工程Jl 零件为红色的供应商号码SNO ;SELECT SNO FROM SPJ,P WHERE JNO='J1' AND SPJ.PNO=P.PNO AND COLOR='红' ( 4 )求没有使用天津供应商生产的红色零件的工程号JNO ;SELECT DIST JNO FROM SPJ WHERE JNO NOT IN (SELE JNO FROM SPJ,P,S WHERE S.CITY='天津' AND COLOR='红' AND S.SNO=SPJ.SNO AND P.PNO=SPJ.PNO)。
( 5 )求至少用了供应商Sl 所供应的全部零件的工程号JNO ;由于VFP不允许子查询嵌套太深,将查询分为两步A、查询S1供应商供应的零件号SELECT DIST PNO FROM SPJ WHERE SNO='S1'结果是(P1,P2)B、查询哪一个工程既使用P1零件又使用P2零件。
SELECT JNO FROM SPJ WHERE PNO='P1'AND JNO IN (SELECT JNO FROM SPJ WHERE PNO='P2')5.针对习题3中的四个表试用SQL语言完成以下各项操作:(1)找出所有供应商的姓名和所在城市。
SELECT SNAME,CITY FROM S(2)找出所有零件的名称、颜色、重量。
数据库概论第1-3章习题参考答案
第1章绪论习题参考答案1、试述数据、数据库、数据库管理系统、数据库系统的概念。
(参见P3、4、5页)参考答案:描述事物的符号记录称为数据;数据库是长期储存在计算机内的、有组织的、可共享的数据集合;数据库管理系统是位于用户与操作系统之间的一层数据管理软件; 数据库系统是指在计算机系统中引入数据库后的系统,一般由数据库、数据库管理系统(及其开发工具)、应用系统、数据库管理员和用户构成。
2.使用数据库系统有什么好处?(参见P12页)参考答案:数据库系统使信息系统从以加工数据的程序为中心转向围绕共享的数据库为中心的阶段,这样既便于数据的集中管理,又有利于应用程序的研制和维护,提高了数据的利用率和相容性,提高了决策的可靠性。
3.试述文件系统与数据库系统的区别和联系。
(8、9、10页)参考答案:1)数据结构化是数据库与文件系统的根本区别。
在文件系统中,相互独立的文件的记录内部是有结构的,管其记录内部已有了某些结构,但记录之间没有联系。
数据库系统实现整体数据的结构化,是数据库的主要特征之一。
2)在文件系统中,数据的最小存取单位是记录,粒度不能细到数据项。
而在数据库系统中,存取数据的方式也很灵活,可以存取数据库中的某一个数据项、一组数据项一个记录或或一组记录。
3)文件系统中的文件是为某一特定应用服务的,文件的逻辑结构对该应用程序来说是优化的,因此要想对现有的数据再增加一些新的应用会很困难,系统不容易扩充。
而在数据库系统中数据不再针对某一应用,而是面向全组织,具有整体的结构化。
5.试述数据库系统的特点。
(9、10、11页)参考答案:数据结构化;数据的共享性高、冗余度低、易扩充;数据独立性高;数据由DBMS统一管理和控制。
6.数据库管理系统的主要功能有哪些? (4页)参考答案:数据定义功能、数据操纵功能、数据库的运行管理、数据库的建立和维护功能。
7.试述数据模型的概念(13页)、数据模型的作用、数据模型的三个要素。
数据库原理及应用第3章课后习题答案
习题31.试述关系模型的3个组成部分。
1)数据结构关系模型的数据结构非常简单,只包括单一的数据结构——关系。
从用户角度,关系模型中数据的逻辑结构是一张扁平的二维表。
2)数据操作关系操作采用集合操作方式,即操作的对象和结果都是集合。
这种方式称为一次一集合的方式。
而非关系数据结构的数据操作方式为一次一记录方式。
关系模型中常用的关系操作包括查询操作和插入、删除、修改操作两大部分。
3)完整性约束关系模型提供了丰富的完整性控制机制,允许定义三类完整性:实体完整性、参照完整性和用户定义完整性。
2.定义并理解下列术语,说明它们之间的联系与区别:1)域、笛卡尔积、关系、元组、属性①域(Domain)域是一组具有相同数据类型的值的集合。
②笛卡尔积(Cartesian Product)定义 3.2 给定一组域D1,D2,…,D n,这些域中可以有相同的域。
D1,D2,…,D n 的笛卡尔积为:D1×D2×…×D n={(d1,d2,…,d n)|d i D i,i=1,2,…,n}③关系D1×D2×…×D n的子集叫作在域D1,D2,…,D n上的关系,表示为:R(D1,D2,…,D n),这里R是关系名。
④表的每行对应一个元组,也可称为记录(Record)。
⑤表的每列对应一个域,也可以称为字段(Filed )。
由于域可以相同,为了加以区分,必须为每列起一个名字,称为属性(Attribute)。
2)主码、候选码、外码①若关系中的某一属性或属性组的值能唯一地标识一个元组,则称该属性组为候选码或码(Key)。
其中属性组中不能含有多余的属性。
②若一个关系有多个候选码,则选定其中一个作为主码(Primary Key)。
每个关系有且仅有一个主码。
③如果一个属性或属性组不是所在关系的码,却是另一个关系的码,则称该属性或属性组为所在关系的外码。
3)关系模型、关系、关系数据库①关系数据库中关系模式是型,关系是值,关系模式是对关系的描述,关系模式可以用一个五元组表示:R(U,D,DOM,F)。
数据库答案第三章习题参考答案
6. 找出使用上海产的零件的工程项目名。 Select jname from j,spj,s where j.jno=spj.jno and spj.sno=s.sno and s.city=‘上海’; 或: Select jname from j where jno in (Select jno from spj, s where spj.sno=s.sno and s.city=‘上海’);
4.求没有使用天津供应商生产的红色零件的工程号JNO。 Select jno From j Where not exists (Select * From spj, s, p where spj.jno=j.jno and spj.sno=s.sno and spj.pno=p.pno and s.city=‘天津’ and p.color=‘红’ );
4.找出工程项目J2 所使用的各种零件的名称及其数量。 Select p.pname, spj.qty from p, spj where p.pno=spj.pno and spj.jno=‘J2’; 5.找出上海厂商供应的所有零件的代码。 Select distinct pno from spj where sno in (Select sno from s where city=‘上海’);
5.求至少用了供应商S1所供应的全部零件的工程号JNO。 即查找:不存在这样的零件y,供应商S1供应了y,而工程x为选用y。 Select distinct jno From spj z Where not exists (select * from spj x where sno=‘S1’ and not exists (select * from spj y where y.pno=x.pno and y.jno=z.jno));
数据库第3章习题解答PPT教学课件
CREATE TABLE S (SNO CHAR(4) NOT NULL ,
SNAME CHAR(20) NOT NULL,
STATUS CHAR(10),
CITY CHAR(20),
PRIMARY KEY (SNO));
CREATE TABLE SPJ (SNO CHAR(4) NOT NULL,
PNO CHAR(4) NOT NULL,
JNO CHAR(4) NOT NULL,
QTY SMALLINT,
PRIMARY KEY (SNO,PNO,JNO),
FOREIGN KEY (SNO) REFERENCES S(SNO),
2)求供应工程J1零件P1的供应商号码SNO; SELECT SNO FROM SPJ WHERE JNO=‘J1’ AND PNO=‘P1’;
3)求供应工程J1零件为红色的供应商号SNO; SELECT DISTINCT SNO FROM SPJ WHERE JNO=‘J1’ AND PNO IN (SELECT PNO FROM P WHERE COLOR=‘红’);
FOREIGN KEY (PNO) REFERENCES P(PNO),
2020/12/10
FOREIGN KEY (JNO) REFERENCES J(JNO)); 9
4.针对上题中建立的四个表试用SQL语言完成第二 章习题5中的查询
1)求供应工程J1零件的供应商号码SNO;
2)求供应工程J1零件P1的供应商号码SNO;
2020/12/10
3
(1) 检索“程军”老师所授课程的课程号CNO和课程名CNAME。
(完整word版)数据库系统基础教程第三章答案
Exercise 3.1.1Answers for this exercise may vary because of different interpretations.Some possible FDs:Social Security number → nameArea code → stateStreet address, city, state → zipcodePossible keys:{Social Security number, street address, city, state, area code, phone number}Need street address, city, state to uniquely determine location. A person couldhave multiple addresses. The same is true for phones. These days, a person couldhave a landline and a cellular phoneExercise 3.1.2Answers for this exercise may vary because of different interpretationsSome possible FDs:ID → x-position, y-position, z-positionID → x-velocity, y-velocity, z-velocityx-position, y-position, z-position → IDPossible keys:{ID}{x-position, y-position, z-position}The reason why the positions would be a key is no two molecules can occupy the same point.Exercise 3.1.3aThe superkeys are any subset that contains A1. Thus, there are 2(n-1) such subsets, since each of the n-1 attributes A2 through A n may independently be chosen in or out.Exercise 3.1.3bThe superkeys are any subset that contains A1 or A2. There are 2(n-1) such subsets when considering A1 and the n-1 attributes A2 through A n. There are 2(n-2) such subsets when considering A2 and the n-2 attributes A3 through A n. We do not count A1 in these subsets because they are already counted in the first group of subsets. The total number of subsets is 2(n-1) + 2(n-2).Exercise 3.1.3cThe superkeys are any subset that contains {A1,A2} or {A3,A4}. There are 2(n-2) such subsets when considering {A1,A2} and the n-2 attributes A3 through A n. There are 2(n-2) – 2(n-4) such subsets when considering {A3,A4} and attributes A5 through A n along with the individual attributes A1 and A2. We get the 2(n-4) term because we have to discard the subsets that contain the key {A1,A2} to avoid double counting. The total number of subsets is 2(n-2) +2(n-2) – 2(n-4).Exercise 3.1.3dThe superkeys are any subset that contains {A1,A2} or {A1,A3}. There are 2(n-2) such subsets when considering {A1,A2} and the n-2 attributes A3 through A n. There are 2(n-3) such subsets when considering {A1,A3} and the n-3 attributes A4 through A n We do not count A2 in these subsets because they are already counted in the first group of subsets. The total number of subsets is 2(n-2) + 2(n-3).Exercise 3.2.1aWe could try inference rules to deduce new dependencies until we are satisfied we have them all. A more systematic way is to consider the closures of all 15 nonempty sets of attributes.For the single attributes we have {A}+ = A, {B}+ = B, {C}+ = ACD, and {D}+ = AD. Thus, the only new dependency we get with a single attribute on the left is C→A.Now consider pairs of attributes:{AB}+ = ABCD, so we get new dependency AB→D. {AC}+ = ACD, and AC→D is nontrivial. {AD}+= AD, so nothing new. {BC}+ = ABCD, so we get BC→A, and BC→D. {BD}+ = ABCD, giving us BD→A and BD→C. {CD}+ = ACD, giving CD→A.For the triples of attributes, {ACD}+ = ACD, but the closures of the other sets are each ABCD. Thus, we get new dependencies ABC→D, ABD→C, and BCD→A.Since {ABCD}+ = ABCD, we get no new dependencies.The collection of 11 new dependencies mentioned above are:C→A, AB→D, AC→D, BC→A, BC→D, BD→A, BD→C, CD→A, ABC→D, ABD→C, and BCD→A.Exercise 3.2.1bFrom the analysis of closures above, we find that AB, BC, and BD are keys. All other sets either do not have ABCD as the closure or contain one of these three sets.Exercise 3.2.1cThe superkeys are all those that contain one of those three keys. That is, a superkeythat is not a key must contain B and more than one of A, C, and D. Thus, the (proper) superkeys are ABC, ABD, BCD, and ABCD.Exercise 3.2.2ai) For the single attributes we have {A}+ = ABCD, {B}+ = BCD, {C}+ = C, and {D}+ = D. Thus, the new dependencies are A→C and A→D.Now consider pairs of attributes:{AB}+ = ABCD, {AC}+ = ABCD, {AD}+ = ABCD, {BC}+ = BCD, {BD}+ = BCD, {CD}+ = CD. Thus the new dependencies are AB→C, AB→D, AC→B, AC→D, AD→B, AD→C, BC→D and BD→C.For the triples of attributes, {BCD}+ = BCD, but the closures of the other sets are each ABCD. Thus, we get new dependencies ABC→D, ABD→C, and ACD→B.Since {ABCD}+ = ABCD, we get no new dependencies.The collection of 13 new dependencies mentioned above are:A→C, A→D, AB→C, AB→D, AC→B, AC→D, AD→B, AD→C, BC→D, BD→C, ABC→D, ABD→C and ACD→B.ii) For the single attributes we have {A}+ = A, {B}+ = B, {C}+ = C, and {D}+ = D. Thus, there are no new dependencies.Now consider pairs of attributes:{AB}+ = ABCD, {AC}+ = AC, {AD}+ = ABCD, {BC}+ = ABCD, {BD}+ = BD, {CD}+ = ABCD. Thus the new dependencies are AB→D, AD→C, BC→A and CD→B.For the triples of attributes, all the closures of the sets are each ABCD. Thus, we get new dependencies ABC→D, ABD→C, ACD→B and BCD→A.Since {ABCD}+ = ABCD, we get no new dependencies.The collection of 8 new dependencies mentioned above are:AB→D, AD→C, BC→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.iii) For the single attributes we have {A}+ = ABCD, {B}+ = ABCD, {C}+ = ABCD, and {D}+ = ABCD. Thus, the new dependencies are A→C, A→D, B→D, B→A, C→A, C→B, D→B and D→C.Since all the single attributes’ closures are ABCD, any superset of the singleattributes will also lead to a closure of ABCD. Knowing this, we can enumerate the restof the new dependencies.The collection of 24 new dependencies mentioned above are:A→C, A→D, B→D, B→A, C→A, C→B, D→B, D→C, AB→C, AB→D, AC→B, AC→D, AD→B, AD→C, BC→A, BC→D, BD→A, BD→C, CD→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.Exercise 3.2.2bi) From the analysis of closures in 3.2.2a(i), we find that the only key is A. All other sets either do not have ABCD as the closure or contain A.ii) From the analysis of closures 3.2.2a(ii), we find that AB, AD, BC, and CD are keys.All other sets either do not have ABCD as the closure or contain one of these four sets.iii) From the analysis of closures 3.2.2a(iii), we find that A, B, C and D are keys. All other sets either do not have ABCD as the closure or contain one of these four sets.Exercise 3.2.2ci) The superkeys are all those sets that contain one of the keys in 3.2.2b(i). The superkeys are AB, AC, AD, ABC, ABD, ACD, BCD and ABCD.ii) The superkeys are all those sets that contain one of the keys in 3.2.2b(ii). The superkeys are ABC, ABD, ACD, BCD and ABCD.iii) The superkeys are all those sets that contain one of the keys in 3.2.2b(iii). The superkeys are AB, AC, AD, BC, BD, CD, ABC, ABD, ACD, BCD and ABCD.Exercise 3.2.3aSince A1A2…A n C contains A1A2…A n, then the closure of A1A2…A n C contains B. Thus it follows that A1A2…A n C→B.Exercise 3.2.3bFrom 3.2.3a, we know that A1A2…A n C→B. Using the concept of trivial dependencies, we can show that A1A2…A n C→C. Thus A1A2…A n C→BC.Exercise 3.2.3cFrom A1A2…A n E1E2…E j, we know that the closure contains B1B2…B m because of the FD A1A2…A n→B1B2…B m. The B1B2…B m and the E1E2…E j combine to form the C1C2…C k. Thus the closure ofA1A2…A n E1E2…E j contains D as well. Thus, A1A2…A n E1E2…E j→D.Exercise 3.2.3dFrom A1A2…A n C1C2…C k, we know that the closure contains B1B2…B m because of the FD A1A2…A n→B1B2…B m. The C1C2…C k also tell us that the closure of A1A2…A n C1C2…C k contains D1D2…D j. Thus, A1A2…A n C1C2…C k→B1B2…B k D1D2…D j.Exercise 3.2.4aIf attribute A represented Social Security Number and B represented a person’s name,then we would assume A→B but B→A would not be valid because there may be many peoplewith the same name and different Social Security Numbers.Exercise 3.2.4bLet attribute A represent Social Security Number, B represent gender and C represent name. Surely Social Security Number and gender can uniquely identify a person’s name (i.e.AB→C). A Social Security Number can also uniquely identify a person’s name (i.e. A→C). However, gender does not uniquely determine a name (i.e. B→C is not valid).Exercise 3.2.4cLet attribute A represent latitude and B represent longitude. Together, both attributes can uniquely determine C, a point on the world map (i.e. AB→C). However, neither A nor B can uniquely identify a point (i.e. A→C and B→C are not valid).Exercise 3.2.5Given a relation with attributes A1A2…A n, we are told that there are no functional dependencies of the form B1B2…B n-1→C where B1B2…B n-1 is n-1 of the attributes from A1A2…A n and C is the remaining attribute from A1A2…A n. In this case, the set B1B2…B n-1 and any subset do not functionally determine C. Thus the only functional dependencies that we can make are ones where C is on both the left and right hand sides. All of these functional dependencies would be trivial and thus the relation has no nontrivial FD’s.Exercise 3.2.6Let’s prove this by using the contrapositive. We wish to show that if X+ is not a subset of Y+, then it must be that X is not a subset of Y.If X+ is not a subset of Y+, there must be attributes A1A2…A n in X+ that are not in Y+. If any of these attributes were originally in X, then we are done because Y does not contain any of the A1A2…A n. However, if the A1A2…A n were added by the closure, then we must examine the case further. Assume that there was some FD C1C2…C m→A1A2…A j where A1A2…A j is some subset of A1A2…A n. It must be then that C1C2…C m or some subset of C1C2…C m is in X. However, the attributes C1C2…C m cannot be in Y because we assumed that attributes A1A2…A n are only in X+ and are not in Y+. Thus, X is not a subset of Y.By proving the contrapositive, we have also proved if X ⊆ Y, then X+⊆ Y+.Exercise 3.2.7The algorithm to find X+ is outlined on pg. 76. Using that algorithm, we can prove that (X+)+ = X+. We will do this by using a proof by contradiction.Suppose that (X+)+≠ X+. Then for (X+)+, it must be that some FD allowed additional attributes to be added to the original set X+. For example, X+→ A where A is some attribute not in X+. However, if this were the case, then X+ would not be the closure of X. The closure of X would have to include A as well. This contradicts the fact that we weregiven the closure of X, X+. Therefore, it must be that (X+)+ = X+ or else X+ is not the closure of X.Exercise 3.2.8aIf all sets of attributes are closed, then there cannot be any nontrivial functional dependencies. Suppose A1A2...A n→B is a nontrivial dependency. Then {A1A2...A n}+ contains B and thus A1A2...A n is not closed.Exercise 3.2.8bIf the only closed sets are ø and {A,B,C,D}, then the following FDs hold:A→B A→C A→DB→A B→C B→DC→A C→B C→DD→A D→B D→CAB→C AB→DAC→B AC→DAD→B AD→CBC→A BC→DBD→A BD→CCD→A CD→BABC→DABD→CACD→BBCD→AExercise 3.2.8cIf the only closed sets are ø, {A,B} and {A,B,C,D}, then the following FDs hold:A→BB→AC→A C→B C→DD→A D→B D→CAC→B AC→DAD→B AD→CBC→A BC→DBD→A BD→CCD→A CD→BABC→DABD→CACD→BBCD→AExercise 3.2.9We can think of this problem as a situation where the attributes A,B,C represent cities and the functional dependencies represent one way paths between the cities. The minimal bases are the minimal number of pathways that are needed to connect the cities. We do not want to create another roadway if the two cities are already connected.The systematic way to do this would be to check all possible sets of the pathways. However, we can simplify the situation by noting that it takes more than two pathways to visit the two other cities and come back. Also, if we find a set of pathways that is minimal, adding additional pathways will not create another minimal set.The two sets of minimal bases that were given in example 3.11 are:{A→B, B→C, C→A}{A→B, B→A, B→C, C→B}The additional sets of minimal bases are:{C→B, B→A, A→C}{A→B, A→C, B→A, C→A}{A→C, B→C, C→A, C→B}Exercise 3.2.10aWe need to compute the closures of all subsets of {ABC}, although there is no need to think about the empty set or the set of all three attributes. Here are the calculations for the remaining six sets:{A}+=A{B}+=B{C}+=ACE{AB}+=ABCDE{AC}+=ACE{BC}+=ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: C→A and AB→C. Note that BC->A is true, but follows logically from C->A, and therefore may be omitted from our list.Exercise 3.2.10bWe need to compute the closures of all subsets of {ABC}, although there is no need to think about the empty set or the set of all three attributes. Here are the calculations for the remaining six sets:{A}+=AD{B}+=B{C}+=C{AB}+=ABDE{AC}+=ABCDE{BC}+=BCWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: AC→B.Exercise 3.2.10cWe need to compute the closures of all subsets of {ABC}, although there is no need tothink about the empty set or the set of all three attributes. Here are the calculationsfor the remaining six sets:{A}+=A{B}+=B{C}+=C{AB}+=ABD{AC}+=ABCDE{BC}+=ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: AC→B and BC→A.Exercise 3.2.10dWe need to compute the closures of all subsets of {ABC}, although there is no need tothink about the empty set or the set of all three attributes. Here are the calculationsfor the remaining six sets:{A}+=ABCDE{B}+=ABCDE{C}+=ABCDE{AB}+=ABCDE{AC}+=ABCDE{BC}+=ABCDEWe ignore D and E, so a basis for the resulting functional dependencies for ABC is: A→B, B→C and C→A.Exercise 3.2.11For step one of Algorithm 3.7, suppose we have the FD ABC→DE. We want to use Armstrong’s Axioms to show that ABC→D and ABC→E follow. Surely the functional dependencies DE→D and DE→E hold because they are trivial and follow the reflexivity property. Using the transitivity rule, we can derive the FD ABC→D from the FDs ABC→DE and DE→D. Likewise, we can do the same for ABC→DE and DE→E and derive the FD ABC→E.For steps two through four of Algorithm 3.7, suppose we have the initial set ofattributes of the closure as ABC. Suppose also that we have FDs C→D and D→E. Accordingto Algorithm 3.7, the closure should become ABCDE. Taking the FD C→D and augmenting both sides with attributes AB we get the FD ABC→ABD. We can use the splitting method in stepone to get the FD ABC→D. Since D is not in the closure, we can add attribute D. Taking the FD D→E and augmenting both sides with attributes ABC we get the FD ABCD→ABCDE.Using again the splitting method in step one we get the FD ABCD→E. Since E is not in the closure, we can add attribute E.Given a set of FDs, we can prove that a FD F follows by taking the closure of the left side of FD F. The steps to compute the closure in Algorithm 3.7 can be mimicked by Armstrong’s axioms and thus we can prove F from the given set of FDs using Armstrong’s axioms.Exercise 3.3.1aIn the solution to Exercise 3.2.1 we found that there are 14 nontrivial dependencies, including the three given ones and eleven derived dependencies. They are: C→A, C→D,D→A, AB→D, AB→ C, AC→D, BC→A, BC→D, BD→A, BD→C, CD→A, ABC→D, ABD→C, and BCD→A.We also learned that the three keys were AB, BC, and BD. Thus, any dependency above that does not have one of these pairs on the left is a BCNF violation. These are: C→A, C→D,D→A, AC→D, and CD→A.One choice is to decompose using the violation C→D. Using the above FDs, we get ACD and BC as decomposed relations. BC is surely in BCNF, since any two-attribute relation is. Using Algorithm 3.12 to discover the projection of FDs on relation ACD, we discover that ACD is not in BCNF since C is its only key. However, D→A is a dependency that holds in ABCD and therefore holds in ACD. We must further decompose ACD into AD and CD. Thus, the three relations of the decomposition are BC, AD, and CD.Exercise 3.3.1bBy computing the closures of all 15 nonempty subsets of ABCD, we can find all thenontrivial FDs. They are B→C, B→D, AB→C, AB→D, BC→D, BD→C, ABC→D and ABD→C. From the closures we can also deduce that the only key is AB. Thus, any dependency above that does not contain AB on the left is a BCNF violation. These are: B→C, B→D, BC→D andBD→C.One choice is to decompose using the violation B→C. Using the above FDs, we get BCD and AB as decomposed relations. AB is surely in BCNF, since any two-attribute relation is. Using Algorithm 3.12 to discover the projection of FDs on relation BCD, we discover that BCD is in BCNF since B is its only key and the projected FDs all have B on the left side. Thus the two relations of the decomposition are AB and BCD.Exercise 3.3.1cIn the solution to Exercise 3.2.2(ii), we found that there are 12 nontrivial dependencies, including the four given ones and the eight derived ones. They are AB→C, BC→D, CD→A, AD→B, AB→D, AD→C, BC→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.We also found out that the keys are AB, AD, BC, and CD. Thus, any dependency above that does not have one of these pairs on the left is a BCNF violation. However, all of the FDs contain a key on the left so there are no BCNF violations.No decomposition is necessary since all the FDs do not violate BCNF.Exercise 3.3.1dIn the solution to Exercise 3.2.2(iii), we found that there are 28 nontrivial dependencies, including the four given ones and the 24 derived ones. They are A→B, B→C, C→D, D→A, A→C, A→D, B→D, B→A, C→A, C→B, D→B, D→C, AB→C, AB→D, AC→B, AC→D,AD→B, AD→C, BC→A, BC→D, BD→A, BD→C, CD→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.We also found out that the keys are A,B,C,D. Thus, any dependency above that does nothave one of these attributes on the left is a BCNF violation. However, all of the FDs contain a key on the left so there are no BCNF violations.No decomposition is necessary since all the FDs do not violate BCNF.Exercise 3.3.1eBy computing the closures of all 31 nonempty subsets of ABCDE, we can find all the nontrivial FDs. They are AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ABE→C, ABE→D, ADE→C, BCE→D, BDE→C, ABCE→D, and ABDE→C. From the closures we canalso deduce that the only key is ABE. Thus, any dependency above that does not contain ABE on the left is a BCNF violation. These are: AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ADE→C, BCE→D and BDE→C.One choice is to decompose using the violation AB→C. Using the above FDs, we get ABCDand ABE as decomposed relations. Using Algorithm 3.12 to discover the projection of FDs on relation ABCD, we discover that ABCD is not in BCNF since AB is its only key and the FD B→D follows for ABCD. Using violation B→D to further decompose, we get BD and ABC as decomposed relations. BD is in BCNF because it is a two-attribute relation. Using Algorithm 3.12 again, we discover that ABC is in BCNF since AB is the only key and AB→Cis the only nontrivial FD. Going back to relation ABE, following Algorithm 3.12 tells us that ABE is in BCNF because there are no keys and no nontrivial FDs. Thus the three relations of the decomposition are ABC, BD and ABE.Exercise 3.3.1fBy computing the closures of all 31 nonempty subsets of ABCDE, we can find all the nontrivial FDs. They are: C→B, C→D, C→E, D→B, D→E, AB→C, AB→D, AB→E, AC→B, AC→D, AC→E, AD→B, AD→C, AD→E, BC→D, BC→E, BD→E, CD→B, CD→E, CE→B, CE→D, DE→B,ABC→D, ABC→E, ABD→C, ABD→E, ABE→C, ABE→D, ACD→B, ACD→E, ACE→B, ACE→D, ADE→B, ADE→C, BCD→E, BCE→D, CDE→B, ABCD→E, ABCE→D, ABDE→C and ACDE→B. From the closures we can also deduce that the keys are AB, AC and AD. Thus, any dependency above that does not contain one of the above pairs on the left is a BCNF violation. These are: C→B, C→D,C →E,D →B, D →E, BC →D, BC →E, BD →E, CD →B, CD →E, CE →B, CE →D, DE →B, BCD →E, BCE →D and CDE →B.One choice is to decompose using the violation D →B. Using the above FDs, we get BDE and ABC as decomposed relations. Using Algorithm 3.12 to discover the projection of FDs on relation BDE, we discover that BDE is in BCNF since D, BD, DE are the only keys and all the projected FDs contain D, BD, or DE in the left side. Going back to relation ABC,following Algorithm 3.12 tells us that ABC is not in BCNF because since AB and AC are its only keys and the FD C →B follows for ABC. Using violation C →B to further decompose, we get BC and AC as decomposed relations. Both BC and AC are in BCNF because they are two-attribute relations. Thus the three relations of the decomposition are BDE, BC and AC.Exercise 3.3.2Yes, we will get the same result. Both A →B and A →BC have A on the left side and part ofthe process of decomposition involves finding {A}+to form one decomposed relation and Aplus the rest of the attributes not in {A}+as the second relation. Both cases yield the same decomposed relations.Exercise 3.3.3Yes, we will still get the same result. Both A →B and A →BC have A on the left side andpart of the process of decomposition involves finding {A}+to form one decomposedrelation and A plus the rest of the attributes not in {A}+as the second relation. Both cases yield the same decomposed relations.Exercise 3.3.4This is taken from Example 3.21 pg. 95.Suppose that an instance of relation R only contains two tuples.The projections of R onto the relations with schemas {A,B} and {B,C} are:If we do a natural join on the two projections, we will get:The result of the natural join is not equal to the original relation R.Exercise 3.4.1aThis is the initial tableau:→A.Since there is not an unsubscripted row, the decomposition for R is not lossless for this set of FDs.We can use the final tableau as an instance of R as an example for why the join is not lossless. The projected relations are:The joined relation is:The joined relation has three more tuples than the original tableau.Exercise 3.4.1bThis is the initial tableau:This is the final tableau after applying FDs AC→E and BC→DSince there is an unsubscripted row, the decomposition for R is lossless for this set of FDs.Exercise 3.4.1cThis is the initial tableau:This is the final tableau after applying FDs A→D, D→E and B→D.Since there is an unsubscripted row, the decomposition for R is lossless for this set of FDs.Exercise 3.4.1dThis is the initial tableau:This is the final tableau after applying FDs A→D, CD→E and E→DSince there is an unsubscripted row, the decomposition for R is lossless for this set of FDs.Exercise 3.4.2When we decompose a relation into BCNF, we will project the FDs onto the decomposed relations to get new sets of FDs. These dependencies are preserved if the union of these new sets is equivalent to the original set of FDs.For the FDs of 3.4.1a, the dependencies are not preserved. The union of the new sets of FDs is CE→A. However, the FD B→E is not in the union and cannot be derived. Thus the two sets of FDs are not equivalent.For the FDs of 3.4.1b, the dependencies are preserved. The union of the new sets of FDs is AC→E and BC→D. This is precisely the same as the original set of FDs and thus the two sets of FDs are equivalent.For the FDs of 3.4.1c, the dependencies are not preserved. The union of the new sets of FDs is B→D and A→E. The FDs A→D and D→E are not in the union and cannot be derived. Thus the two sets of FDs are not equivalent.For the FDs of 3.4.1d, the dependencies are not preserved. The union of the new sets of FDs is AC→E. However, the FDs A→D, CD→E and E→D are not in the union and cannot be derived. Thus the two sets of FDs are not equivalent.Exercise 3.5.1aIn the solution to Exercise 3.3.1a we found that there are 14 nontrivial dependencies. They are: C→A, C→D, D→A, AB→D, AB→ C, AC→D, BC→A, BC→D, BD→A, BD→C, CD→A,ABC→D, ABD→C, and BCD→A.We also learned that the three keys were AB, BC, and BD. Since all the attributes on the right sides of the FDs are prime, there are no 3NF violations.Since there are no 3NF violations, it is not necessary to decompose the relation.Exercise 3.5.1bIn the solution to Exercise 3.3.1b we found that there are 8 nontrivial dependencies. They are B→C, B→D, AB→C, AB→D, BC→D, BD→C, ABC→D and ABD→C.We also found out that the only key is AB. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The 3NF violations are B→C, B→D, BC→D and BD→C.Using algorithm 3.26, we can decompose into relations using the minimal basis B→C andB→D. The resulting decomposed relations would be BC and BD. However, none of these two sets of attributes is a superkey. Thus we add relation AB to the result. The final set of decomposed relations is BC, BD and AB.Exercise 3.5.1cIn the solution to Exercise 3.3.1c we found that there are 12 nontrivial dependencies. They are AB→C, BC→D, CD→A, AD→B, AB→D, AD→C, BC→A, CD→B, ABC→D, ABD→C, ACD→B and BCD→A.We also found out that the keys are AB, AD, BC, and CD. Since all the attributes on the right sides of the FDs are prime, there are no 3NF violations.Since there are no 3NF violations, it is not necessary to decompose the relation.Exercise 3.5.1dIn the solution to Exercise 3.3.1d we found that there are 28 nontrivial dependencies. They are A→B, B→C, C→D, D→A, A→C, A→D, B→D, B→A, C→A, C→B, D→B, D→C, AB→C,AB→D, AC→B, AC→D, AD→B, AD→C, BC→A, BC→D, BD→A, BD→C, CD→A, CD→B, ABC→D,ABD→C, ACD→B and BCD→A.We also found out that the keys are A,B,C,D. Since all the attributes on the right sidesof the FDs are prime, there are no 3NF violations.Since there are no 3NF violations, it is not necessary to decompose the relation.Exercise 3.5.1eIn the solution to Exercise 3.3.1e we found that there are 16 nontrivial dependencies. They are AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ABE→C, ABE→D, ADE→C, BCE→D, BDE→C, ABCE→D, and ABDE→C.We also found out that the only key is ABE. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The 3NFviolations are AB→C, DE→C, B→D, AB→D, BC→D, BE→C, BE→D, ABC→D, ABD→C, ADE→C, BCE→D and BDE→C.Using algorithm 3.26, we can decompose into relations using the minimal basis AB→C,DE→C and B→D. The resulting decomposed relations would be ABC, CDE and BD. However,none of these three sets of attributes is a superkey. Thus we add relation ABE to the result. The final set of decomposed relations is ABC, CDE, BD and ABE.Exercise 3.5.1fIn the solution to Exercise 3.3.1f we found that there are 41 nontrivial dependencies. They are: C→B, C→D, C→E, D→B, D→E, AB→C, AB→D, AB→E, AC→B, AC→D, AC→E, AD→B, AD→C, AD→E, BC→D, BC→E, BD→E, CD→B, CD→E, CE→B, CE→D, DE→B, ABC→D, ABC→E,ABD→C, ABD→E, ABE→C, ABE→D, ACD→B, ACD→E, ACE→B, ACE→D, ADE→B, ADE→C, BCD→E, BCE→D, CDE→B, ABCD→E, ABCE→D, ABDE→C and ACDE→B.We also found out that the keys are AB, AC and AD. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The3NF violations are C→E, D→E, BC→E, BD→E, CD→E and BCD→E.Using algorithm 3.26, we can decompose into relations using the minimal basis AB→C, C→D, D→B and D→E. The resulting decomposed relations would be ABC, CD, BD and DE. Since relation ABC contains a key, we can stop with the decomposition. The final set of decomposed relations is ABC, CD, BD and DE.Exercise 3.5.2aThe usual procedure to find the keys would be to take the closure of all 63 nonempty subsets. However, if we notice that none of the right sides of the FDs contains。
数据库原理第三章课后习题答案
第三章作业一、试述SQL特点SQL集数据查询、数据操纵、数据定义和数据控制功能于一体,其主要特点包括以下几部分。
1.综合统一2.高度非过程化3.面向集合的操作方式4.以同一种语法结构提供多种使用方式5.语言简洁,易学易用二、设有两个关系S(A,B,C,D)和T(C,D,E,F),写出与下列查询等价的SQL表达式(1)select A,B,S.C, S.D,E,Ffrom S,Twhere S.C=T.C(2)select * from S,Twhere S.C=T.C三、设关系RA B C10 NULL 2020 30 NULL写出查询语句SELECT * FROM R WHERE X的查询结果,其中X分别为1.1 A IS NULL;1.2 A>8 AND B<20;1.3 A>8 OR B<20;1.4 C+10>25;1.5 EXISTS (SELECT B FROM R WHERE A=10);use Rcreate table R(A tinyint primary key,B tinyint,C tinyint)1.11.21.31.41.5四、基于教材中的学生-课程数据库,用SQL完成如下查询:2.1 创建一张新表,记录每个学生的学号、选课门数和总学分数。
格式如下SCC(sno, totalCourse, totalCredit)并插入每个学生相应的数据。
create table SCC( sno char(10),totalcourse tinyint,totalcredit int)insertinto SCC(sno,totalcourse,totalcredit)select sc.sno,count(distinct o)as totalcourse,sum(ccredit)as totalcredit from sc,student,coursegroup by sc.snoselect*from SCC2.2、查询缺考和不及格课程多于3门的学生的学号和姓名select sc.sno,snamefrom student,scwhere exists(select snofrom scwhere grade<60 or grade=nullgroup by snohaving count(grade)>3)2.3 查询每个学生超过他自己选修课程平均成绩的课程号(写出3种以上类型的方法)(1)select cnofrom sc,(select sno,avg(grade)from sc group by sno)as avg_sc(avg_sno,avg_grade)where sc.sno=avg_sc.avg_sno and sc.grade>=avg_sc.avg_grade(2)select sno,cnofrom sc xwhere grade>=(select avg(grade)from sc ywhere y.sno=x.sno);2.4 查询同时选修了“数据库”和“数据结构”的学生的学号和姓名(写出5种以上类型方法)(1)select sno,snamefrom student,coursewhere cname='数据库'and sno in(select snofrom scwhere cname='数据结构')(2)select sc.sno,snamefrom student,course,scwhere student.sno=sc.sno and o=o and cname='数据库'intersectselect sc.sno,snamefrom student,course,scwhere student.sno=sc.sno and o=o and cname='数据结构';五、在上机实践过程中遇到过什么问题?解决方案是什么?。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第三章关系数据库
一、单项选择题
1、实体是信息世界中的术语,与之对应的关系数据库术语为( A )。
A、元组
B、数据库
C、字段
D、文件
2、关系数据表的主关键字由( D )个字段组成。
A、一个
B、两个
C、多个
D、一个或几个
3、在概念模型中,一个实体集对应于关系模型中的一个( D )。
A、元组
B、字段
C、属性
D、关系
4、下列叙述中,( A )是不正确的?
A、一个关系中可以出现相同的行
B、关系中的列称为属性
C、关系中的行称为元组
D、属性的取值范围称为域
5、下列关于关系模式的码的叙述中,( C )是不正确的?
A、从候选码中选出一个作为主码,在关系中只能有一个主码
B、主码可以是单个属性,也可以是属性组
C、在关系中只能有一个候选码
D、若一个关系模式中的所有属性构成码,则称为全码
6、关于关系模式的关键字,以下说法正确的是( B )。
A、一个关系模式可以有多个主关键字
B、一个关系模式可以有多个侯选关键字
C、主关键字可以取空值
D、关系模式必须有主关键字
7、在关系数据库中,关系是指( D )。
A、视图
B、属性
C、实体
D、二维表
8、如果A表示某学校学生的集合,B表示该学校所有课程的集合,则A与B的
笛卡尔积表示( A )。
A、所有可能选课的情况
B、所有学生选部分课程的情况
C、所有课程被部分学生选课的情况
D、均不是
9、如果集合A含2个元素,集合B含3个元素,则A与B的笛卡尔积包含( B )
个元素。
A、2
B、6
C、3
D、5
10、数据的完整性是指( C )。
A、数据的存储和使用数据的程序无关
B、防止数据被非法使用
C、数据的正确性、一致性
D、减少重复数据
11、关系模型中有三类基本的完整性约束,定义外部关键字实现的是( C )。
A、实体完整性
B、域完整性
C、参照完整性
D、实体完整性、参照完整性和域完整性
12、某表的性别字段只能输入男或女,属于( B )约束。
A、实体完整性
B、域完整性
C、参照完整性
D、实体完整性、参照完整性和域完整性
13、关系代数运算是以( C )为基础的运算。
A、关系运算
B、谓词演算
C、集合运算
D、代数运算
14、对关系s和关系r进行集合运算,结果中既包含s中元组也包含r中元组,
这种集合运算称为( A )。
A、并运算
B、交运算
C、差运算
D、积运算
15、在关系运算中,投影运算是( B )。
A、在基本表中选择满足条件的记录组成一个新的关系
B、在基本表中选择字段组成一个新的关系
C、在基本表中选择满足条件的记录和属性组成一个新的关系
D、其他三项都正确
16、在关系运算中,选择运算是( A )。
A、在基本表中选择满足条件的记录组成一个新的关系
B、在基本表中选择字段组成一个新的关系
C、在基本表中选择满足条件的记录和属性组成一个新的关系
D、其他三项都正确
17、关系代数中的连接操作是由( B )操作组合而成。
A、选择和投影
B、选择和笛卡儿积
C、投影、选择、笛卡儿积
D、投影和笛卡儿积
18、专门的关系运算不包括下列中的( D )。
A、连接运算
B、选择运算
C、投影运算
D、交运算
19、数据库中的冗余数据是指( D )的数据。
A、容易产生错误
B、容易产生冲突
C、无关紧要
D、由基本数据导出
20、如何构造出一个合适的数据逻辑结构是( C )主要解决的问题。
A、关系数据库优化
B、数据字典
C、关系数据库规范化理论
D、关系数据库查询
21、关系数据库规范化是为了解决关系数据库中( A )的问题而引入的。
A、插入、删除及数据冗余
B、提高查询速度
C、减少数据操作的复杂性
D、保证数据的安全性和完整性
22、规范化理论是关系数据库进行逻辑设计的理论依据,根据这个理论,关系
数据库中的关系必须满足:每一个属性都是( B )。
A、长度不变的
B、不可分解的
C、互相关联的
D、互不相关的
23、在关系数据库中,任何一个关系模式都必须满足( A )。
A、第一范式
B、第二范式
C、第三范式
D、BC范式
24、关系模式中,满足2nf的模式( B )。
A、可能是1nf
B、必定是1nf
C、必定是3nf
D、必定是bcnf
25、在关系数据库系统中,数据模式设计即设计一组( B )来记录用户需求
数据。
A、实体
B、二维表
C、属性
D、视图
26、区分不同实体的依据是( B )。
A、名称
B、属性
C、对象
D、概念
27、关系数据库操作的处理单位( D )。
A、字段
B、记录
C、数据库
D、关系
28、关系R(学号,姓名,性别,生日,入学总成绩)经过( B )运算得到新的关系
S(学号,姓名,入学总成绩)。
A、选择
B、投影
C、连接
D、除
29、关于主关键字描述错误的是( D )。
A、能唯一标识关系中不同元组的属性或属性组合
B、设置关系的主关键字,以保证实体完整性
C、主关键字不能重复
D、主关键字可以取空值
30、在Access表设计中,通过有效性规则设置某表【成绩】字段的值在0~
100之间,属于( B )完整性约束。
A、实体
B、域
C、参照
D、有效
31、在关系数据库中,关系是指( A )。
A、二维表
B、实体
C、属性
D、视图
二、判断题
1、关系表中的每一行称作一个元组。
√
2、关系表中的每一列称为属性。
√
3、对关系的描述称为关系模型。
×
4、数据表的关键字用于唯一标识一个记录,每个表必须具有一个关键字,主
关键字只能由一个字段组成。
×
5、候选关键字和主关键字不同,不能唯一标识一个记录。
×
6、按照完整性规则,外部关键字的值应该和关联表中的主键值保持一致。
√
7、关系就是二维表,二维表就是关系。
×
8、消除了部分函数依赖的1nf的关系模式必定是2nf。
√
9、关系数据库有坚实的理论基础支持,具有数据结构简单、易理解的特点,
得到众多开发商的支持,是目前主流的数据模型。
√
10、如果A表示某学校学生的集合,B表示该学校所有课程的集合,则A与B
的笛卡尔积表示所有可能选课的情况。
√。