javascript Facebook 的数据库结构来存储评论和帖子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19400622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:28:16  来源:igfitidea点击:

Database structure of Facebook to store comments and post?

javascriptphpmysqlfacebooksocial-networking

提问by Milan

I'm going to create simple social networking site. so... I want to know that how Facebook store comments for posts.

我将创建一个简单的社交网站。所以...我想知道 Facebook 如何存储帖子的评论。

is Facebook store Posts and its comments in same database table row, then how they store a long list of comments in a one table row field. or is it have each database table for each post, so that can make the comments for that post in that table row.

是 Facebook 在同一个数据库表行中存储帖子及其评论,然后他们如何在一个表行字段中存储一长串评论。或者它是否有每个帖子的每个数据库表,以便可以在该表行中对该帖子进行评论。

What is the best database structure for a simple social network site like CMS. but it could be handle a large amount of users.

对于像 CMS 这样的简单社交网站,最好的数据库结构是什么?但它可以处理大量用户。

Please help me in this matter. Thanks.

请帮助我解决这个问题。谢谢。

回答by Wayne Whitty

Small example of what you could do:

你可以做什么的小例子:

Table users:

用户

users
--------
id
name

Table comments:

评论

comments
--------
id
comment_text
posted_by
posted_to
date_posted

When user with ID 342 posts a comment to user 783's profile, you can store the comment like so:

当 ID 为 342 的用户对用户 783 的个人资料发表评论时,您可以像这样存储评论:

INSERT INTO comments(comment_text, posted_by, posted_to, date_posted) 
VALUES ('Hi!', 342, 783, '2013-10-16 11:17:00');

Then, when you're selecting comments to show on a user's profile, simply join the posted_by and posted_to columns up with the relevant ids in the users table.

然后,当您选择要在用户个人资料上显示的评论时,只需将posted_by 和posted_to 列与用户表中的相关ID 连接起来。

回答by loomie

I guess they have a table with all posts and the post_id. Then they have another table with all comments. And in the comments_table they have a row with the post_id so they can assign it.

我猜他们有一个包含所有帖子和 post_id 的表格。然后他们有另一个包含所有评论的表格。在comments_table 中,他们有一行带有post_id,因此他们可以分配它。

So,

所以,

every Post has a post_id with the status text, id of the user who wrote it etc. and every comment has a comment_id and a post_id to assign it to the post.

每个帖子都有一个带有状态文本的 post_id,写它的用户的 id 等等,每个评论都有一个 comment_id 和一个 post_id 来分配给帖子。

That's what I'm guessing. I can't tell you for sure though.

这就是我的猜测。不过我不能肯定地告诉你。

Simple example UPDATE

简单示例 更新

posts-table

帖子表

post_id | user_id | status_text | datetime

comments-table

评论表

comments_id | user_id | post_id | comments_text | datetime

回答by Kushal Suthar

i think for comment you try this table it table for video song thaty it collect a column with track id name

我想评论你试试这个表它的视频歌曲表它收集了一个带有轨道 ID 名称的列

              CREATE TABLE `tbl_video_comments` (                       
                  `comment_id` int(11) NOT NULL AUTO_INCREMENT,           
                  `user_name` varchar(20) DEFAULT NULL,                   
                  `track_id` int(11) NOT NULL,                            
                  `datetime` datetime NOT NULL,                           
                  `comment` varchar(100) NOT NULL,                        
                  PRIMARY KEY (`comment_id`)                              
                ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1  

回答by Santosh Upadhayay

All that you need is the relaeation and three table for now. Just keep the userid as primary key in the user table and make it foreign in every table you create. That will help you in getting every thing on basis of the userid.

您现在需要的只是关系和三个表。只需将 userid 作为主键保留在用户表中,并在您创建的每个表中将其设为外部。这将帮助您根据用户 ID 获取所有内容。