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

时间:2016-06-02 20:22:46

标签: database oracle database-design 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<> NULL)
  2. 或者它是创建中间表的更好方法,例如

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

    然后将一个外键与预订相关联,其中预订表将是“事务性的”。 - 意思是,当为某个特定事件选择一个坐标时,将插入新行。就建模业务逻辑和潜在错误/无效数据而言,在给定方法中是否有任何优势?

1 个答案:

答案 0 :(得分:0)

我认为你不需要EVENT_SEATS表。对于BOOKINGS,您甚至不需要填充NULL userid Bookings。只需将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

当创建预订记录时,#include <regex> #include <string> #include <iostream> int main() { std::string str = "An R&D string with one more R&amp;D here and S&D or T&engrave; Some More T&D and &amp;&engrave; and R&&D"; std::cout << str << '\n'; // match HTML style entities aka &amp; std::regex e(R"(&\S+;)"); // iterate through matches (0) and non-matches (-1) std::sregex_token_iterator itr(str.begin(), str.end(), e, {-1, 0}); std::sregex_token_iterator end; for(; itr != end; ++itr) { std::string s = *itr; // replace the "&" in non-matching portions of the string if(!std::regex_match(s, e)) s = std::regex_replace(s, std::regex("&"), "&amp;"); std::cout << s; } } 将删除可能的预订列表中的结果。

相关问题