邮件系统数据库结构,需要帮助

发布于 2024-09-06 09:27:30 字数 958 浏览 3 评论 0原文

我有一个系统,用户(发送者)可以给朋友(接收者)写一条便条,接收者数量> = 0。消息文本保存在数据库中,发送者和所有接收者都可以看到,然后他们登录系统。发送者可以随时添加更多接收者。更重要的是,任何接收者都可以编辑消息,甚至将其从数据库中删除。对于这个系统,我很快创建了 3 个表:

用户(用户 ID、用户名、密码)
消息(消息ID,文本)
列表(ID,发送者ID,接收者ID,消息ID)

每行对应于发送者-接收者对,例如

发送者_x_ID -- 接收者_1_ID -- 消息_1_ID
发送者_x_ID -- 接收者_2_ID -- message_1_ID
发送者_x_ID -- 接收者_3_ID -- 消息_1_ID

现在的问题是:
1.如果用户从表“messages”中删除消息,如何自动删除表“list”中与已删除消息相对应的所有行。我必须包含一些外键吗?

更重要的是:
2. 如果发送者为他的消息 1 指定了 3 个接收者(用户名 1、用户名 2 和用户名 3),并且在某个时刻决定添加用户名 4 和用户名 5,同时从接收者列表中排除用户名 1。 PHP代码将获取新的接收者列表(用户名2,用户名3,用户名4,用户名5)这意味着插入到表“列表”

发送者_x_ID -- 接收者_4_ID -- message_1_ID
发送者_x_ID -- 接收者_5_ID -- 消息_1_ID

并从表“list”中删除与 user1 对应的行(不再在列表或接收者中)

发送者_x_ID -- 接收者_1_ID -- 消息_1_ID

从 PHP 发送哪个 SQL 查询以使其以简单而智能的方式发送?请帮忙! sql查询的例子就完美了!

i have a system there user(sender) can write a note to friends(receivers), number of receivers>=0. Text of the message is saved in DB and visible to sender and all receivers then they login to system. Sender can add more receivers at any time. More over any of receivers can edit the message and even remove it from DB. For this system i created 3 tables, shortly:

users(userID, username, password)
messages(messageID, text)
list(id, senderID, receiverID, messageID)

in table "list" each row corresponds to pair sender-receiver, like

sender_x_ID -- receiver_1_ID -- message_1_ID
sender_x_ID -- receiver_2_ID -- message_1_ID
sender_x_ID -- receiver_3_ID -- message_1_ID

Now the problem is:
1. if user deletes the message from table "messages" how to automatically delete all rows from table "list" which correspond to deleted message. Do i have to include some foreign keys?

More important:
2. if sender has let say 3 receivers for his message1 (username1, username2 and username3) and at certain moment decides to add username4 and username5 and at the same time exclude username1 from the list of receivers. PHP code will get the new list of receivers (username2, username3, username4, username5) That means insert to table "list"

sender_x_ID -- receiver_4_ID -- message_1_ID
sender_x_ID -- receiver_5_ID -- message_1_ID

and also delete from table "list" the row corresponding to user1 (which is not in the list or receivers any more)

sender_x_ID -- receiver_1_ID -- message_1_ID

which sql query to send from PHP to make it in an easy and intelligent way? Please help! Examples of sql queries would be perfect!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素染倾城色 2024-09-13 09:27:30

你的第一个问题很简单。您可以通过在 list 上设置外键来做到这一点。您可以将其设置为CASCADE更新和删除(以便DELETE FROM messages WHERE messageID = 5会自动删除list中的所有行,其中messageID = 5 您还需要使用 InnoDB 作为存储引擎才能实现这一点...

对于第二个问题,这也很简单,只需插入即可。一个查询中的新行:

INSERT INTO list (senderID, receiverID, messageID)
VALUES (sender_x_id, receiver_4_id, message_1_id),
       (sender_x_id, receiver_5_id, message_1_id);

然后删除另一个查询中的其他行:

DELETE FROM list
WHERE receiverID = receiver_1_id
  AND messageID = message_1_id

Your first problem is easy. You could do it by setting a foreign key on list. You'd set it up to CASCADE updates and deletes (so that DELETE FROM messages WHERE messageID = 5 would automatically delete all rows in list where messageID = 5 as well. You'd need to use InnoDB as the storage engine for this to work...

For the second problem, that's easy as well. Just insert the new rows in one query:

INSERT INTO list (senderID, receiverID, messageID)
VALUES (sender_x_id, receiver_4_id, message_1_id),
       (sender_x_id, receiver_5_id, message_1_id);

And then remove the others in another:

DELETE FROM list
WHERE receiverID = receiver_1_id
  AND messageID = message_1_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文