Database 座位预订系统数据库设计
我正在为公共汽车座位预订设计一个数据库。我的问题是如何设计一个数据库来映射公交车的座位 例如,从路线SRC到目的地有10辆公共汽车,每辆公共汽车有50个座位。每辆公共汽车都有唯一的公共汽车号码。 我该如何设计一个数据库,将50个座位映射到每辆公交车上,并显示座位是否已预订? 乘客可以根据自己的舒适程度选择任意公交车和任意座位号Database 座位预订系统数据库设计,database,database-design,Database,Database Design,我正在为公共汽车座位预订设计一个数据库。我的问题是如何设计一个数据库来映射公交车的座位 例如,从路线SRC到目的地有10辆公共汽车,每辆公共汽车有50个座位。每辆公共汽车都有唯一的公共汽车号码。 我该如何设计一个数据库,将50个座位映射到每辆公交车上,并显示座位是否已预订? 乘客可以根据自己的舒适程度选择任意公交车和任意座位号 一种方法是用列bus\u id(int)、seat\u id(int)、status(bool)创建表。但通过这种方法,每个公交id的行数将等于公交车上可用的总座位数。请
一种方法是用列
bus\u id(int)、seat\u id(int)、status(bool)
创建表。但通过这种方法,每个公交id的行数将等于公交车上可用的总座位数。请就这个问题向我提出建议。这只是一个例子,它的简单性试图与问题中提出的简单性相匹配。我相信,根据实际的实施细节,会有一些复杂的情况
如果通常只有几个预订,您可以选择在有如下预订时只保留行:
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
使用左连接查看可用座椅:
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
使用不存在()
查看可用座椅:
它有助于用半正式语言表达业务领域 我想你说的是: 该系统有许多位置 这个系统有很多路线 一条路线包含2..n个目的地 一条路线有1..n个计划 一个日程表有1..n个出发点 一个出发点有一辆公共汽车 公共汽车有50个座位 系统有0..n个乘客 乘客有0..n个预订 预订有一个出发点和一个座位 这将为您提供一个大致如下的模式:
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的序列,还需要存储位置,或者残疾人是否可以访问座椅,或者座椅是否为窗口或岛上座椅),则需要引入附加的“巴士座椅”表类似于@sqlzim的建议。为每个总线中的每个座位创建一行仍然比为“总线”表中的每个座位创建一列要好,因为您可以在不修改db模式的情况下管理总线布局。最好的设计是非常主观的,但我认为db不需要关心未预订的座位(状态=false/NULL)-这应该是应用程序级别的责任。
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