课后作业答案_第1页
课后作业答案_第2页
课后作业答案_第3页
免费预览已结束,剩余9页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

.产品数据库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)、处理类型(激光的还是喷墨的)以及价格。关系 product 的数据makermodeltypea1001pca1002pca1003pca2004laptopa2005laptopa2006laptopb1004pcb1005pcb1006pcb2007laptopc1007pcd1008pcd1009pcd1010pcd3004printerd3005printere1011pce1012pce1013pce2001laptope2002laptope2003laptope3001printere3002printere3003printerf2008laptopf2009laptopg2010laptoph3006printerh3007printer;.关系 pc 的数据modelspeedramhdprice10012.661024250211410022.1051225099510031.425128047810042.80102425064910053.2051225063010063.201024320104910072.20102420051010082.20204825077010092.00102425065010102.80204830077010111.86204816095910122.80102416064910133.0651280529关系 laptop 的数据modelspeedramhdscreenprice20012.00204824020.1367320021.7310248017.094920031.805126015.454920042.005126013.3115020052.16102412017.0250020062.0020488015.4170020071.83102412013.3142920081.60102410015.490020091.605128014.168020102.00204816015.42300关系 printer 的数据modelcolortypeprice3001trueink-jet993002falselaser2393003truelaser8993004trueink-jet1203005falselaser1203006trueink-jet1003007truelaser200二战中的大型舰船数据库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 的数据classtypecountrynumgunsboredisplacementbismarckbbgermany81542000iowabbusa91646000kongobcjapan81432000north carolinabbusa91637000renownbcgt. britain61532000revengebbgt. britain81529000tennesseebbusa121432000yamatobbjapan91865000关系ships 的数据nameclasslaunchedcaliforniatennessee1921harunakongo1915hieikongo1914iowaiowa1943kirishimakongo1915kongokongo1913missouriiowa1944musashiyamato1942new jerseyiowa1943north carolinanorth carolina1941ramilliesrevenge1917renownrenown1916repulserenown1916resolutionrevenge1916revengerevenge1916royal oakrevenge1916royal sovereignrevenge1916tennesseetennessee1920washingtonnorth carolina1941wisconsiniowa1944yamatoyamato1941shipbattleresultarizonapearl harborsunkbismarckdenmark straitsunkcaliforniasurigao straitokduke of yorknorth capeokfusosurigao straitsunkhooddenmark straitsunkking george vdenmark straitokkirishimaguadalcanalsunkprince of walesdenmark straitdamagedrodneydenmark straitokscharnhorstnorth capesunksouth dakotaguadalcanaldamagedtennesseesurigao straitokwashingtonguadalcanalokwest virginiasurigao straitokyamashirosurigao straitsunkname关系 battles 的数据datedenmark strait guadalcanal north capesurigao strait5/24-27/4111/15/4212/26/4310/25/44关系 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) defaultnone;习题 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)4.b,avg(c) (s)2.b,a (r)5.a,avg(c) (r3.(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(bore16(classes)2.查询在丹麦海峡(denmark strait )战役中沉没的舰船。ship (battle= denmark strait result= sunk (outcomes)3.1921 年签署的华盛顿条约禁止制造超过顿条约的军舰。35000 吨的大型军舰, 请列出那些违背华盛name (displacement35000(classes)launched1921 (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,speed from product, laptopwhere product.model=laptop.model and hd=30;2. 查询制造商b 生产的任意类型的所有产品的型号和价格。(select product.model, price from product, pcwhere product.model = pc.model and maker = b) union(select product.model, price from product, laptopwhere product.model = laptop.model and maker = b)union(select product.model, price from product, printerwhere product.model = printer.model and maker = b);3. 查询出现在两种或两种以上pc 中的硬盘大小。select distinct x.hd from pc x, pc ywhere x.hd=y.hd and x.modely.model;.4. 查询每对具有相同速度和ram 的 pc 的型号。 每一对只能列出一次;例如, 若(i, j) 已被列出,则 (j, i) 就不能再被列出。select x.model,y.model from pc x, pc ywhere x.speed=y .speed and x.ram=y.ram and x.model=3.0);6. 找出价格最高的打印机。select model from printerwhere price=all (select pricefrom printer);7. 找出速度比任何一台pc 都慢的笔记本电脑。select model from laptopwhere speedall (select speedfrom pc);8. 找出价格最低的彩色打印机的制造商select maker from product where model in (select model from printerwhere color =trueand price =3;习题 3.2 对于产品数据库,用sql 写出下面的更新:1. 通过两条insert 语句在数据库中添加如下信息:厂商c 生产的型号为1100 的 pc,其速度为 3.2, ram 容量大小为1024,硬盘容量大小为180,售价为 $2499。insert into product v alues( c, 1100, pc); insert into pc v alues( 1100, 3.2, 1024, 180, 2499);2. 删除所有硬盘容量低于100gb 的 pc 。delete from pcwhere hd=10000000;5. 找出是演员同时资产至少有$50 000 000 的电影公司经理的名字。select studiop from studiopress, executivestarwhere studiop= executives and studiopress.address= executivestar.addressand networth=50000000;为下面的属性或属性组声明索引:6. studionamecreate index nameindex on movies(studioname);7. genre 和 lengthcreate index genreindex on movies(genre,length);作业四:对于电影数据库,对所有关系都定义了主键,如下所示: movies (title, year, length, genre, studioname, producerc#) starsin (movietitle, movieyear, starname)moviestar (name, address, gender, birthdate) movieexec (name, address, cert#, networth) studio (name, address, presc#)习题 4.1 声明如下引用完整性约束:1. 电影的制片人必须是movieexec 中的某个制片人。任何对 movieexec 的更新, 若违反此约束则拒绝该操作。foreign key (producerc#) references movieexec(cert#)2. 重复( 1),但是当违反约束时,将movies 中的 producerc#置为 null 。foreign key (producerc#) references movieexec(cert#) on update set null3. 重复( 1),但是当违反约束时,movies 中违反约束的元组被删除或修改。foreign key (producerc#) references movieexec(cert#) on update cascadeon delete cascade习题 4.2 声明关于movies 的属性的约束:1. 年份不能是1915 年以前。check (year =1915)2. 长度不能少于60 也不能多于250。check (length=60 and length=250)3. 电影公司的名字只能是disney、 fox、mgm 、paramount check (studioname in (disney ,fox,mgm ,paramount)作业五:(计算)习题 5.1 针对下列关系模式和fd 集合,找出关系的所有候选码。1. r(a, b, c, d) , fd: a bc,cd ,da候选码: (a, b), (b, c), (b, d)2. s(a, b, c, d) , fd: ab, bc,bd候选码: a习题 5.2 对于下列关系模式和fd 集合,找出所有的3nf 违例,如有必要,将关系分解为一系列属于3nf 的关系。1. r(a, b, c, d) , fd: a bc,bcd, cda , adb候选码: (a, b), (b, c), (c, d), (a, d)不存在 3nf 违例2. s(a, b, c, d) , fd: ab, bc,cd , da候选码: (a ), (b), (c), (d)不存在 3nf 违例习题 5.3 考虑关系 courses(c, t, h, r, s, g) ,其属性可以理解为课程、教师、时间、教室、学生和成绩。 设 courses 上的 fd 有 ct,hrc,htr,hsr,csg。直观上表示: 一门课程有唯一的一个教师;在一个给定的时间和教室,只能有一门课程;在给定的时间里一个教师只能在一个教室;在给定的时间里一个学生只能在一个教室;一个学生在一门课程中只能得到一个成绩。1. 给出 courses 的所有候选码。候选码: (h, s)2. 证明给定的fd 本身就是它们的一个最小基本集。略。3. 将关系 courses 分解为多个3nf 关系,分解后的关系中是否有不属于bcnf 的关系?r1(c, t), r2(h, r, c), r3(h, t, r), r4(h, s, r), r5(c, s, g)分解后的关系都属于bcnf 。习题 5.4(问答)假设有关系模式r(职工编

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论