ylbtech-SubwayNav(地铁线路导航)-数据库设计

ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计

DatabaseName:SubwayNav(地铁线路导航)

Type:线路导航

1.A,数据库关系图(Database Diagram)
1.B,数据库设计脚本(Database Design Script)

1.B.1,SubwayNav.sql 第一版

-- =============================================
-- ylb:地铁线路导航系统
-- author:YUANBO
-- pubdate:22:02 2013/3/31
-- =============================================
USE master
GO -- Drop the database if it already exists
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'SubwayNav'
)
DROP DATABASE SubwayNav
GO CREATE DATABASE SubwayNav
GO
use SubwayNav go
-- =============================================
-- ylb:1,线路图
-- =============================================
create table Line
(
lineId int primary key,
lineName varchar(100)
)
go
-- =============================================
-- ylb:2,站点表
-- =============================================
create table Station
(
stationId int primary key identity(1,1),
stationName varchar(100),
lineId int,
flagTransfer bit default(0) --是否为换乘车站 1:是;0:不是
)
go
-- =============================================
-- ylb:3,换乘站点表
-- =============================================
create table TransferSite
(
stationId int,
lineId int,
lineId2 int
) go
-- =============================================
-- ylb:3,算法思想
-- 1,先判断站点(起始点|目的地)是否存在。
-- 2,(存在)返回站点所在的线路
-- 3,若在同一条线上,则计算俩站相隔的站数。
-- 3.2,不再同一条线路,则找两站的交集(即换乘站点,相同的站点),
-- 3.3,交集一个,则分别计算交集点到出发和目的的站数,并相加在一起。
-- ============================================= --插入测试数据
--insert into Line(lineId,lineName) values(,'')
insert into Line(lineId,lineName) values(1,'1号线')
insert into Line(lineId,lineName) values(5,'5号线')
insert into Line(lineId,lineName) values(10,'10号线') --向1号线 插入测试站点数据
--insert into Station(stationName,lineId) values('',)
insert into Station(stationName,lineId) values('苹果园',1)
insert into Station(stationName,lineId) values('王府井',1)
insert into Station(stationName,lineId,flagTransfer) values('东单',1,1)
insert into Station(stationName,lineId,flagTransfer) values('国贸',1,1)
insert into Station(stationName,lineId) values('四惠东',1) --向5号线 插入测试站点数据
--insert into Station(stationName,lineId) values('',)
insert into Station(stationName,lineId) values('天通苑北',5)
insert into Station(stationName,lineId) values('慧新西街南口',5)
insert into Station(stationName,lineId) values('东四',5)
insert into Station(stationName,lineId,flagTransfer) values('东单',5,1)
insert into Station(stationName,lineId) values('宋家庄',5) --向10号线 插入测试站点数据
--insert into Station(stationName,lineId) values('',)
insert into Station(stationName,lineId) values('巴沟',10)
insert into Station(stationName,lineId) values('北土城',10)
insert into Station(stationName,lineId,flagTransfer) values('慧新西街南口',10,1)
insert into Station(stationName,lineId,flagTransfer) values('国贸',10,1)
insert into Station(stationName,lineId) values('劲松',10) --insert into TransferSite(stationId,lineId,lineId2) values(,,)
--insert into TransferSite(stationId,lineId,lineId2) values(1,5,3)
--insert into TransferSite(stationId,lineId,lineId2) values(1,10,14)
--insert into TransferSite(stationId,lineId,lineId2) values(5,10,) select * from Station

1.B.2,SubwayNav.sql 第二版【On】

-- =============================================
-- ylb:地铁线路导航系统
-- author:YUANBO
-- pubdate:22:02 2013/3/31
-- =============================================
USE master
GO -- Drop the database if it already exists
IF EXISTS (
SELECT name
FROM sysdatabases
WHERE name = N'SubwayNav'
)
DROP DATABASE SubwayNav
GO CREATE DATABASE SubwayNav
GO
use SubwayNav go
-- =============================================
-- ylb:1,线路图
-- =============================================
create table Line
(
lineId int primary key,
lineName varchar(100)
)
go
-- =============================================
-- ylb:2,站点表
-- =============================================
create table Station
(
stationId int primary key identity(1,1),
stationName varchar(100),
flagTransfer int default(0) --是否为换乘车站 1:是;0:不是
)
go -- =============================================
-- ylb:3,换乘站点表
-- =============================================
create table TransferSite
(
stationId int,
lineId int
) go
-- =============================================
-- ylb:4,线路详细列表
-- =============================================
create table LineDetail
(
stationId int,
lineId int,
orderNum int --顺序大小,以顺时针开始的起始站点
)
--drop table LineDetail go
-- =============================================
-- ylb:3,算法思想
-- 1,先判断站点(起始点|目的地)是否存在。
-- 2,(存在)返回站点所在的线路
-- 3,若在同一条线上,则计算俩站相隔的站数。
-- 3.2,不再同一条线路,则找两站的交集(即换乘站点,相同的站点),
-- 3.3,交集一个,则分别计算交集点到出发和目的的站数,并相加在一起。
-- ============================================= --插入测试数据
--insert into Line(lineId,lineName) values(,'')
insert into Line(lineId,lineName) values(1,'1号线')
insert into Line(lineId,lineName) values(5,'5号线')
insert into Line(lineId,lineName) values(10,'10号线') --向1号线 插入测试站点数据
--insert into Station(stationName) values('',)
insert into Station(stationName) values('苹果园')
insert into Station(stationName) values('王府井')
insert into Station(stationName,flagTransfer) values('东单',1)
insert into Station(stationName,flagTransfer) values('国贸',1)
insert into Station(stationName) values('四惠东') --向5号线 插入测试站点数据
--insert into Station(stationName) values('',)
insert into Station(stationName) values('天通苑北')
insert into Station(stationName,flagTransfer) values('慧新西街南口',1)
insert into Station(stationName) values('东四')
--insert into Station(stationName,flagTransfer) values('东单',1)
insert into Station(stationName) values('宋家庄') --向10号线 插入测试站点数据
--insert into Station(stationName) values('',)
insert into Station(stationName) values('巴沟')
insert into Station(stationName) values('北土城')
insert into Station(stationName) values('劲松') --insert into TransferSite(stationId,lineId,lineId2) values(,)
--向1号线
insert into TransferSite(stationId,lineId) values(1,1)
insert into TransferSite(stationId,lineId) values(2,1)
insert into TransferSite(stationId,lineId) values(3,1)
insert into TransferSite(stationId,lineId) values(4,1)
insert into TransferSite(stationId,lineId) values(5,1) --向5号线
insert into TransferSite(stationId,lineId) values(6,5)
insert into TransferSite(stationId,lineId) values(7,5)
insert into TransferSite(stationId,lineId) values(8,5)
insert into TransferSite(stationId,lineId) values(3,5)
insert into TransferSite(stationId,lineId) values(9,5) --向10号线
insert into TransferSite(stationId,lineId) values(10,10)
insert into TransferSite(stationId,lineId) values(11,10)
insert into TransferSite(stationId,lineId) values(7,10)
insert into TransferSite(stationId,lineId) values(4,10)
insert into TransferSite(stationId,lineId) values(12,10)
select * from Station go
--insert into LineDetail(stationId,lineId,orderNum) values(,,)
--向1号线
insert into LineDetail(stationId,lineId,orderNum) values(1,1,1)
insert into LineDetail(stationId,lineId,orderNum) values(2,1,2)
insert into LineDetail(stationId,lineId,orderNum) values(3,1,3)
insert into LineDetail(stationId,lineId,orderNum) values(4,1,4)
insert into LineDetail(stationId,lineId,orderNum) values(5,1,5) --向5号线
insert into LineDetail(stationId,lineId,orderNum) values(6,5,1)
insert into LineDetail(stationId,lineId,orderNum) values(7,5,2)
insert into LineDetail(stationId,lineId,orderNum) values(8,5,3)
insert into LineDetail(stationId,lineId,orderNum) values(3,5,4)
insert into LineDetail(stationId,lineId,orderNum) values(9,5,5) --向10号线
insert into LineDetail(stationId,lineId,orderNum) values(10,10,1)
insert into LineDetail(stationId,lineId,orderNum) values(11,10,2)
insert into LineDetail(stationId,lineId,orderNum) values(7,10,3)
insert into LineDetail(stationId,lineId,orderNum) values(4,10,4)
insert into LineDetail(stationId,lineId,orderNum) values(12,10,5) select * from Station s inner join LineDetail ld on s.stationId=ld.stationId
where lineId=1 order by orderNum asc select * from Station where stationId not in(select stationId from LineDetail where lineId=0)

1.B.3,SubwayNav-Searching.sql

use SubwayNav
go
go
-- =============================================
-- ylb-search:1,起始点和目的地在同一条线上
-- =============================================
go
--1,判断站点是否存在
--苹果园--》东单
--1.1.1
select * from Station where stationName='苹果园'
--retrun station=1
--1.1.2
select * from Station where stationName='%苹果园%'
--retrun station=3 --1.2
--统计俩站点的站点数量(除去本站点)
select * from LineDetail where stationId=1 and lineId=1
--retrun orderNum=1
select * from LineDetail where stationId=3 and lineId=1
--retrun orderNum=3 select count(*)'amount' from LineDetail where orderNum>1 and orderNum<=3 and lineId=1 select * from LineDetail where lineId=1 select stationId,count(stationId) from LineDetail group by stationId having count(stationId)>1 select stationId,stationName,flagTransfer from Station
1.C,功能实现代码(Function Implementation Code)

http://files.cnblogs.com/ylbtech/WebForm-SubwayNav.rar

ylbtech-SubwayNav(地铁线路导航)-数据库设计 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
上一篇:洛谷P2777 [AHOI2016初中组]自行车比赛


下一篇:面试题整理:SQL(一)