数据库设计 (Oracle) - 建模业务逻辑

Database Design (Oracle) - modelling business logic

我面临以下困境

考虑下面的代码 - 这是我用来描述问题的东西 - 不是真正的实现,我省略了一些检查约束和 NOT NULL 以避免混淆图片。

CREATE TABLE Theater_Halls(
    thha_id NUMBER(2) CONSTRAINT pk_thha_id PRIMARY KEY,
    thha_name VARCHAR2(30) CONSTRAINT nn_thha_name NOT NULL,
    thha_no_of_seats NUMBER(4) CONSTRAINT ch_thha_no_of_seats CHECK (thha_no_of_seats > 0)
        CONSTRAINT nn_thh_no_of_seats NOT NULL
);     
CREATE TABLE Seats (
    seat_id NUMBER (8) CONSTRAINT pp_seat_id PRIMARY KEY,
    seat_no NUMBER (4) CONSTRAINT ch_seat_no CHECK (seat_no > 0),
    thha_id NUMBER(2) CONSTRAINT fk_seat_thha_id REFERENCES Theater_Halls(thha_id)
);


CREATE TABLE Events ( -- each event has a hall in theater associated with it
    evnt_id NUMBER(4) CONSTRAINT pk_evnt_id PRIMARY KEY,
    evnt_name VARCHAR2(30) CONSTRAINT nn_evnt_name NOT NULL,
    thha_id NUMBER(2) CONSTRAINT fk_evnt_thha_id REFERENCES Theater_Halls(thha_id),
    evnt_date TIMESTAMP CONSTRAINT nn_evnt_time NOT NULL
);

CREATE Table Users (
    user_id NUMBER(10) CONSTRAINT pk_user_id PRIMARY KEY
    );

CREATE TABLE Bookings (
    bkng_id NUMBER(10) CONSTRAINT pk_bkng_id PRIMARY KEY,
    evnt_id NUMBER(10) CONSTRAINT fk_bkng_evnt_id REFERENCES Events(evnt_id),
    seat_id NUMBER(8) CONSTRAINT fk_bkng_seat_id REFERENCES Seats(seat_id),
    user_id NUMBER(10) CONSTRAINT fk_bkng_user_id REFERENCES Users(user_id),
    bkng_price NUMBER(6,2) CONSTRAINT nn_bkng_price NOT NULL,
    CONSTRAINT un_evnt_seat_user UNIQUE (evnt_id, seat_id)
);

现在,这个实现在某种意义上满足了标准,因为它保存的数据没有(看起来)异常。

不过我有两个问题。

  1. 如果它保留在上面的表格中,最初创建时 user_id 为 NULL,一旦在应用程序中进行预订,user_id 将被填充并且程序将跟踪预订的座位( user_id <> 空)
  2. 或者创建中介 table 是更好的方法吗?

    CREATE TABLE Events_Seats (
    evse_id NUMBER(8) CONSTRAINT pk_evse_id PRIMARY KEY,
    evnt_id NUMBER(4) CONSTRAINT fk_evse_evnt_id REFERENCES Events(evnt_id),
    seat_id NUMBER(8) CONSTRAINT fk_seat_id REFERENCES Seats(seat_id),
    evse_price NUMBER(6,2) CONSTRAINT nn_evse_price NOT NULL,
    CONSTRAINT un_evnt_seat_user UNIQUE (evnt_id, seat_id));
    

    然后将外键链接到预订,其中预订 table 将是 'transactional' - 这意味着,当为给定事件选择座位时,将插入新行。就业务逻辑建模和潜在错误/无效数据而言,给定方法是否有任何优势?

我觉得你不需要EVENT_SEATStable。对于 userid,您甚至不需要用 NULL 填充 BOOKINGS。只需将 Bookings table 留空即可。 您可以使用以下方式查询可能的活动预订:

select e.EVNT_NAME as EventName, s.seat_no as AvailableSeat
from  events e
join seats s
on s.thha_id = e.thha_id
left join Bookings b
on b.seat_id = s.seat_id and
   b.evnt_id = e.thha_id
where  e.evnt_id = 1 and
   b.bkng_id is null

创建预订记录后,b.bkng_id is null 将从可能的预订列表中删除结果。