Search code examples
mysqlsqldatabasedatabase-designrdbms

How to design database for blog system?


I am developing a simple blog application in which users can write posts and others can like, comment, share etc. i am facing difficulty in designing database. What I have tried is:

user (user_id(pk), user_name)

post (post_id(pk), post_content, likes, user_id(fk))

comment(comment_id(pk), comment, post_id(fk))

PROBLEM: One user can have many posts and many users can like and comment on many posts. Whether I have to separate two tables like: user, post and userpost?

Can anyone tell me if anything wrong in this design?


Solution

  • Your comment table is missing field which indicates user who added this comment.

    And also it may be good idea to join posts and comments into one table - as comment is just a kind of post. So you will need just to store parent_id of post being commented in the record for comment.

    And likes should be in another one table for many-to-many relationship of users and posts they like.

    So your tables can look like:

    users (user_id(pk), user_name)
    
    posts (post_id(pk), parent_post_id(fk), date, post_content, user_id(fk))
    
    likes (like_id(pk), post_id(fk), user_id(fk))