If you want to set up a relationship you have to determine the sense of your relationship.
One to One
Each side of the relationship is related to one and only entity. For example, nowaday, you split up the users definition in two parts :
- the connection handler (password, last login date, activation)
- the profile info (username, email, favourite movie...)
One profile has only one handler and one handler has only one profile.
In that case each table has to be added with a foreign key that takes the identifier of each entity.
One to Many
The case that you have with your blog. One article has many comments, one comments has one and only one article.
So, in your comment table, you have to add your foreign key that is a link to your article.id.
Then to select the comment that are in one article, you have to make a WHERE :
SELECT * FROM comment_table WHERE id_article = 1 -- for your first article,
If you want to load info from your article table and your comment table, you have to use the JOIN command :
SELECT comment_table.*, article_table.* FROM article_table
JOIN comment_table ON article_table.id = comment_table.id_article
many to many
each entity can be associated to many of the other. For example, il you provide tags to your articles : each tag cas be assigned to many articles, each article can have many tags.
In this case, you have to create a new table which only have tag_id and article_id.
To select one article with his tags, you have to make a double join :
SELECT article.*, tags.* FROM article_table article
JOIN tags_in_article tia ON tia.id_article = article.id
JOIN tag_table tags ON tia.id_tag = tag.id
WHERE article.id = 1