数据库实验报告.docx

上传人:b****5 文档编号:6407104 上传时间:2023-01-06 格式:DOCX 页数:15 大小:192.23KB
下载 相关 举报
数据库实验报告.docx_第1页
第1页 / 共15页
数据库实验报告.docx_第2页
第2页 / 共15页
数据库实验报告.docx_第3页
第3页 / 共15页
数据库实验报告.docx_第4页
第4页 / 共15页
数据库实验报告.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

数据库实验报告.docx

《数据库实验报告.docx》由会员分享,可在线阅读,更多相关《数据库实验报告.docx(15页珍藏版)》请在冰豆网上搜索。

数据库实验报告.docx

数据库实验报告

 

实验报告

(2010/2011学年第二学期)

 

课程名称

数 据 库 系 统与设计

实验名称

数据库系统程序设计与分析

实验时间

2011

5

10/17

指导单位

计算机学院/软件学院

指导教师

沈苏彬、李莉

 

学生姓名

欧阳乐

班级学号

B09050326

学院(系)

计算机学院

专业

软件工程(NIIT)

实验报告

实验名称

数据库系统程序设计与分析

指导教师

沈苏彬、李莉

实验类型

上机

实验学时

6

实验时间

2011.5.10/17

一、实验目的和要求

(1)掌握数据库系统创建和数据添加的程序设计方法

(2)掌握采用数据库查阅语言SQL实现关系数据库查询的程序设计方法

(3)比较核心SQL与完全SQL在功能和性能方面的差异

二、实验环境(实验设备)

硬件:

微机

软件:

SQLServer2005

三、实验原理及内容

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编制的相关查询的关系代数表达式,利用核心数据库查询语言(即核心SQL,不包括GROUPBY、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在功能和性能方面的差异。

参考教材

JeffreyD.Ullman,JenniferWidom著,岳丽华,龚育昌等译.数据库系统基础教程(英文版第二版).机械工业出版社,2009.1.

 

实验报告

一、数据库系统创建和数据添加

按以下步骤进行:

1、打开程序,并以正确用户名登陆。

2、在合适的数据库里新建表格(关系),若没有还要先新建数据库,再建立表格;然后填写属性和属性的类型、约束的要求。

3、先收起ObjectExplorer里打开的表,刷新数据库后再打开,就可以看到刚才建立的几张表,以“dbo.xxx”的格式出现,下面将数据写入或拷贝进也可以。

下面就可以进行查询和修改操作了。

二、数据查询

1、教材第164页习题6.2.2

--a

selectmaker,speedfromLaptop,Productwherehd>=30andLaptop.model=Product.model

查询结果:

--b

selectProduct.model,pricefromProduct,PCwhereProduct.maker='B'andPC.model=Product.model

union

selectProduct.model,pricefromProduct,LaptopwhereProduct.maker='B'andLaptop.model=Product.model

union

selectProduct.model,pricefromProduct,PrinterwhereProduct.maker='B'andPrinter.model=Product.model

查询结果:

--c

selectdistinctmakerfromProduct,Laptop

whereProduct.model=Laptop.model

except

selectdistinctmakerfromProduct,PC

whereProduct.model=PC.model

查询结果:

--d

selectdistinctpc1.hdfromPCpc1,PCpc2

wherepc1.model<>pc2.modelandpc1.hd=pc2.hd

查询结果:

--e

selectpc1.model,pc2.modelfromPCpc1,PCpc2

wherepc1.ram=pc2.ramandpc1.speed=pc2.speedandpc1.model

查询结果:

--f.2

selectmakerfrom(

selectmaker,Laptop.model,speedfromProduct,LaptopwhereProduct.model=Laptop.modelandspeed>=1000

union

selectmaker,PC.model,speedfromProduct,PCwhereProduct.model=PC.modelandspeed>=1000)mms

groupbymakerhavingcount(model)>=2

查询结果:

2、172页习题6.3.1核心数据库查询语言

--a.1

selectmaker

fromProduct

wheremodelin(selectmodelfrompcwherespeed>1200)

查询结果:

--b.1

selectmodelfromprinterwhereprice>=All(selectpricefromprinter)

查询结果:

--c.1

selectlaptop.modelfromlaptopwherelaptop.speed

查询结果:

--d.1

(selectmodelfrompcwhereprice>=All((selectpricefrompc)union(selectpricefromlaptop)union(selectpricefromprinter)))

union

(selectmodelfromlaptopwhereprice>=All((selectpricefrompc)union(selectpricefromlaptop)union(selectpricefromprinter)))

union

(selectmodelfromprinterwhereprice>=All((selectpricefrompc)union(selectpricefromlaptop)union(selectpricefromprinter)))

查询结果:

--e.1

selectmakerfromProductwheremodelin(selectmodelfromPrinterwherecolor='true'andprice<=ALL(selectpricefromPrinter))

查询结果:

--f.1

selectmakerfromProductwheremodelin(selectmodelfromPCwherespeed>=ALL(selectspeedfromPC)andram<=ALL(selectramfrompc))

查询结果:

3、178页习题6.4.6完全数据库查询语言

--a

selectPCavgSpeed=avg(speed)fromPC

查询结果:

--b

selectLaptopavgPrice=avg(price)fromLaptop

whereprice>2000

查询结果:

--c

selectmAPCavgPrice=avg(price)fromProduct,PC

wheremaker='A'andProduct.model=PC.model

查询结果:

--d.2

selectmBPLavgPrice=avg(price)from(

selectProduct.model,pricefromProduct,PC

wheremaker='D'andProduct.model=PC.model

union

selectProduct.model,pricefromProduct,Laptop

wheremaker='D'andProduct.model=Laptop.model)DProduct

查询结果:

--e

selectspeed,difSpPCPrice=avg(price)fromPC

groupbyspeed

查询结果:

--f.2

selectmaker,LaptopavgPrice=avg(price)from(

selectmaker,pricefromProduct,Laptop

whereProduct.model=Laptop.model)makerLaptopPrice

groupbymaker

查询结果:

--g

selectmakerfromProduct

wheretype='pc'groupbymaker

havingcount(distinctmodel)>=3

查询结果:

--h

selectmaker,HighestPrice=max(price)fromProduct,PC

whereProduct.model=PC.modelgroupbymaker

查询结果:

--i

selectPCavgPrice=avg(price)fromPC

wherespeed>800

查询结果:

--j.2

selectPrinterMaker.maker,PCavgHd=avg(hd)from(selectmakerfromProductwheretype='printer')PrinterMaker,PC,Product

wherePrinterMaker.maker=Product.makerandProduct.model=PC.model

groupbyPrinterMaker.maker

查询结果:

4、对习题6.3.1最后三题用完全数据库查询语言查询

--d.2

selectmodelfrom(

selectmodel,pricefromPC

union

selectmodel,pricefromLaptop

union

selectmodel,pricefromPrinter)MP

whereprice=(selectmax(price)from(

selectmodel,pricefromPC

union

selectmodel,pricefromLaptop

union

selectmodel,pricefromPrinter)MP)

查询结果:

查询时间:

00:

00:

00.852?

00:

00:

00.055?

不确定

--e

selectmakerfromProduct,Printer

whereProduct.model=Printer.modelandprice=(selectmin(price)fromPrinter

whereexists(selectpricefromProduct,Printer

whereProduct.model=Printer.modelandcolor='true'))

查询结果:

查询时间:

00:

00:

00.411?

00:

00:

00.049?

--f

selectmakerfromProduct,PC

whereProduct.model=PC.modelandspeed=(selectmax(speed)fromPC)andram=(selectmin(ram)fromPC)

查询结果:

查询时间:

00:

00:

00.059?

00:

00:

00.047?

00:

00:

00.032?

三、对比2和4的查询语句、查询结果和查询花费的时间,比较核心SQL和完全SQL在功能和性能方面的差异。

在实际查询中,由于某些原因(比如之前查询过相关的表,还有缓存),同一个查询语句执行在时间上会发生很大出入,但是就相对时间而言,完全SQL查询更快;而且完全SQL的查询语句也更为简洁,功能更加丰富。

既然这样为什么还要核心SQL呢?

也许是核心SQL作为基础可以进一步扩展,产生更多的具有复杂功能的查询语句;也可能是测量的原因(比如测量的先后顺序);还有通过核心SQL更能体现出关系代数的过程,有利于大家的理解。

实验报告

四、实验小结(包括问题和解决方法、心得体会、意见与建议等)

实验过程中遇到了很多问题,比如登录时用户名的输入,由于学校机器的限制,名称要拼写完全,这样比如PC-047,按这样输入也不能登陆,在老师的帮助下才知道“-”要去掉。

在编写查询语句时,对子查询,通过实验知道子表是不能在where子句中被查询的,只能与子表中的元素比较使用。

尝试用视图来解决部分需要子查询或中间查询的题目,在关系代数的推演上是一样的,而视图查询的结构看起来更简单,只不过代价是时间使用较多。

在使用SQL查询时,发现它可以通过“Executionplan”按钮展示各部分查询所用时间占总时间的百分比,以及查询的过程;于是就想知道它是怎样查询优化的,查阅了一点资料,算是意料之中的收获吧。

至于建议,就是希望老师不要对我们的学习态度太乐观,还有把题目讲简单点,感觉简单的题目被讲复杂了。

五、指导教师评语

成绩

批阅人

日期

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1