好文档 - 专业文书写作范文服务资料分享网站

成都信息工程学院 数据库实验报告

天下 分享 时间: 加入收藏 我要投稿 点赞

实验1 客房预订数据库

(1) 客房预订数据库的E-R图: 年龄

性别 证件名称 证件号码 地址 客户名称 客户编号 备注信息 属于 customer 联系电话 时间 房间等级 room 房间编号 房间价格 房间状态 (2) 根据E-R图转化出来的数据表结构:共有三张表

实验2 创建客房预订数据库和表 1.创建数据库orderroom的SQL命令;

CREATE DATABASE orderroom

ON PRIMARY

(NAME=\

FILENAME='C:\\Program Files\\Microsoft SQL Server\\MSSQL\\Data\\orderroom_data.mdf', SIZE=2MB,

MAXSIZE=5MB, FILEGROWTH=1MB )

LOG ON (NAME=\

FILENAME='F:\\Program Files\\Microsoft SQL Server\\MSSQL\\Data\\orderroom_log.ldf', SIZE=1MB,

MAXSIZE=5MB, FILEGROWTH=1MB )

2.创建三张表的SQL命令;

(1).创建customer表的SQL代码: useorderroom

create table customer (

customeridint not null primary key, customernamevarchar(20) not null, ageint,

sex char(2),

cardname char(8) not null, cardid char(20) not null, addressvarchar(30), telvarchar(20), notevarchar(100) )

(2).创建room表的SQL代码: useorderroom Create table room (roomidint not null, grade char(4), priceint, state char(4),

primary key(roomid) )

(3).创建orderdetail的SQL代码: useorderroom

create table orderdetail

(roomidint not null, customeridint not null, orderdatedatetime not null, dayssmallint not null, notevarchar(100),

primary key(roomid,customerid) )

3.三张表的表结构(屏幕截图):

customer表:

room表:

orderdetail表:

4.三张表中增加的10条记录(屏幕截图) (1)customer表:

(2)orderdetail表:

(3)room表:

6. 查询数据的SQL代码:

(1)查询customer表中的所有客户的信息以及仅查询客户姓名。 命令:

select * from customer

selectcustomername from customer 执行结果:

实验3 客房预订数据的简单和复杂查询

(2)查询出customer表中的前十个或前面10%的客户信息。 select top 10 * from customer

select top 10 percent * from customer

(3)查询预定天数为2的所有客户的姓名,年龄,证件名称,证件号码。 Select Customername,age,cardname,cardid

from customer join orderdetail on customer.customerid=orderdetail.customerid where days=2

Select Customername,age,cardname,cardid from customer

wherecustomerid in

(selectcustomerid from orderdetail where days=2)

(4)将room表中客房价格按90%价格显示,并且将价格列名改为‘调价后价格’。 select调价后价格=price*0.9 from room

(5)查询customer表中的客户信息,并以姓名、电话、地址作为列名。 Select Customername as 姓名,tel as 电话,address as 地址 from customer Select Customername姓名,tel电话, address 地址 from customer Select 姓名=Customername,电话=tel, 地址=address from customer

(6)查询room表,显示客房价格在150到200之间的客房信息。 Select * from room

Where price between 150 and 200

(7)从客房表中找出客房价格大于200的客房信息,并按照升序和降序显示。 Select * from room

Where price>200 order by price desc Select * from room

Where price>200 order by price asc

(8)找出姓王的所有客户。 Select * from customer

Where customername like '%王%'

(9)查询预订天数大于2天的所有客户的姓名,年龄,证件名称,证件号码和预订的房间号和房间等级。

Select customername,age,cardname,grade,room.roomid,customer.cardid From orderdetail join room on orderdetail.roomid=room.roomid Join customer on customer.customerid=orderdetail.customerid Where days>2

selectcustomername,age,cardname,grade,b.roomid,cardid from orderdetail a join room b on a.roomid=b.roomid join customer c on c.customerid=a.customerid where days>2

(10)查询客户地址在成都的客户所预定的房间号,房间价格和房间等级 Select room.roomid,price,grade

From orderdetail join room on orderdetail.roomid=room.roomid Join customer on customer.customerid=orderdetail.customerid Where address like '%成都%'

Select b.roomid,price,grade from orderdetail a join room b on a.roomid=b.roomid Join customer c on c.customerid=a.customerid Where address like '%成都%'

Select roomid,price,grade from room Where roomid in

(selectroomid from orderdetail Where customerid in

(selectcustomerid from customer Where address like '%成都%') )

2cfs88nz331x2cx44e8d
领取福利

微信扫码领取福利

微信扫码分享