Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorMKayHavoc
    • CommentTimeApr 11th 2007 edited
     permalink
    Hi all,

    Which works best? Have two tables with a link table for the ID's, for example:

    BID - date - blog
    1 - 11/04/07 - First Blog

    CID - date - comment
    1 - 11/04/07 - First Blog

    BID - CID
    1 - 1

    Or just attaching the blog ID to each comment?

    BID - date - blog
    1 - 11/04/07 - First Blog

    CID - BID - date - comment
    1 - 1 - 11/04/07 - First Blog

    Cheers
    • CommentAuthorvarland
    • CommentTimeApr 11th 2007
     permalink

    I'm assuming a normal blog here, where each comment must relate to one and only one blog (article?). In that case, the second setup (where you use the article ID as a foreign key) is far superior.

    Think about what it would take to find all comments for a given article for each method. Let's say you're working with the article whose ID is 1. In the first scenario you would have:

    SELECT * FROM `comments` WHERE `id` IN (SELECT `cid` FROM `bid_cid` WHERE `bid` = 1);

    In the second scenario you would have:

    SELECT * FROM `comments` WHERE `bid` = 1;

    The second is much simpler, much more efficient, and much more normalized.

    • CommentAuthorPettyRider
    • CommentTimeApr 11th 2007
     permalink
    Although, the first approach can be extended if your application grows. All your relations are handled in one table, and you can write your program to find these relations for any type of data you might add on. For just blogs with comments, though, I'd stick with Varland's assessment.
    • CommentAuthorvarland
    • CommentTimeApr 11th 2007 edited
     permalink

    I disagree with PettyRider… I can't remember ever seeing an argument for using a join table unless you're modeling a many-to-many relationship. All one-to-many relationships should be modeled using a foreign key unless there's an extremely compelling reason not to do so.

    • CommentAuthorMatt
    • CommentTimeApr 11th 2007
     permalink
    Yeah, I don't see any reason to to create a new associative table. Either way you are going to go through the same amount of tables getting the answer... I am not an optimization guru though. The only way I could see having the associative table is if you want people to be able to reply to comments.
    • CommentAuthorvarland
    • CommentTimeApr 11th 2007
     permalink
    I don't mean to keep harping on the same thing, but, even if you want to allow people to reply to comments, you don't need an associative table. Simply add a column in your comments table to store the parent comment ID. If the parent comment ID is null, you know the comment was in reply to the article itself, not another comment. Similarly, if the comment has a parent comment ID, you know it was in reply to another comment, not the article itself.
    • CommentAuthorMatt
    • CommentTimeApr 11th 2007
     permalink
    Yeah, you win varland.
    • CommentAuthorvarland
    • CommentTimeApr 11th 2007
     permalink
    I'm just glad people are asking questions about something other than design. I know this is a design oriented site, but questions about query efficiency are important too.
Add your comments
    Username Password
  • Format comments as (Help)