views:

78

answers:

7

I am creating a simple blog app and so far I have 3 tables, Posts, Comments, and Authors.

I will just list the primary key for each table as well as a foreign key.

Posts will contain postid as a primary key.

Comments will contain commentid as a primary key and postid as a foreign key.

Posts has a 0 to many relationship with comments.

Authors will contain an authorid as a primary key.

Authors will have a many to many relationship with posts and a many to many relationship with comments.

The last statement is where I am having the most trouble. Does authors actually have a many to many relationship with posts and comments since or is it a one to one. If it is a many to many, I have heard that it is a good idea to have an in-between table describing the relationship between posts-authors and comments-authors, but I am not sure what keys I would use in this table?

If I verbally express it as an author can write many posts and many posts can be written by one author, I see it as a one to many, but if I view it from the actual data stored in the tables such as posts will contain multiple posts and authors will contain multiple authors, then it seems like a many to many, so is this what the cross-reference table is for, to remove duplicates.

+1  A: 

The cross-reference table in between would look something like this:

create table PostsToAuthors (
    postToAuthorsId int primary key,
    postId int,
    autorId int
);

Each association between an author and a post would require that a record is created in this table. Note that this table does not include the constraints (foreign key and unique specifically) that you would want on a table like this.

Andrew Hare
Would it be similar for the comments such as a CommentsToAuthors table?
Xaisoft
Exactly - you would want to use the same pattern there as well.
Andrew Hare
This schema is correct, assuming that you do have a true many-to-many relationship.
Nathan Long
+2  A: 

No it's just a one-to-many. A post or comment can only have one author - right?

So you would just need a foreign key AuthorID in the post and comments tables

DJ
Ok, I guess if I verbally express it as one author can write many posts and many posts can be written by one author, it makes sense that it is a one to many.
Xaisoft
+7  A: 

I would think Authors to posts/comments would be a 1-to-many relationship, unless you plan to support a situation where a post could have multiple authors. Maybe that makes sense, but seems unlikely many people would use that. I wouldn't bother complicating my schema for a feature that maybe 0.1% of my users would want.

Just put the authorId on the Posts and Comments table and you should be good to go.

Eric Petroelje
and the same applies for comments, you wouldn't want to allow 2 authors to publish the same comment, would you?
BlackTigerX
+1 agreed, just put a FK to the authors table in the post table.
SnOrfus
@BlackTigerX - yes, exactly. I could see a case for multiple post authors, but not for multiple comment authors.
Eric Petroelje
I will edit my post.
Xaisoft
+4  A: 

Unless you are running a peculiar kind of forum, surely the rules are

  • an Author can write many Posts
  • a Post must have one and only one Author
  • an Author can write many Comments
  • a Comment must have one and only one Author
APC
+1  A: 

I would add one more column to tables Posts and Comments to store foreign key of author. Usually, posts and comments have only one single author.

So your schema would look like this:

Posts
    postid - primary key
    authorid - foreign key
Comments
    commentid - priamry key
    postid - foreign key
    authorid - foreign key
Authors
    authorid - primary key

Authors-comments - one(zero) to many relationship
Authors-posts - one(zero) to many relationship
Posts-comments - one(zero) to many relationship
Lukasz Lysik
That was another question I had. Instead of authorid being on posts and comments, can postid and commentid be on the authors table or would that cause more problems?
Xaisoft
More natural is if the post and comment has a column authorid. When you buy a book, you have the author on the cover. None of authors has a list of book they have written in theirs pockets.
Lukasz Lysik
Ok, when you put it that way, it make more sense. Thanks again.
Xaisoft
+1  A: 

Authors will have a many to many relationship with posts and a many to many relationship with comments.

So you're saying that each author has many posts, and each post has many authors? And the same for comments?

If you're making a wiki, OK, but in most cases, a given post or comment would only have one author. Is that what you meant?

In any case, let's say there is an actual many-to-many relationship. You would create a relational table, for example, called authors_posts, and it would have the following columns: id, author_id, post_id. Each row indicates that "author X and post Y are related."

Nathan Long
Yes, I think the more I look at it, it is a 1 to many relationship.
Xaisoft
+2  A: 
If I verbally express it as an author can write many posts and many posts can be written by one author, I see it as a one to many, but if I view it from the actual data stored in the tables such as posts will contain multiple posts and authors will contain multiple authors, then it seems like a many to many, so is this what the cross-reference table is for, to remove duplicates.

I see where you're getting confused. "The posts table will contain multiple posts" and "the authors table will contain multiple authors" is true, but that's not the relationship that you're trying to express. (That's just the nature of tables - an authors table will have many rows, each of which represents a single author.)

The crucial thing is, for a given post, how many authors will it have? One, or many? For a given author, how many posts will it have? One, or many?

Nathan Long
Exactly, now you see why I was getting confused lol. If I look at it from the tables perspective, it looks one way, but if I look at it the correct way which is what you mentioned the crucial thing is, it looks another way. Thanks for understanding my confusion.
Xaisoft