为了练习MySQL,我正在用java创建一个数据库聊天程序。
到目前为止,我已经创建了3个表:
但是我想知道什么是最好和最有效的方式来存储聊天消息。我应该为每个聊天创建一个新的表(使用聊天日志表中的名称来标识它)还是有一个大表来存储所有聊天的所有消息,并将聊天名引用为外键。
发布于 2018-06-01 13:13:31
到目前为止,我们只考虑表ChatLog
和UserLog
。
ChatLog
chatid | chatname | chatpassword
-------+----------+-------------
1 | foo | bar
2 | lol | xD
UserLog
userid | username | userpassword
-------+------------+-------------
1 | chatbot | skynet
2 | john | doe
3 | chucknixon | thegreatone
对于消息日志,我的方法将是您编写的最后一个方法,它有一个带有外键的大表。
MessageLog
messageid | userid | chatid | messagecontent
----------+--------+--------+---------------
1 | 1 | 1 | john has joined the chat
2 | 1 | 1 | chucknixon has joined the chat
3 | 2 | 1 | hello chuck
4 | 1 | 2 | john has joined the chat
5 | 3 | 1 | Hi john how are you ?
然后,关系逻辑和结构结构将如下所示:
-a唯一用户可以编写0或n消息。
-a唯一消息可以由1编写,并且只有1用户可以编写。
-a唯一聊天室可以包含0或n消息
-a唯一的消息可以包含在1中,只有一个聊天室。
UserLog 0,n -> (写入) <- 1,1 MessageLog 1,1 -> (要包含) <- 0,n ChatLog
https://stackoverflow.com/questions/50643044
复制相似问题