我有一个包含实体的数据库,如下所示:
1. User entity
2. Event entity (musical concert etc.)
3. Ticket entity
3. Notification entity
Run Code Online (Sandbox Code Playgroud)
我一直在思考三种可能的解决方案:
a) 通知实体具有以下结构:
id serial PRIMARY KEY,
.
.
ticketId integer REFERENCES tickets(id),
eventId integer REFERENCES events(id))
userId integer REFERENCES users(id) // this is present in all three solutions;
Run Code Online (Sandbox Code Playgroud)
这样,Notification 实体就拥有两个外键,但一次只填充其中一个(eventId 或 TicketId),另一个永远为空。
b) 通知实体仅具有与通知本身相关的列,它不包含任何外键(userId 除外)。
关系被提取到另外两个具有此结构的关系映射表中(对于通知-工单关系,同样适用于通知-事件,除了外键引用事件):
id serial PRIMARY KEY,
notificationId integer REFERENCES notifications(id),
ticketId integer REFERENCES tickets(id));
Run Code Online (Sandbox Code Playgroud)
这样,我们创建了类似接口的东西,并且不让通知实体知道任何有关关系的信息(它只有与通知本身和 userId 相关的属性),并且我们还有两个映射关系的附加表。
c) 将Notification实体分成两个不同的实体
(TicketNotification、EventNotification),每个实体具有相同的属性,但外键列不同。
- TicketNotification - …Run Code Online (Sandbox Code Playgroud)