這只是一個例子,其簡單性試圖與問題中提出的簡單性相匹配。我相信根據實際的實施細節會有複雜的情況。
如果有往往只有一些保留,你可以選擇只排在有像這樣的保留:
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
)
MySQL <> SQL Server,請編輯您的問題並刪除不必要/不正確的標記。這也最適合dba.stackexchange.com。 – scsimon
爲每個公共汽車的每個座位創建一個行比爲「總線」表中的每個座位創建一個列更好,因爲您可以在不修改數據庫架構的情況下管理總線佈局。 * best *設計非常主觀,但我認爲db不應該關心非預訂的席位(status = false/NULL) - 這應該是應用程序級別的責任。 – Filburt