我的租房网设计与实现代码.docx
- 文档编号:3358355
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:28
- 大小:385.34KB
我的租房网设计与实现代码.docx
《我的租房网设计与实现代码.docx》由会员分享,可在线阅读,更多相关《我的租房网设计与实现代码.docx(28页珍藏版)》请在冰豆网上搜索。
我的租房网设计与实现代码
数据库技术与开发》
项目实训设计报告
项目名称:
《我的租房网》
姓名:
专业:
指导教师:
完成日期:
内蒙古科技大学信息工程学院计算机系
《数据库技术与应用》实验报告
姓名
班级
项目号、实验名称
学号I实验成绩
实验日期—
实训项目《我的租房网》
实验要求
1、完成实训项目《我的租房网》并完成实训一到实训4中的上机实践内容
2、按照项目实训报告相关要求,提交一份电子版项目实训报告
1、实训一:
建立数据库结构
⑴创建数据库House
使用SSMS向导创建数据库House
(2)建立5张数据表
--创建客户信息表sys_usercreatetablesys_user
(_
--客户编号,主键标识列
Useridintidentity(1,1)primarykey,
--客户姓名,非空
实
验
内
容
UserNamevarchar(50)notnull,
--客户密码,至少6个字符
UserPwdvarchar(50)constraintck_UserPwdcheck(len(UserPwd)>=6))_
--创建区县信息表hos_district
useHouse
go
createtablehos_district
(_
--区县编号,主键,标识列从1开始,递增值为1
DIdintidentity(1,1)primarykey,
--区县名称,非空
DNamevarchar(50)notnull
)
--创建街道信息表hos_street
useHouse
go
createtablehosstreet
(
--街道编号,主键,标识列从1开始,递增值为1
Streetldintidentity(1,1)primarykey,
--街道名称,非空
SNamevarchar(50)notnull,
--区县编号,表hos_district的外键
SDIdintconstraintfk_SDIdforeignkey(SDId)referenceshos_district(DId)
)_
--创建房屋信息表hos_type
useHouse
go
createtablehos_type
(_
--房屋类型编号,主键,标识列从1开始,递增值为1
HTIdintidentity(1,1)primarykey,
--房屋类型名称,非空
HTNamevarchar(50)notnull
)
--创建出租房屋信息表hos_house
useHouse
go
createtablehos_house
(_
--出租房屋编号,主键,标识列从1开始,递增值为1
HMIDintidentity(1,1)primarykey,
--客户编号,非空,外键
UserIdintnotnullconstraintfk_Userldforeignkey(UserId)referencessys_user(UserId),
--街道编号,非空,外键
StreetIDintnotnullconstraintfk_StreetIDforeignkey(StreetID)
referenceshos_street(StreetID),
--房屋类型编号,非空,外键
HTIdintnotnullconstraintfk_HTIdforeignkey(HTId)referenceshos_type(HTId),
--月租金,非空,默认值为0,要求大于等于0
Pricedecimal(6,2)notnulldefault(0)constraintck_Pricecheck(Price>=0),
--标题,非空
Topicvarchar(50)notnull,
--描述,非空
Contentsvarchar(100)notnull,
--发布时间,非空,默认值为当前日期,要求不大于当前日期
HTimedatetimenotnulldefault(getdate())constraintck_HTimecheck(HTime<=getdate()),
--备注
Copyvarchar(80)
L
(3)添加外键约束
--给客户信息表中的UserNam创建非聚集索引
createuniquenonclusteredindexldx_userName
onsys_user(UserName)with
fillfactor=10;
--给区县信息表中的DNam创建非聚集索引
createuniquenonclusteredindexIdx_dName
onhos_district(DName)with
fillfactor=10;
--给街道信息表中的SNam创建非聚集索引
createuniquenonclusteredindexIdx_sName
onhos_street(SName)with
fillfactor=10;
--给房屋信息表中的HTNam创建非聚集索引
createuniquenonclusteredindexIdx_htName
onhos_type(HTName)with
fillfactor=10;
分析过程:
给客户信息表、区县信息表、街道信息表、房屋信息表中添加非聚集索引来提高查询的速度,对经常使用的UserName、DName、SName、HTName进行查询优化
2、实训二:
添加测试数据
(1)主表添加测试数据
--向客户信息表sys_user添加多条条测试数据
insertintosys_user
values('王雪丽','100000'),
('严德赛','100001'),
('王生高','100002'),
('崔晓宇','100003'),
('卢一帆','100004'),
('张英武','100005'),
('安鹏','100006'),
('胖哥','100007'),
('程峰','100008'),
('马云','100009'),
('王铮','100010'),
('刘强东','100011'),
('雷舒然','100012'),
('成龙','100013'),
('武则天','100014'),
('焦旭鹏','100015'),
('郑利泽','100016'),
('罗阳光','100017'),
('('
邱国龙','100018'),
李小龙','100019')
--向区县信息表中添加多条记录
insertintohos_district
values('
洪山区'),
('
武昌区'),
('
青山区'),
('
江汉区'),
('
硚口区')
--向街道信息表中添加多条记录
insertintohos_street
values('
街道口','1'),
('
卓刀泉','1'),
('
广埠屯','1'),
('
石牌岭','1'),
('
积玉桥','2'),
('
杨家园','2'),
('
水果湖','2'),
('
黄鹤楼','2'),
('
红卫路','3'),
('
新沟桥','3'),
('
冶金街','3'),
('
厂前街道','3'),
('
吴家山','4'),
('
北湖街','4'),
('
满春街','4'),
('
新华街','4'),
('
六角亭','5'),
('
汉正街','5'),
('
汉中街','5'),
('
长风街','5')
--向房屋信息表中添加多条记录
insertintohos_typevalues('两室一厅'),
('两室两厅'),
('一室一厅'),
('三室两厅'),
('四室两厅'),
('五室两厅')
--建立三张临时表
createtable##topic
(
Topicvarchar(50)notnull,
)
createtable##contents
(Contentsvarchar(50)notnuII,)createtable##copy(
Copyvarchar(50)notnuII,)
--向三张临时表中插入数据insertinto##topicvalues('东方花园')insertinto##topicvalues('金茂东方公寓')insertinto##topicvalues('世贸大酒店')insertinto##topicvalues('民航小区')
insertinto##contentsvalues('全新家具电器')insertinto##contentsvalues('简单装修押一付三')insertinto##contentsvalues('精装修,首出租')insertinto##contentsvalues('豪华装修,拎包入住')
insertinto##copyvalues('环境优雅,学区房')insertinto##copyvalues('购物方便')insertinto##copyvalues('豪华小区,环境优美')insertinto##copyvalues('交通便利,配套完善')执行结果:
如图1、图2、图3、图4、图5
囲结果亦肖息
1
UseridUs
UeerPwd
100000
\1
i王雪丽
2
2
严德赛
IOCoot
3
5
王為
100002
4
4
100X13
E
C
卢TR
100004
张英壷
100005
7
7
10000S
S
3
胖寄
10000?
图1客户信息表
Did
Wanie
1
\1
洪山配
2
%・11・・“1IHHIlM
4
汇汉区
3
E
砖口区
4
3
青山区
6
2
武昌匡
图2区县信息表
5DIi
1
H
【街道口
„■
1
2
2
車刀泉
1
3
3
广埠屯
1
4
4
石牌龄
1
5
6
2
G
e
杨家园
2
7
7
水果朋
2
9
&
苒鹤瞿
2
图3街道信息表
xrid
KIN^jne
1
\Z
.两室葫J7
2
i
两室一厅
3
4
二室烦厅
4
&
四室曲JT
5
&
五室茁厅
6
3
—室一JT
图4房屋信息表
m结果消息
ToplC
1
东方优园!
Z
金茂东方生惠
3
世驛大曆店
4
民航小区
Contents
1
2
简单装修押一忖三
3
精装修.首出祖
4
慕华装悽拎包…
Copy
1:
£
环塩优腫■学扈番j
:
SI■―■■■:
—■!
■■■■lllbJ■■■£—■■lia—■■Iti
购物方傍
3
棗华小区,环…
4
交通便利,BS-
图5三张临时表
(2)添加批量数据
declare@begindatetime,@enddatetime
set@begin=getdate()
--定义局部变量
declare@topicvarchar(50)
declare@contentsvarchar(50)
declare@copyvarchar(50)
declare@useridint
declare@streetidint
declare@htidint
declare@pricedecimal(6,2)
declare@htimedatetime
--向hos_house表中插入10000条数据
--使用事物
begintransactiondeclare@iintset@i=0while@i<100begin
--对局部变量进行赋值
set@topic=(selecttop1*from##topicorderbynewid())
set@contents=(selecttop1*from##contentsorderbynewid())
set@copy=(selecttop1*from##copyorderbynewid())
selecttop1@userid=useridfromsys_userorderbyNEWID()
--租金在-4000之间随机产生一
set@price=1000+cast(3000*RAND()asint)
--发布时间@htime,要求小干当前系统时间,发布时间在当前系统时间一
年内
set
@htime=cast(dateadd(day,-cast(rand()*datepart(dayofyear,getdate
())asint),getdate())asdatetime)
set@streetid=(selecttop1StreetIdfromhos_streetorderby
newid())
set@htid=(selecttop1HTIdfromhos_typeorderbynewid())
--向hos_house中插入数据
insertintohos_housevalues(@userid,@streetid,@htid,@price,@topic,@contents,@htime,@copy)
set@i=@i+1
end
declare@recordcountint
select@recordcount=(selectcount(*)fromhos_house)
if@recordcount>1OOOOO
begin
rollbacktransaction
print'插入人数超过上限,插入失败’
end
else
begin
committransaction
print'插入成功’
end
set@end=getdate()
PRINTDATEDIFF(millisecond,@begin,@end)/1000.0--单位:
s
分析过程:
定义局部变量,对局部变量进行随机赋值,利用循环语句对hos_house表插入十万条语
句,运用事务对插入语句进行优化,缩短插入语句时间。
执行结果:
如图6
羽軀由覩1
Iidli
StrfEtU
KTId
Eri«
Topic
Coitats
1|im
2
Id
4
3553W
世贸大酹
囚1^-1E11:
35篇馬3
2113®
11
12
6
L5QL01
世稣一畦
輙勢-1G
仃:
臥輛脚
31皿
14
7
cw
ELBLDQ
翳僦首烧
2315-11-1C11;15.36懈
4LL3O1
$
汩
6
3945.DO
世贸翻宏
鹤匮首出租
11:
163«263
£1L2D6
18
16
5
IQSBDO
2D1E-0E-1E11N536283
电11205
4
15
叩
东方擁
231^-0^4口:
巧笳3^
?
1120?
e
百
目
391tDO
w园
2015-05-241J:
:
536530
B1L208
2
1
4
X79DJ
世贸划工
卿他馳…
Z31H)S-ie11:
3536350
011209
JI
E
3EWDO
榨翱-忙三
旳1日12211:
15湍353
玳誑湍学霑
10LL210
12
13
6
3437DO
ZJ15-01-2i11:
15363U
iiinii
1
ri
tniim
百半册知EB
nmrnrni-ii.1r>
图6hos_house表中插入的数据
3、实训三:
综合查询
(1)分页显示查询出租房屋信息
--建立临时表#t,用于存放查询的数据
createtable#t
(
HMIDintprimarykey.
Useridintnotnull,
StreetIDintnotnull,
HTIdintnotnull,
Pricedecimal(6,2)notnull,
Topicvarchar(50)notnull,
Contentsvarchar(100)notnull,
HTimedatetimenotnull,
Copyvarchar(80)
)
--用select-top分页方式查询数据,并将数据插入到临时表中
insertinto
#t(HMID,Userld,StreetlD,HTId,Price,Topic,Contents,HTime,Copy)selecttop10*fromhos_house
where(HMIDnotin(selecttop90HMIDfromhos_houseorderby
HMID))orderbyHMID-
--显示临时表中的数据
select*from#t
--查询临时表中第6-第10行数据
selecttop(5)*from#twhereHMIDnotin(selecttop(5)HMIDfrom#t)
--查询并改变所有列标题
selectHMIDas房屋编号,
UserIdas用户编号,
StreetIDas街道编号,
HTIdas房屋类型编号,
Priceas价格,
Topicas标题,
Contentsas房屋描述,
HTimeas发布时间,
Copyas备注,ROW_NUMBERVer(orderbyHMIDdesc)rankfromhos_house
分析过程:
建立临时表#t用于存放查询过程,用select-top分页方式查询数据,并将数据插入到临时表中,查询临时表中第6-第10行数据,查询并改变所有列标题。
执行结果:
如图7
Tstrld
Hi
Pric?
Cintents
Hint
1i哼
饰
1
&
26H.OO
11:
153
6727
21139?
2
£
OT.OO
訣黠器
西苗船卫11:
]£36727
31139?
1
5
&
33021OC
2016^6-141;:
]B:
3
删方便
111解
3
5
23哄
20:
5-J2-l£11']&3
6②
fift方便
51130C
L
4
3
2532.00
2Q1E-33-2611:
153
■?
33
瀚麒•学取
图7分页显示查询出租房屋信息
(2)查询指定客户发布的出租房屋信息
--使用内联接innerjoin查询实现
declare@begindatetime,@enddatetime
set@begin=getdate()
select
DName,SName,hos_type.HTName,Topic,Price,Contents,HTime,Copyfrom((((hos_houseinnerjoinsys_useronhos_house.Userld=sys_user.Userld)
innerjoinhos_streetonhos_house.StreetlD=hos_street.Streetld)
innerjoinhos_districtonhos_street.SDId=hos_district.DId)
innerjoinhos_typeonhos_house.HTId=hos_type.HTId)
wheresys_user.UserName='王雪丽'
set@end=getdate()
PRINTDATEDIFF(millisecond,@begin,@end)/1000.0--单位:
s
--建立临时表用where子句和内查询实现
declare@begindatetime,@enddatetime
set@begin=getdate()
createtable#n
(
DIdint,
DNamevarchar(50),
StreetIdint,
SNamevarchar(50),
SDIdint
)
insertinto#n(Dld,DName,Streetld,SName,SDId)select
Dld,DName,Streetld,SName,SDIdfromhos_district,hos_streetwherehos_district.Dld=hos_street.SDIdselect
DName,SName,hos_type.HTName,Topic,Price,Contents,HTime,Copyfromhos_house,hos_type,#n,sys_userwheresys_user.UserName='王雪丽'andhos_house.Userld=sys_user.Userldandhos_house.HTId=hos_type.HTIdandhos_house.StreetID=#n.StreetIdset@end=getdate()
PRINTDATEDIFF(millisecond,@begin,@end)/1000.0--单位:
s分析过程:
使用内联接innerjoin查询实现,建立临时表用where子句和内查询实现。
执行结果:
如图8、图9
J结果-J消息
-结果1龍
Dian*
KT临
To?
ic
Iricsi
Contents
Hrina
Copy
1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 租房 设计 实现 代码