对于一个简单的消息系统,我应该为发送和接收的消息创建一个数据库表,还是只用一个表来处理这两个消息?
我想在我的 LAMP 站点上创建一个简单的消息系统。不是即时消息系统,而是异步消息系统,就像一种非常简单的电子邮件形式。
在数据库中,我应该:
A)为收件箱(收件人)创建一张表,为发送件(发件人)创建一张表,以便当收件人从收件箱中删除邮件时,发件人仍然可以在其已发送文件夹中看到它,
或
B)只创建一个表,然后添加一列来表明接收者/发送者是否已删除它,然后根据该列相应地向每个用户显示?
我还想跟踪接收者是否已阅读以及是否已回复。
选项 B 似乎更有效,因为您本质上并不复制表,但我想知道该方法是否存在我没有考虑到的潜在问题。
那么,您推荐哪个选项,A)、B) 还是其他选项,为什么?
I want to create a simple Messaging System on my LAMP site. Not an instant messaging System, but an asyncronous messaging system, like a very simple form of e-mail.
In the Database, should I:
A) create one table for the Inbox (receiver) and one table for Sent (sender), so that when a receiver deletes the message from their Inbox, the sender can still see it in their Sent folder,
or
B) create just one table, and then add a column for whether the Receiver/Sender has deleted it or not and then display it accordingly to each user based on that?
I would also like to keep track of whether the receiver has read it and whether they have replied to it or not.
Option B seems more efficient since you are not essentially duplicating a table, but I'm wondering if there are any potential issues with that method that I'm not thinking about.
So, which option do you recommend, A), B) or something else, and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会将其创建为单个表,其中包含以下内容:
将用户链接到消息的附加表将用于确定谁阅读了哪些内容(例如,发件人自动链接到该消息)。
本质上,如果用户已阅读消息,他们将在此表中拥有一个条目。
通过这种方式,我们可以为所有用户提供单一视图。某人的收件箱基本上是表中他们作为收件人的任何邮件,而发送箱是他们作为发件人的任何邮件。
我的收件箱:
我的发件箱:
我的未读消息:恕我直言,
这比尝试使用基本上执行相同操作的两个表要简单得多。您将在两个表中复制
From
和To
,但反过来,当我们只需要一份时,我们就会有 2 个消息副本在浮动。使用额外的MessagesRead
表可以让我们跟踪谁读了什么。I would create it as a single table with something like:
An additional table that linked users to messages would be used to determine who has read what (the sender automatically being linked to that message, for example).
Essentially, if a user has read a message, they'll have an entry in this table.
Doing it this way gives us a single view for all users. Someone's inbox is basically any message in the table where they are the recipient, and there sent box is any message where they are the sender.
My InBox:
My OutBox:
My Unread messages:
This is far simpler, imho than trying to use two tables that are basically doing the same thing. And you'd be replicating the
From
andTo
in both tables, but in reverse and we'd have 2 copies of the message floating around when we only needed one. Using an additionalMessagesRead
table allows us to track who has read what.很快我就想到有一个“消息”表,然后在消息和用户之间有一个链接表。这样,如果您愿意,您可以在稍后阶段随时拥有多个接收者 - 想想群聊。
可以删除此链接表输入(或删除标记)以防止用户在收件箱中看到它。
希望这是有道理的。
“链接表”是指有效提供消息和用户之间多对多关系的表
At a very quick first thought I would be tempted to have a "Message" table, and then have a link table between a message and user. That way, you can always have multiple receivers at a later stage if you want - think group chat.
This link table enter can be deleted (or delete flagged) to prevent the user seeing it in their inbox.
Hope that makes sense.
By "link table" I mean a table that effectively provides a many-to-many relationship between a message and a user
通常您有一个表、一个
sender_id
列和一个recipient_id
列。对于删除,您需要一个“软删除”功能,以便消息保留在其他人的消息中心,正如您提到的那样。为此,您可以有一个
deleted_messages
表,然后在message_id
、deleting_user_id
上有一个外键,并且您还可以存储其他信息,例如时间戳,以便您知道用户何时删除消息。然后,当您显示用户的收件箱(或已发送的邮件)时,您只需从messages
表中选择所有邮件,减去deleted_messages
表中出现的邮件即可。Usually you have one table, a
sender_id
column and arecipient_id
column.With deletion, you'd need a "soft delete" facility so the message remained in the other person's message centre as you mentioned. For this, you could have a
deleted_messages
table that then has a foreign key onmessage_id
,deleting_user_id
, and you can also store other information like a timestamp so you know when a user deleted a message. Then when you're displaying a user's inbox (or sent items), you'd just select all messages from yourmessages
table, minus any that appear in yourdeleted_messages
table.