新浪博客

数据库原理编程与性能第二章课后习题答案

2013-11-24 23:24阅读:

数据库原理编程与性能

第二章课后习题答案

作者: 张成龙 QQ1529735616 或 2754290636


2.1bconsider the table T2 ,following
T2
A
B
C
D
E
a1
b1
c1
d1
e1
a2
b1
c1
d1
e2
a3
b1
c2
d1
e1
a4
b2
c1
d1
e1
Finder the two candidate keys for this table. 找出表T2的两个候选键

数据库的超键,候选键,主键:
超键(super key):在关系中能唯一标识元组的属性集称为关系模式的超键
候选键(candidate key):不含有多余属性的超键称为候选键
主键(primary key):用户选作元组标识的一个候选键程序主键

在上述表T2中,列A中的每一行的数据各不相同,所以列A可以作为候选键;列BCE也可以共同标识每一行的数据。所以T2的两个候选键为:ABCE

2.2aIndentify all candidate keys for the subscribers table,based on the assumptions given above . Note that there are three such keys ;one of them contains the information_no attribute and a different one contains the zip attribute . 根据题目的假定,指定表subscribers 所有候选键。这样的候选键有3个,其中一个包含information_no属性,另一个包含zip 属性。

候选键定义: 关系模式Ru)的属性集合K ∈u 的候选键,如果
1Ru)的任何一个关系实例的任意两个元素在属性集合K上的值部不相同————唯一性
2K的任何真子集都不满足条件 ————最小性
通俗点,候选键在每一行数据里的值都不相同,就像自动增长的id一样,可以说成是候选的主键。

三个候选键,分别是是:ssn , ( name information_no) 和(name address city zip ).

bwhich of these candidate keys would you choose for a primary key? Explain why .你会选择那个候选键作为主键,解释为什么。

主键的选择:
数据库主键:表中经常有一个列或列的组合,其值能唯一地标识表中的每一行。这样的一列或多列称为表的主键,通过它可强制表的实体完整性。当创建或更改表时可通过定义 PRIMARY KEY 约束来创建主键。一个表只能有一个 PRIMARY KEY 约束,而且 PRIMARY KEY 约束中的列不能接受空值。由于 PRIMARY KEY 约束确保唯一数据,所以经常用来定义标识列作用:1)保证实体的完整性;2)加快数据库的操作速度3在表中添加新记录时,ACCESS会自动检查新记录的主键值,不允许该值与其他记录的主键值重复。
4) ACCESS自动按主键值的顺序显示表中的记录。如果没有定义主键,则按输入记录的顺序显示表中的记录。主键的选择应考虑:1有意义的键和无意义的键:有意义的键:例如身份证号。键值是唯一的,而由人来指定是不可靠的,有可能会出现重复,虽然数据库可以检测出来,但是在数据进入系统之前不会被发现。所以有意义的键是不可靠的。
无意义的键:本质是数据库构造的并不打算供人使用的一个随即数。对应小系统或稳定的情况,应优先考虑。2简单键和组合键:组合键:在一个表和另一个表上下文相关时更易于使用。比如:订单号加上顺序号来作为订单的键值。简单键:一致性好。如果都使用简单键,在对键的操作中可以使用相同的代码。而组合键需要特殊处理。3表唯一键和数据库唯一键:表唯一键:在一张表中是唯一的。数据库唯一键:对任意一个表的任意一行都是唯一的。

候选键ssn 相比较( name information_no) 和(name address city zip ),本身意义简单,不会重复,可以唯一标识;是简单键,一致性好,便于操作。

2.4afinder all (ordno,pid) pairs for orders of quantity equal to 1000 or more. 找出订单总价至少为1000的(ordno , pid ).
(Orders where qty>=1000)[ordno,pid]
bfinder all products names of products priced between 0.5 and 1.0,inclusive.找出价格在0.5 1.0 之间的产品的名字。
(Products where price>=0.5 and price<=1.0 )[pname]
cfind all (ordno,cname) pairs for orders of dollar value less than 500.找出订单价格低于500的所有(ordno,cname).
(Customers JOIN (orders where dollars<500)[ordno,cid])[ordno,cname]
(Customers JOIN (orders where dollars<500))[ordno,cname]
((Customers JOIN orders)where dallars<500)[ordno,cname]
dfind all (ordno,aname)pairs for orders in march .找出3月份接受订单的 (ordno,aname)对。
(agents JOIN (orders where month=mar)[ordno,aid])[ordno,aname]
( (Agents JOIN orders )where month=mar)[ordno,aname]
(agents JOIN (orders where month=mar))[ordno,aname]
efinder all (ordno,cname,anametriples for orders in march .
(((Orders where month=mar)[0rdno,cid,aid] JOIN customers ) [ordno, cname,aid] JOIN agents)[ardno,cname,aname]
((Orders JOIN customers JOIN agents ) where month=mar)[ordno,cname,aname]
((Orders where month=mar JOIN customers ) [ordno, cname,aid] JOIN agents)[ardno,cname,aname]
ffinder all the names of agents in new york who placed orders of individual dollars values less than 500. 找出所有在new york 的代理商的名字,这些代理商接受的单个订单价格少于500.
((Agents where city=new york) JOIN (orders where dollars<500 )[aid]) )[Aname]

gfinder all products names of products in duluth ordered in march. 找出所有3月份在duluth 订购的产品的名字。
(Orders where month=mar JOIN products where city=duluth )[pname]
2.5
(a)finder all (cid aid pid)triples for customers ,agents,products combination that are all in the same city .nothing about orders is involved in this section.找出所有customers ,agents,products 都在同一城市的三元组(cid aid pid),本题不涉及订单信息。
(((Customers TIMES products) TIMES agents )where customers.city=products.city and customers.city=agents.city)[cid,aid,pid]
(b) finder all (cid aid pid)triples for customers ,agents,products combination that are not all in the same city .any two maybe.找出所有customers ,agents,products 不在同一城市的三元组(cid aid pid),可能有两个在同一城市。
(((Customers TIMES products) TIMES agents )where customers.city<>products.city 0r customers.city<>agents.city or products.city <> agents.city)[cid,aid,pid]
cfinder all (cid aid pid)triples for customers ,agents,products combination no two of which are in the same city ..找出所有customers ,agents,products 两两不在同一城市的三元组(cid aid pid)
(((Customers TIMES products) TIMES agents )where customers.city<>products.city and customers.city<>agents.city and products.city<>agents.city)[cid,aid,pid]
dget cities of agents booking and order from customer c002. 查找接受顾客c002的代理商所在的城市。
((Orders where cid =c002 ) JOIN agents )[city]
((Orders where cid =c002 )[aid] JOIN agents )[city]
eget products names ordered by at least one customer based in dallas througt an agent based in tokyo.查找至少一个在dallas 的顾客通过在tokyo 的代理商订购的商品的名字。((Orders JOIN (Customers where city=dallars)[cid] JOIN (agents where city=toyko) [aid])JOIN products)[pname]
fget pid of products orders through any agent who make one order for a customer in tokyo . Note :the request posed here is not the same as asking for pids of products ordered by a customer in tokyo. 查找曾经收到来自tokyo的顾客的低粘单的代理商所销售的商品的Pid。注意:本题和位于tokyo的顾客的订单的商品的Pid不同。
((((Customers where city=tokyo)[cid)]JOIN orders )[aid])orders)[pid]
gdisplay all pairs of aids for agents who live in the same city. 列出所有在同一城市的代理商的aid.
A1:=agents
A2:=agents
((A1 TIMES A2) where A1.city=A2.city and A1.aid
hfind cids of customers who did not place an order through agent a03. 找出没有通过代理商a03订购的顾客的cid.
Orders [cid] MINUS (Orders where aid=a03)[cid]
customers [cid] MINUS (Orders where aid=a03)[cid]
ifind cids of customers who have the largest discount , separately, find those who have the smallest discount . 找出折扣最大和最小的顾客的cid.
C1:=customers
C:=customers
(((C1 TIME C )where C1.discnt>=C.discnt)[C1.cid,C.cid]) DIVIDEBY C[cid]
UNION
(((C1 TIME C )where C1.discnt<=C.discnt)[C1.cid,C.cid]) DIVIDEBY C[cid]
jfind cids of customers who order all products.找出订购了所有商品的顾客的cid.
Orders[pid,cid] DIVIDEBY products[pid]
kfind pids from products ordered through agents a03 but not through a06.找出通过代理商a03 而不通过a06订购的商品的pid.
(Orders where orders.aid=a03)[pid] MINUS (orders where orders.aid=a06)[pid]
lget pnames and pids of products that are stored in the same city as one of the agents who sold these products. 查找产品的pname , pid .要求这些商品所在的城市相同,销售该商品的代理商所在的城市相同。
(Products JOIN (Orders JOIN(products[pid,city] JOIN Agents[aid,city]))[pid])[pid,pname]
mget aid and anames of agents with aname begining with the letter n who don not place orders for any product in newark. 查找以字母n开始的代理商的aidaname,并且这些代理商没有销售过任何newark生产的产品。
(((Orders [aid]-(Orders JOIN (products where city =newark))[aid]) JOIN agents )where aname>=n and aname<</SPAN>o)[aid,aname]
nget cids of customers who order both products p01 and p07.查找同时订购了p01 p07 的顾客的cid.
(Orders where pid=p01)[cid] INTERSECT (Orders where pid=p07)[cid]
oget names of agents who place orders for all products ordered by customers c002. 查找销售过所有曾经被顾客c002订购过的所有商品的代理商的名字。
(Orders [aid,pid] DIVIDEBY (Orders where cid=c002)[pid] JOIN agents ) [aname]
pget names of agents who place orders for all products that are ordered by any customer at all . 查找销售过所有曾经被顾客订购过的商品的代理商的名字。
((Orders [aid,pid] DIVIDEBY orders[pid])JOIN agents)[aname]
qget (cid,aid,pid)triples for customer,agents,products comnination so that at most two of them are in the same city .找出所有customers ,agents,products至少有两个位于在同一城市的三元组(cid aid pid) .
(((Customers TIMES products) TIMES agents )where customers.city=products.city and customers.city=agents.city)[cid,aid,pid]
UNION
(((Customers TIMES products) TIMES agents )where customers.city=products.city or customers.city=agents.city or products.city=agents.city)[cid,aid,pid]
rget pid of products ordered by all customers who place any order through agent a03所有曾在代理商a03处订购过商品的顾客订购过的商品的pid.
Orders [cid,pid] DIVIDEBY (Agents where aid=ao3 )[cid]
sget aids of agents who place individual orders in dollar value great than 500 for customers live in kyoto. 接受过kyoto的顾客一笔总额超过500的代理商的aid
((Customers where city=kyoto JOIN orders where dallars>500)[cid] JOIN orders)[aid]
tgive all (cname,aname)pairs where the customers placed an order through the agent.查找所有的cname,aname.要求对应的顾客通过对应的代理商订购过商品。
((customers TIMES agents JOIN orders )[cid,aid])[cname,aname]
(customers TIMES agents JOIN orders )[cname,aname]

uget cids of customers who order all their products through one agent.只从一家代理商处订购过商品的顾客的cid.
O1:=orders;
O2:=orders
O1[cid]- ((O1 TIMES O2)where O1.aid<>O2.aid and O1.cid=O2.cid)[O1.cid]


我的更多文章

下载客户端阅读体验更佳

APP专享