当前位置:文档之家› 课后作业答案

课后作业答案

产品数据库Product(maker, model, type)PC(model, speed, ram, hd, price)Laptop(model, speed, ram, hd, screen, price)Printer(model, color, type, price)关系Product给出了各种产品的制造厂商、型号和产品类型(PC、笔记本电脑或者打印机)。

为了简单起见,假设所有产品的型号都唯一,而不管它是由哪个制造商生产的。

关系PC对于不同型号给出了如下属性:速度(处理器的速度,单位是GHz)、RAM的大小(单位是MB)、硬盘的容量(单位是GB)以及价格。

关系Laptop与关系PC类似,它在PC的基础上增加了属性screen,即显示器的尺寸(以英寸为单位)。

关系Printer对于每种型号有如下属性:是否彩色(如果是的话,这个值是true)、处理类型(激光的还是喷墨的)以及价格。

关系PC的数据关系Laptop的数据关系Printer的数据二战中的大型舰船数据库Classes(class, type, country, numGuns, bore, displacement) Ships(name, class, launched)Battles(name, date)Outcomes(ship, battle, result)相同设计的舰船组成一个“类”,类别的名称通常就是这个类的第一艘船的名字。

关系Classes 记录了“类”的名字、型号(bb代表主力舰,bc代表巡洋舰)、生产国家、火炮的门数、火炮的尺寸(口径,单位是英寸)和排水量(重量,单位是吨)。

关系Ships记录了舰船的名字、舰船类属名字、开始服役的日期。

关系Battles给出了这些舰船参加的战役的时间。

关系Outcomes给出了各个舰船在各场战役中的结果(是沉没,还是受伤,或者完好)关系Classes的数据关系Battles的数据关系Outcomes的数据需要注意的是:这个数据库中存在着“悬浮元组”,比如,在关系Outcomes中出现的船只可能在关系Ships中查不到。

※作业一:习题1.11.对于产品数据库中的4个关系,写出它们的关系模式定义语句。

CREATE TABLE Product (maker char(3),model char(4) primary key,type char(10));CREATE TABLE PC (model char(4) primary key,speed decimal(3,2),ram int,hd int,price int);CREATE TABLE Laptop (model char(4) primary key,speed decimal(3,2),ram int,hd int,screen decimal(3,1),price int);CREATE TABLE Printer (model char(4) primary key,color char(5),type char(10),price int);2.修改关系Printer,删掉属性color。

ALTER TABLE Printer DROP COLUMN color;3.修改关系Laptop,增加属性od(光驱类型,比如CD、DVD)。

如果某个笔记本电脑没有光驱,则该属性的默认值为‘none’。

ALTER TABLE Laptop ADD od char(10) DEFAULT ‘none’;习题1.2 [选做]1.对于二战中的大型舰船数据库中的4个关系,写出它们的关系模式定义语句。

CREATE TABLE Classes (class char(20) primary key,type char(2),country char (20),numGuns smallint,bore smallint,displacement int);CREATE TABLE Ships (name char(20) primary key,class char(20),launched int);CREATE TABLE Battles (name char(20) primary key,date datetime);CREATE TABLE Outcomes (ship char(20),battle char(20),result char(7),primary key (ship, battle));2.修改关系Classes,删掉属性bore。

ALTER TABLE Classes DROP COLUMN bore;3.修改关系Ships,增加属性yard,它给出了该船的船坞。

ALTER TABLE Ships ADD yard char(30);※作业二:(写关系代数表达式)习题2.1 对于产品数据库,试写出下列查询的关系代数表达式。

1.哪种PC型号具有最少3.00的速度?πmodel(σspeed>=3.00(PC))2.哪个生产厂商的笔记本电脑的硬盘容量至少100GB?πmaker (Product ∞(σhd>=100 (Laptop)))3.查询厂商B生产的所有产品的型号和价格。

πmodel, price(σmaker=’B’ (Product) ∞ PC) ⋃πmodel, price(σmaker=’B’ (Product) ∞Laptop) ⋃πmodel, price(σmaker=’B’ (Product) ∞ Printer)4.查询所有彩色激光打印机的型号。

πmodel (σcolor=’true’∧type=’laser’ (Printer))5.查询那些只生产笔记本电脑,不生产PC的厂商。

πmaker(σtype=’laptop’(Product)) - πmaker(σtype=’pc’(Product))※习题2.2 (关系代数计算)已知关系R(A,B):{(0,1), (2,3), (0,1), (2,4), (3,4)}S(B,C):{(0,1), (2,4), (2,5), (3,4), (0,2), (3,4)}计算下面的表达式:1. πB+1, C-1(S)2. τB,A(R)3. δ(R)4. γB,A VG(C)(S)5. γA,A VG(C)(R∞S) (注:∞此处代表自然连接)6. R与S的自然左外连接7. R与S的自然右外连接答:1. {(1,0), (3,3), (3,4), (4,3), (1,1), (4,3)}2. {(0,1), (0,1), (2,3), (2,4), (3,4)}3. {(0,1), (2,3), (2,4), (3,4)}4. {(0,1.5), (2,4.5), (3,4)}5. {(2, 4)}6. {(2,3,4), (2,3,4), (0,1,NULL), (0,1,NULL),(2,4,NULL),(3,4,NULL)}7. {(2,3,4), (2,3,4), (NULL,0,1), (NULL,2,4), (NUL,2,5), (NULL,0,2)}习题2.3 [选做]对于二战中的大型舰船数据库,试写出下列查询的关系代数表达式。

1.查询那些火炮口径大于16英寸的舰船类属和生产国。

πclass,country(σbore>16 (Classes))2.查询在丹麦海峡(Denmark Strait)战役中沉没的舰船。

πship (σbattle=’Denmark Strait’∧result=’sunk’(Outcomes)3.1921年签署的华盛顿条约禁止制造超过35000吨的大型军舰,请列出那些违背华盛顿条约的军舰。

πname(σdisplacement>35000 (Classes) ∞σlaunched>1921(Ships))4.列出参加了瓜达康纳尔岛(Guadalcanal)海战的战舰的名称、排水量及火炮的数目。

πname,displacement,numGuns(σbattle=’Guadalcanal’(Outcomes)∞σOutcomes.ship=(Ships) ∞ Classes)5.列出那些既有主力舰又有巡洋舰的国家。

πcountry(σtype=’bb’ (Classes)) ⋂πcountry(σtype=’bc’ (Classes))作业三:(写SQL语句)习题3.1 对于产品数据库,用SQL写出下面的查询:以下4小题要求使用连接查询1.查询硬盘容量至少30G的笔记本电脑制造商及该电脑的速度。

SELECT maker,speedFROM Product, LaptopWHERE Product.model=Laptop.model and hd>=30;2.查询制造商B生产的任意类型的所有产品的型号和价格。

(SELECT Product.model, priceFROM Product, PCWHERE Product.model = PC.model AND maker = 'B')UNION(SELECT Product.model, priceFROM Product, LaptopWHERE Product.model = Laptop.model AND maker = 'B')UNION(SELECT Product.model, priceFROM Product, PrinterWHERE Product.model = Printer.model AND maker = 'B');3.查询出现在两种或两种以上PC中的硬盘大小。

SELECT DISTINCT X.hdFROM PC X, PC YWHERE X.hd=Y.hd and X.model<Y.model;4.查询每对具有相同速度和RAM的PC的型号。

每一对只能列出一次;例如,若(i, j)已被列出,则(j, i)就不能再被列出。

SELECT X.model,Y.modelFROM PC X, PC YWHERE X.speed=Y.speed and X.ram=Y.ram and X.model<Y.model;以下4小题要求使用子查询(使用IN、EXISTS、ANY、ALL)5.找出速度在3.0以上的PC制造商。

SELECT DISTINCT makerFROM ProductWHERE model IN (SELECT modelFROM PCWHERE speed>=3.0);6.找出价格最高的打印机。

相关主题