【问题标题】:seat reservation system database design座位预订系统数据库设计
【发布时间】:2017-03-20 16:38:53
【问题描述】:

我正在设计一个用于巴士座位预订的数据库。我的问题是如何设计一个映射公共汽车座位的数据库?

例如,从 SRC 到 DEST 的路线有 10 辆公共汽车,每辆公共汽车有 50 个座位。每辆巴士都有唯一的巴士号码。 我如何设计可以将 50 个座位映射到每辆公共汽车的数据库,并显示该座位是否已预留? 乘客可以根据自己的舒适度选择任何巴士和任何座位号。

一种方法是使用列bus_id (int), seat_id (int), status (bool) 创建表。但是通过这种方法,每个 bus_id 的行数都等于公共汽车上可用的总座位数。请就这个问题向我提出建议。

【问题讨论】:

  • 为每辆巴士的每个座位创建一行仍然比为“巴士”表中的每个座位创建一列更好,因为您可以在不修改数据库架构的情况下管理巴士布局。 最佳 设计非常主观,但我认为 db 不需要关心未预订的座位 (status=false/NULL) - 这应该是应用程序级别的责任。

标签: database database-design


【解决方案1】:

有助于用半正式的语言表达业务领域。

我想你是在说这样的话:

系统有很多位置。

系统有很多路线。

一条路线结合了 2..n 个目的地。

一条路线有 1..n 个时间表。

一个时间表有 1..n 个班次。

一个出发地有 1 辆公共汽车。

一辆公共汽车有 50 个座位。

系统有 0..n 名乘客。

一位乘客有 0..n 个预订。

预订有 1 个出发地和 1 个座位。

这将为您提供以下架构:

Destination
---------
Destination_id

Route
-------
Route_id

Route_destination
-------------
Route_destination_id
Route_id
from_destination
to_destination
sequence

Departure
--------
Departure_id
Route_destination_id
Departure_time
Arrival_time

Bus
---
Bus_id
(seats if this could vary per bus)

Customer
------
Customer_id

Reservation
---------
Reservation_id
Departure_id
Customer_id
Date
Seat_no

如果您需要存储有关座位的其他数据(即它们不仅仅是从 1 到 50 的序列,但您需要存储位置,或者它们是否可供残疾人士使用,或者它是窗户还是isle seat)你需要引入一个额外的“bus_seat”表,类似于@sqlzim 的建议。

【讨论】:

    【解决方案2】:

    这只是一个例子,它的简单性试图与问题中提出的简单性相匹配。我相信根据实际的实施细节会有复杂的情况。

    如果通常只有少数预订,您可以选择仅在有预订时保留行,如下所示:

    create table bus_seats (
        bus_id int
      , seat_id int
      /*
      , additional_columns
      , handicap reservation only
      , which row
      , which side
      , is a window seat
      , seat reclines
      , extra_wide
      , distance from the restroom for calculating diffusion of odors over time
      , etc
      */
      );
    create table bus_seat_reservations (
        reservation_id int
      , bus_id int
      , seat_id int
      , route_id int
      , passenger_id int
      );
    

    查看所有巴士座位,并按路线预订:

    select 
        bs.bus_id
      , bs.seat_id
      , r.route_id
      , bsr.passenger_id as reserved_by
    from bus_seats bs
      inner join routes r
        on bs.bus_id = r.bus_id
      left join bus_seat_reservations bsr
        on bsr.bus_id   = bs.bus_id
       and bsr.seat_id  = bs.seat_id
       and bsr.route_id =  r.route_id
    

    查看预留座位:

    select 
        bsr.bus_id
      , bsr.seat_id
      , bsr.route_id
      , passenger_id
    from bus_seat_reservations bsr
    

    使用left join查看可用座位:

    select bs.bus_id
      , bs.seat_id
      , r.route_id
      , bsr.passenger_id as reserved_by
    from bus_seats bs
      inner join routes r
        on bs.bus_id = r.bus_id
      left join bus_seat_reservations bsr
        on bsr.bus_id   = bs.bus_id
       and bsr.seat_id  = bs.seat_id
       and bsr.route_id =  r.route_id
    where bsr.reservation_id is null
    

    使用not exists()查看可用座位:

    select bs.bus_id
      , bs.seat_id
      , r.route_id
    from bus_seats bs
      inner join routes r
        on bs.bus_id = r.bus_id
    where not exists (
      select 1
      from bus_seat_reservations bsr
      where bsr.bus_id   = bs.bus_id
        and bsr.seat_id  = bs.seat_id
        and bsr.route_id =  r.route_id
        )
    

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多