Database structure for comments system

1

In 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 of adding the option to add comments to the post, but I have no idea how the database structure would look, whether it would create a table for all comments in the post or I know, I'm just lost .

Someone to shed light on this?

Thanks!

    
asked by anonymous 23.04.2015 / 00:24

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'll call it the id_post). Which briefly has the id attributes, post text.
  • You'll get N comments on a post . The post will contain com_index, post_id (foreign key for Posting), and comment.

Therefore, a 1 x N association (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 the comments for that post:

SELECT * FROM comentario WHERE id_postagem = :numeroPostagem

Frequently Asked Questions

If I already have the id_post, why create a comment id (comment_id) in the comment table?

So you can identify one comment from the other.

Why do I have to take the post id (temp_id) to the comments table?

To identify which post belongs that comment

    
23.04.2015 / 01:04