Database structure of Facebook to store comments and post?

Milan picture Milan · Oct 16, 2013 · Viewed 13.7k times · Source

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

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.

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

Please help me in this matter. Thanks.

Answer

Wayne Whitty picture Wayne Whitty · Oct 16, 2013

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:

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.