Mysql structure for posts and comments

I read several manuals, documentation on mysql, db structures, and also I use them through php for several weeks. Now I have a problem, I do not know how to form / organize / create my db structure for posts and comments. I already read some posts about this (here in stackoverflow), but I did not find anything useful. I understand that I need to have 2 tables for messages and comments, and when I need to print them on the page using a foreign key (or ID), I will "combine" them (only on the page, not with SQL). When a person views a page, he usually sees the message and comments, but in the "background" everything is stored in 2 tables.

Do I need to add a new column every time someone adds a new comment or response?

If my question is correct, does this mean that if there are 100+ comments in the message, does that mean I need ALTER TABLE every time? This means that if post “A” has 3 comments and post “B” has 150 comments, will there be more than 100 columns in my “comments” table?

eg:

Messages | column1 | column2 | ... | columnN

A | bla1 | bla2 | bla3 | - empty | - empty | ... | - empty - |

B | bla1 | bal2 | bla3 | bla4 | bla5 | bla6 | ... | bla100 |

+1
source share
1 answer

"" . . - :

Post
--------
Id
Content
User
DatePosted

Comment
--------
Id
PostId
Content
User
DatePosted

, " " ( "--" ) , Pomments, .

( ), , , . , Id , , :

SELECT `Content`, `User`, `DatePosted` FROM `Post` WHERE `Id` = ?Id
SELECT `Id`, `Content`, `User`, `DatePosted` FROM `Comment` WHERE `PostId` = ?Id

, . , ( ), . , , , .. (, , . .)

+6

All Articles