如何将 'Attachment' table 绑定到 'PrivateMessage' 和 'Post' table

How to bind 'Attachment' table to 'PrivateMessage' and 'Post' tables

我有 3 tables PrivateMessagePostAttachment。简化结构如下:

PrivateMessage
--------------
 - ID (Primary Key)
 - PMContent

Post (means topic's/thread's post)
--------------
 - ID (Primary Key)
 - PostContent

Attachment
--------------
 - ID (Primary Key)
 - FileName
 - Bytes
 - ContentType

Post 必须是不同的 table,因此不允许将 PrivateMessagePost 合并为一个 table。 Attachment table 应存储有关用户来自私人消息和主题帖子的附件的信息。如何正确设计Attachmenttable?

下面这个正确吗?

Attachment
--------------
- ID (PK)
- PostID (FK and allow nulls)
- PrivateMessageID (FK and allow nulls)
- FileName
- Bytes
- ContentType

尽管您的方法可行,但我会选择以下解决方案之一:

  1. 创建链接表 PrivateMessageAttachmentPostAttachment,您可以在其中使用 FKs(id | private_message_id | attachment_id).
  2. 保留对主表的引用

这样您将拥有以下实体:PrivateMessagePostAttachmentPrivateMessageAttachmentPostAttachment

  1. 为两个实体创建单独的附件表,简单地通过一对多关系引用它们。

这样您将得到以下实体:PrivateMessagePostPrivateMessageAttachmentPostAttachment.