Database structure for comment system

Asked

Viewed 329 times

1

On my system (PHP), it is allowed to make posts, which are saved in a table with their respective information: author, time, content, etc. Now, I’m thinking about adding comments to the post, but I have no idea what the structure of the database would look like, if I would create a table for all the comments of the post or whatever, I’m just lost.

Someone to shed some light on that?

Thank you!

1 answer

4


Being as basic as possible.

Requirements

See for your own requirement:

  • You have 1 post (which consequently has a primary key, I will call id_post). Which briefly has the attributes id, text of the post.
  • You will get N comments on a post. The post will contain id_comment, id_post (Foreign key for Post) and comment.

Therefore, an association of 1 x N (A post has N comments, where N can be any number).

SQL

To identify the post:

SELECT * FROM postagem WHERE id_postagem = :numeroPostagem

To identify comments from that post:

SELECT * FROM comentario WHERE id_postagem = :numeroPostagem

Frequently asked questions

If I already have the id_post, why create an id for comment (id_comment) in the comment table ?

To be able to identify a comment from the other.

Why I have to take the post id (id_post) to the comment table ?

In order to identify which post that comment belongs to

  • I think I get the logic... Well, tomorrow I put in place and then put the results, I will certainly put as the best answer, but that was already a great help to open my mind. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.