公交车路线查询系统后台数据库设计说明

上传人:人*** 文档编号:476892373 上传时间:2023-08-18 格式:DOC 页数:55 大小:567.50KB
返回 下载 相关 举报
公交车路线查询系统后台数据库设计说明_第1页
第1页 / 共55页
公交车路线查询系统后台数据库设计说明_第2页
第2页 / 共55页
公交车路线查询系统后台数据库设计说明_第3页
第3页 / 共55页
公交车路线查询系统后台数据库设计说明_第4页
第4页 / 共55页
公交车路线查询系统后台数据库设计说明_第5页
第5页 / 共55页
点击查看更多>>
资源描述

《公交车路线查询系统后台数据库设计说明》由会员分享,可在线阅读,更多相关《公交车路线查询系统后台数据库设计说明(55页珍藏版)》请在金锄头文库上搜索。

1、公交车路线查询系统后台数据库设计-查询算法1. 公交车路线信息在数据库中的存储方式显然,如果在数据库中简单的使用表 bus_route(路线名,路线经过的站点,费用)来保存公交 车路线的线路信息,则很难使用查询语句实现乘车线路查询, 因此,应该对线路的信息进行 处理后再保存到数据库中,笔者使用的方法是用 站点-路线关系表stop_route(站点,路线名,站点在路线中的位置)来存储公交车路线,例如,如果有以下 3条路线R1 : S1-S2-S3-S4-S5R2 : S6-S7-S2-S8R3 : S8-S9-S10则对应的站点-路线关系表stop_route 为StopRoutePositi

2、onS1R11S2R12S3R13S4R14S5R15S6R21S7R22S2R23S8R24S8R31S9R32S10R33注:Stop为站点名,Route为路线名,Position为站点在路线中的位置2. 直达乘车路线查询算法基于表stop_route 可以很方便实现直达乘车路线的查询,以下是用于查询直达乘车路线的存储过程InquiryTO :create proc In quiryTO(StartStop varchar(32),E ndStop varchar(32)asbeg inselectsr1.Stop as 启始站点,sr2.Stop as 目 的站点,sr1.Route a

3、s 乘坐线路,fromstop_route sr1,stop_route sr2wheresr1.Route=sr2.Routeand srl.Positionsr2.Positionand sr1.Stop=StartStopand sr2.Stop=E ndStopend3. 查询换乘路线算法(1)直达路线视图直达路线视图 可以理解为一张存储了所有直达路线的表(如果两个站点之间存在直达路线,那么在直达路线视图中就有一行与之相对应)。例如R1,R2,R3 对应的RouteTO如下:起点终占 匕八、乘坐路线站点数S3S4R11S3S5R12S4S5R11S1S2R11S1S3R12S1S4R1

4、3S1S5R14S2S3R11S2S4R12S2S5R13S2S8R21S6S2R22S6S7R21S6S8R23S7S2R21S7S8R22S8S10R32S8S9R31S9S10R31RouteTO定义如下:create view RouteTOasselectsrl.Stop as StartStop,-启始站点sr2.Stop as EndStop,-目的站点srl.Route as Route,-乘坐线路sr2.Positio n-sr1.Position as StopCou nt-经过的站点数fromstop_route sr1,stop_route sr2wheresr1.Ro

5、ute=sr2.Routeand sr1.Positionsr2.Position(2)换乘路线算法显然,一条换乘路线由若干段直达路线组成(每段路线的终点与下一段路线的起点相同),因此,基于直达路线视图RouteTO可以很方便实现换乘查询,以下是实现一次换乘查询的存储过程 InquiryTI :create proc In quiryT1(StartStop varchar(32),E ndStop varchar(32)asbeg inselectr1.StartStop as 启始站点,r1.Route as乘坐路线1,r1.EndStop as中转站点,r2.Route as乘坐路线2,

6、r2.EndStop as目 的站点,r1.StopCou nt+r2.StopCou nt as总站点数fromRouteT0 r1,RouteT0 r2wherer1.StartStop=StartStopand r1.E ndStop=r2.StartStopand r2.E ndStop=E ndStopend同理可以得到二次换乘的查询语句create proc In quiryT2(StartStop varchar(32),E ndStop varchar(32) asbeg inselectr1.StartStop as 启始站点,r1.Route as乘坐路线1,r1.E nd

7、Stop as 中转站点 1,r2.Route as乘坐路线2,r2.EndStop as 中转站点 2,r3.Route as乘坐路线3,总站点数r3.EndStop as 目 的站点,r1.StopCou nt+r2.StopCou nt+r3.StopCou nt as fromRouteT0 r1,RouteT0 r2,RouteT0 r3wherer1.StartStop=StartStopand r1.E ndStop=r2.StartStopand r2.E ndStop=r3.StartStopand r3.E ndStop=E ndStopend4. 测试execIn qui

8、ryTOS1 ,S2execIn quiryT1S1 ,S8execIn quiryT2S1 ,S9运行结果:启始站点目的站点 乘坐线路 经辻的站点数LSI32R11启始站点乘坐路线1中转站点】乘尘牆线2 目的站点总站点数ISIR15222S32启始站点乘坐路线1中转站点1乘坐路线2中辂站点2乘坐略线W目的站点总站点数LSIR1S2R2S8時S931公交车路线查询系统后台数据库设计-关联地名和站点在公交车路线查询系统后台数据库设计一一查询算法一文中,已经实现了查询站点到站点的路线查询算法,但是,现实中用户不一定使用站点进行 查询,而是使用地名。因此,公交车查询系统数据库必需记录地名与站点的 对

9、应关系,在查询时将地名映射为站点。根据实际情况,某一地点附近通常 有几个站点,因此,地名与站点之间是多对多的关系。显然,只需创建一个 地名站点关系表stop_spot(Stop,Spot)用于储存这个关系即可。数据库关系 图如下:SpotPKNameRemarkKoutePKNitmeRcmurkroutePKPK.FK1PKJK2PqsiitkinSt叩PKiimeRemarkSCop_spotPK.FKl PKTK2注:Route :路线表Stop :站点表Spot :地名表stop_route : 路线-站点关系表stop_spot :地名-站点关系表1.路线和地名信息维护:以下函数用于

10、维护公交车路线和地名的相关信息字符串分割函数(信息处理及路线查询的存储过程都需要使用到该函数):/*函数功能:将String以SplitChar为分隔点分割为字符串数组,结果保留在表变量中例如 SplitString(A/B,/) 返回表:Value vin dexA1B2*/CREATEfun ctio n SplitStri ng(Stri ng varchar(2048),SplitChar char)returns res table(Value varchar(128),vin dex int)asbeg indeclare in dex in t, unit varchar(128

11、),i next in t,le n in t,i intset in dex=1set i=1set le n=len( Stri ng)while in dex in dex beg insetun it=ltrim(rtrim(substri ng(Stri ng,i ndex,i next-i ndex)if un itbeg inin sert into res (value, vin dex) values (un it,i)set i=i+1endendset in dex=in ext+1endreturnend插入新的公车路线:/*插入新的公交车路线Route:路线名Stop

12、s:公交车经过的所有站点,站点用-隔开*/CREATE proc In sertRoute(Route varchar(32),Stops_Strvarchar(1024)as beg indeclare stops table( name varchar(32),positi on int)insert stops( name,positi on)select Value,vl ndex from dbo.SplitStri ng(Stops_Str,-)begi n tran t1save tran sp1-插入路线信息in sert into Route (n ame) values (

13、Route)if(error0)begi nrollback tran sp1commit tran t1raiserror(插入路线时发生错误,16,1)returnend-插入不存在的站点in sert Stop( name)select dist inct n ame from stops ss where n ame not in (select n ame from Stop)if(error0)begi nrollback tran sp1 commit tran t1raiserror(插入路线时发生错误,16,1)returnendin sert stop_route(Stop,Route,Positi on)select ss.n ame,Route,ss.positi on from stops ss if(error0)beg inrol

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 活动策划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号