Database structure of Facebook to store comments and post?

14,925

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.

Share:
14,925
Milan
Author by

Milan

Updated on June 11, 2022

Comments

  • Milan
    Milan almost 2 years

    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.

    • Sri
      Sri over 10 years
      "is facebook store Posts and its comments in same databse table row" it will never happen. they have deffrent tables for comments and post (i think)
  • Peter
    Peter about 5 years
    Shouldn't you be storing the post_id in the posted_to column?
  • Wayne Whitty
    Wayne Whitty about 5 years
    @Peter I did forget to add the date_posted column to my insert statement. posted_to should be the unique ID of the user profile that the comment is being published on.