1、数据库实验报告实 验 报 告(1 0 / 1 1 学年 第 二 学期)课程名称数据库系统 与 设 计实验名称数据库系统程序设计与分析实验时间1 1年5月10/17日指导单位计算机学院/软件学院指导教师沈苏彬、李莉学生姓名欧阳乐班级学号B09050326学院(系)计算机学院专 业软件工程(NIIT)实 验 报 告实验名称数据库系统程序设计与分析指导教师沈苏彬、李莉实验类型上机实验学时6实验时间2011.5.10/17一、 实验目的和要求 (1)掌握数据库系统创建和数据添加的程序设计方法(2)掌握采用数据库查阅语言SQL实现关系数据库查询的程序设计方法(3)比较核心SQL与完全SQL在功能和性能方
2、面的差异 二、实验环境(实验设备)硬件:微机软件:SQL Server 2005三、实验原理及内容3.1 利用教材第127页习题 5.2.1给出的关系模式和数据,建立包括Product、PC、Laptop、Printer四个关系模式的关系数据库,并且录入给出的关系数据库数据。3.2 按照教材第164页习题6.2.2要求,编制查询语句,并且利用已经3.1建立的关系数据库,执行查询语句,得出查询结果。在编制查询语句时,建议首先罗列出关系代数表达式。也可以利用查询的结果,验证所设计的关系代数表达式的正确性3.3 按照教材第172页习题 6.3.1要求,参照习题5.2.1编制的相关查询的关系代数表达式
3、,利用核心数据库查询语言(即核心SQL,不包括GROUP BY、SUM、AVG、MIN、MAX、和COUNT语句)编制查询语句,对在3.1中建立的关系数据库进行查询,并且记录查询结果。3.4 按照教材第178页习题 6.4.6要求,利用完全数据库查询语言(完全SQL)编制查询语句,对在3.1中建立的关系数据库进行查询,并且记录查询结果。3.5 选择3.3中最后三个练习题,采用完全SQL编制相关的查询语句,针对在3.1中建立的关系数据库进行查询,记录查询结果和查询的时间。3.6 对比3.3和3.5的查询语句、查询结果和查询花费的时间,比较核心SQL和完全SQL在功能和性能方面的差异。参考教材Je
4、ffrey D. Ullman, Jennifer Widom著,岳丽华, 龚育昌等译. 数据库系统基础教程 (英文版第二版). 机械工业出版社, 2009.1.实 验 报 告一、数据库系统创建和数据添加按以下步骤进行:1、打开程序,并以正确用户名登陆。2、在合适的数据库里新建表格(关系),若没有还要先新建数据库,再建立表格;然后填写属性和属性的类型、约束的要求。3、先收起Object Explorer里打开的表,刷新数据库后再打开,就可以看到刚才建立的几张表,以“dbo.xxx”的格式出现,下面将数据写入或拷贝进也可以。下面就可以进行查询和修改操作了。二、数据查询1、教材第164页习题6.2
5、.2-aselect maker,speed from Laptop,Product where hd=30 and Laptop.model=Product.model查询结果:-bselect Product.model,price from Product,PC where Product.maker=B and PC.model=Product.model unionselect Product.model,price from Product,Laptop where Product.maker=B and Laptop.model=Product.modelunionselect
6、Product.model,price from Product,Printer where Product.maker=B and Printer.model=Product.model查询结果:-cselect distinct maker from Product,Laptopwhere Product.model=Laptop.model exceptselect distinct maker from Product,PCwhere Product.model=PC.model查询结果:-dselect distinct pc1.hd from PC pc1,PC pc2where
7、pc1.modelpc2.model and pc1.hd=pc2.hd查询结果:-eselect pc1.model,pc2.model from PC pc1,PC pc2where pc1.ram=pc2.ram and pc1.speed=pc2.speed and pc1.model=1000unionselect maker,PC.model,speed from Product,PC where Product.model=PC.model and speed=1000) mmsgroup by maker having count(model)=2查询结果:2、172页习题 6
8、.3.1 核心数据库查询语言-a.1select makerfrom Productwhere model in (select model from pc where speed 1200)查询结果:-b.1select model from printer where price = All(select price from printer )查询结果:-c.1select laptop.model from laptop where laptop.speed = All(select price from pc) union (select price from laptop) uni
9、on (select price from printer)union(select model from laptop where price = All(select price from pc) union (select price from laptop) union (select price from printer)union(select model from printer where price = All(select price from pc) union (select price from laptop) union (select price from pri
10、nter)查询结果:-e.1select maker from Product where model in(select model from Printer where color=true and price =ALL(select speed from PC) and ram 2000查询结果:-cselect mAPCavgPrice=avg(price) from Product,PCwhere maker=A and Product.model=PC.model查询结果:-d.2select mBPLavgPrice=avg(price) from (select Product
11、.model,price from Product,PCwhere maker=D and Product.model=PC.modelunionselect Product.model,price from Product,Laptopwhere maker=D and Product.model=Laptop.model) DProduct查询结果:-eselect speed,difSpPCPrice=avg(price) from PCgroup by speed查询结果:-f.2select maker,LaptopavgPrice=avg(price) from (select m
12、aker,price from Product,Laptopwhere Product.model=Laptop.model) makerLaptopPricegroup by maker查询结果:-gselect maker from Productwhere type=pc group by makerhaving count(distinct model)=3查询结果:-hselect maker,HighestPrice=max(price) from Product,PCwhere Product.model=PC.model group by maker查询结果:-iselect
13、PCavgPrice=avg(price) from PCwhere speed800查询结果:-j.2select PrinterMaker.maker,PCavgHd=avg(hd) from (select maker from Product where type=printer)PrinterMaker,PC,Productwhere PrinterMaker.maker=Product.maker and Product.model=PC.modelgroup by PrinterMaker.maker查询结果:4、对习题 6.3.1最后三题用完全数据库查询语言查询-d.2sele
14、ct model from(select model,price from PCunionselect model,price from Laptopunionselect model,price from Printer) MPwhere price=(select max(price) from (select model,price from PCunionselect model,price from Laptopunionselect model,price from Printer) MP)查询结果:查询时间:00:00:00.852 ?00:00:00.055?不确定-esele
15、ct maker from Product,Printerwhere Product.model=Printer.model and price=(select min(price) from Printerwhere exists(select price from Product,Printerwhere Product.model=Printer.model and color=true)查询结果:查询时间:00:00:00.411?00:00:00.049?-fselect maker from Product,PCwhere Product.model=PC.model and sp
16、eed=(select max(speed) from PC) and ram=(select min(ram) from PC)查询结果:查询时间:00:00:00.059?00:00:00.047?00:00:00.032?三、对比2和4的查询语句、查询结果和查询花费的时间,比较核心SQL和完全SQL在功能和性能方面的差异。在实际查询中,由于某些原因(比如之前查询过相关的表,还有缓存),同一个查询语句执行在时间上会发生很大出入,但是就相对时间而言,完全SQL查询更快;而且完全SQL的查询语句也更为简洁,功能更加丰富。既然这样为什么还要核心SQL呢?也许是核心SQL作为基础可以进一步扩展,产
17、生更多的具有复杂功能的查询语句;也可能是测量的原因(比如测量的先后顺序);还有通过核心SQL更能体现出关系代数的过程,有利于大家的理解。实 验 报 告四、实验小结(包括问题和解决方法、心得体会、意见与建议等) 实验过程中遇到了很多问题,比如登录时用户名的输入,由于学校机器的限制,名称要拼写完全,这样比如PC-047,按这样输入也不能登陆,在老师的帮助下才知道“-”要去掉。在编写查询语句时,对子查询,通过实验知道子表是不能在where子句中被查询的,只能与子表中的元素比较使用。尝试用视图来解决部分需要子查询或中间查询的题目,在关系代数的推演上是一样的,而视图查询的结构看起来更简单,只不过代价是时间使用较多。在使用SQL查询时,发现它可以通过“Execution plan”按钮展示各部分查询所用时间占总时间的百分比,以及查询的过程;于是就想知道它是怎样查询优化的,查阅了一点资料,算是意料之中的收获吧。至于建议,就是希望老师不要对我们的学习态度太乐观,还有把题目讲简单点,感觉简单的题目被讲复杂了。五、指导教师评语 成 绩批阅人日 期
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1