2017-03-20 187 views
2

我正在设计一个公交车座位预约数据库。我的问题是如何设计一个映射巴士座位的数据库?座位预约系统数据库设计

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

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

+2

MySQL <> SQL Server,请编辑您的问题并删除不必要/不正确的标记。这也最适合dba.stackexchange.com。 – scsimon

+1

为每个公共汽车的每个座位创建一个行比为“总线”表中的每个座位创建一个列更好,因为您可以在不修改数据库架构的情况下管理总线布局。 * best *设计非常主观,但我认为db不应该关心非预订的席位(status = false/NULL) - 这应该是应用程序级别的责任。 – Filburt

回答

3

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

如果有往往只有一些保留,你可以选择只排在有像这样的保留:

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 

请参阅使用可用座位:使用not exists()

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 

查看可用座位:

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 
    ) 
2

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

我想你说的是这样的:

该系统具有许多位置。

系统有很多路由。

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

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

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

出发有1辆巴士。

巴士有50个座位。

该系统有0..n乘客。

一位乘客有0..n保留。

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

这将使你的线沿线的一个模式:如果你需要存储关于座位的附加数据(即他们不只是一个从1到50的序列

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 

,但你需要存储位置,或者它们是否可供残疾人使用,或者是否是窗口或小岛座位),则需要引入类似于@ sqlzim建议的额外“bus_seat”表。