Lassa
Lassa

Reputation: 131

Is it better to have separate tables for articles & comments, or one table for both?

I am working on a little project where a user submits an article to MySQL, and then PHP send the post to the screen. So far so good. Now i want to extend it to a "two level" post system, where people can comment on the articles. The case is, i dont know how to do that.

The table i use for storing articles:

TABLE: posts

id user date avatar signature post

Should i make a new row named comments in the posts table, or should i place the comments in a seperate table? one for articles, one for comments?

All help is much appreciated.

Upvotes: 1

Views: 746

Answers (2)

Tomas
Tomas

Reputation: 59585

Depends on how you use it on your website. You have to ask: "are my articles and comments essentially the same concept?" If yes, then use one table, if no, use two. On most websites articles work differently, can be categorized, editted etc., and usually need a different fields which would clutter the comments table... so in that case two tables are more reasonable. But if you conclude that on your website articles and comments are generally the same (but try to think future proof: wouldn't you need to add some article functionality in 2 months?) then you can think of articles also as of comments and have one table for them.

If you decide to merge things to one table, and you realize that you need another column to distinguish type of the post, and that some columns remain unused for some types, it is a clear warning signal you should have two tables!

Upvotes: 2

Chris Drappier
Chris Drappier

Reputation: 5370

This is a little subjective, but I would just set up a parent/child relationship on your posts table, make a parent_id column that is a foreign key for the id column in the same table. for extra credit, you can make it a nested set relationship which will allow you to pull the parent and all the children in one query

Upvotes: 1

Related Questions