views:

477

answers:

2

I want to implement modules such as comment, rating, tag, etc. to my entities. What I thought was:

comments_table -> comment_id, comment_text

entity1 -> entitity1_id, entity1_text

entity2 -> entitity2_id, entity2_text

entity1_comments -> entity1_id, comment_id

entity2_comments -> entity2_id, comment_id

....

Is this approach correct?

A: 

It is simpler than that. You are going to want something like this:

Entity: EntityID EntityText

Comments: CommentID AssocEntityID CommentText

Where AssocEntityID has a foreign key relation to the Entity table EntityID column.

For this solution, to get all comments for Entity with ID 1, you would do this:

SELECT CommentID, CommentText FROM Comments WHERE AssocEntityID = 1
Matthew Jones
A: 

No, I would suggest having just one entity_comments table that is an intersect table between comments and entity. You would have to have the entity1 and entity2 ids in the one comments table as separate attributes.

so it would look like:

entitiy1 -> entity_comments <- comments_table
entitiy2 -> entity_comments <- comments_table

a simple select might be:

select text
  from entity1
     , entity_comments
     , comments_table
  where entity1.id = entity_comments.entity1_id
    and entity_comments.comment_id = comments_table.id
northpole
You say insert two attributes as entity1 ans entity2 attributes to the entitycomments table. What if i have 10 entities? 10 seperate attributes and lots of null values?
Efe Kaptan
then at that point you might need to redesign your table structure entirely. It would get messy to keep adding attributes to the intersect table with each new entity table. If it is just one comment per row in the entity then you could put the comments.id in the entity1 table and leave out the intersect table. The intersect table allows for multiple comments per entity1 row.
northpole