Creating a reply to the comment table.

I have a basic comment table with Id,Username,Comment, Date-time as the fields ..I need to add a reply table to this there a person can reply to the corresponding comment and then another person can reply to that reply and etc etc .Thank you in advance.

more ▼

asked Dec 14, 2012 at 07:52 PM in Default

avatar image

538 17 22 29

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

I would do it by keeping the replies in the Comment table, but having a separate table to link the Comments, thus:


So that if Comment 47 is a response to Comment 13, then there'll be a record in the ReplyTo table:

 CommentID    InReplyToCommentID
 47           13

This has the advantage of being small, and not requiring multiple organisations for storing the same sort of information, as well as not affecting any existing systems.

more ▼

answered Dec 14, 2012 at 09:16 PM

avatar image

ThomasRushton ♦♦
42.2k 20 57 53

Thank you Sir..Really Appreciate your help !

Dec 17, 2012 at 01:43 PM fashraf
(comments are locked)
10|1200 characters needed characters left

Though Thomas Rushton already provided a good solution, I'd suggest adding just a new column, InReplyToCommentID in the comments table. Then you have both comments, replies and their relation all in the same table. When you want to find all first Level comments you'll just need to query for comments with InReplyToCommentID being null, with no need to involve a second table. Unless a reply can be related to more than one comment, I think it's a simpler, more efficient solution.

more ▼

answered Dec 17, 2012 at 02:28 PM

avatar image

Magnus Ahlkvist
22.3k 20 43 43

You're right, it is a more elegant solution.

However, if the aim is to not change existing data structures...

Dec 17, 2012 at 02:45 PM ThomasRushton ♦♦

So basically i would get a nested grid ? I am assuming ..plus there is only one primary key right ?

Dec 17, 2012 at 04:04 PM fashraf

How it would be presented I don't know.

But there will be only one primary key - each comment will have its own ID, whether it's a reply or not. There will be a foreign key constraint for InReplyToCommentID, referencing ID. Creating an index for InReplyToCommentID would be a good idea.

Dec 18, 2012 at 04:01 AM Magnus Ahlkvist
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Dec 14, 2012 at 07:52 PM

Seen: 4023 times

Last Updated: Dec 18, 2012 at 04:01 AM

Copyright 2018 Redgate Software. Privacy Policy