开发铁路查询系统,如何建立列车,车站和站点的模型?

时间:2011-11-25 18:58:35

标签: mysql database database-design model

例如,如果我有两列火车,X和Y,他们旅行:

火车......... STATIONS

  • Train-X:经过Station-A;站-B;站-C;站-d
  • Train-Y:经过Station-B;站-X;站-d;站-Y

我如何将这些信息放入数据库中,以便乘客询问哪些列车从Station-B开始?哪些列车以Station-D结束?那么Train-X和Train-Y都会出现在结果中。

2 个答案:

答案 0 :(得分:1)

我想说你需要有三张桌子来完成这项工作。

电台:电台ID,姓名等

服务:服务ID,运营商,火车车厢数量等等。

Service_Stop :服务ID,停止号码,电台ID。

然后,您可以使用类似以下的查询找到在Station-B和随后在Station-D停止的服务:

SELECT
    Service_ID
FROM
    Station AS Start_Station
    JOIN Service_Stop AS Start_Stop ON
        Start_Station.Station_ID = Start_Stop.Station_ID
    JOIN Service_Stop AS End_Stop ON
        Start_Stop.Service_ID = End_Stop.Service_ID AND
        Start_Stop.Stop_Number < End_Stop.Stop_Number
    JOIN Station AS End_Station ON
        End_Stop.Station_ID = End_Station.Station_ID AND
        End_Station.Name = "Station-D"
WHERE
    Start_Station.Name = 'Station-B'

答案 1 :(得分:1)

我会使用五个表:。

Train: train_id, name   # e.g "The UnionT522"
Station: station_id, name # e.g. "Eggles Place"
Route: route_id, route_name # e.g. "Afternoon special at 4pm" 
RouteStation: route_station_id, route_id, station_id, route_order, begin_flag, end_flag
TrainRoute: train_route_id, train_id, route_id  # e.g. which train (above) is going on which route (above).

所以RouteStation会对给定路线的某个站点的事情是开始还是结束 TrainRoute将获得有关火车正在走哪条路线的信息。