对这些实体之间的关系建模的最有效方法是什么?

What is the most efficient way to model the relationship between these entities?

我有一个包含如下实体的数据库:

1. User entity
2. Event entity (musical concert etc.)
3. Ticket entity
3. Notification entity

我一直在思考三种可能的解决方案:

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;

这样,Notification 实体同时持有两个外键,但一次只填充其中一个(eventId 或 ticketId),另一个永远为空。

b) 通知实体只有与通知本身相关的列,它不包含任何外键(userId 除外)。
将关系提取到另外两张这种结构的关系映射表(Notification-Ticket关系,Notification-Event同理,外键引用事件除外):

id serial PRIMARY KEY,
notificationId integer REFERENCES notifications(id),
ticketId integer REFERENCES tickets(id));

这样,我们创建了类似接口的东西,并且不让 Notification 实体知道任何关于关系的信息(它只有与通知本身和 userId 相关的属性),并且我们有两个额外的表来映射关系。

c) 将 Notification 实体分成两个不同的实体
(TicketNotification,EventNotification),它们每个都具有相同的属性,但外键列不同。

- TicketNotification - foreign key references ticketId
- EventNotification - foreign key references eventId

这样,我们就有了两个具有相同属性的表,只是在一列中有所不同,这对我来说似乎不是很干燥。

如果能提供任何形式的帮助和可能的解决方案,我将不胜感激,我可能完全偏离了方向,从不好的角度看待它。谢谢

你不知道的是这个。您声明的谓词是:

  • 每个通知都与{事件|之一相关门票}

这需要一个 Exclusive Subtype 集群。当然,我们不想要 Nullable Foreign Keys,后果是可怕的。这是正确的解决方案。

参考

请检查这些答案以从概念上理解问题和解决方案:

  • How can I relate a primary key field to multiple tables?

关系数据模型

注意 • 表示法

  • 我所有的数据模型都在 IDEF1X 中呈现,这是自 1993 年以来的关系数据库建模标准。

  • 我的IDEF1X Introduction是初学者必读的

  • 参考Subtype了解和实现子类型

  • 的全部细节