Implementing Comments and Likes in database

The most extensible solution is to have just one “base” table (connected to “likes”, tags and comments), and “inherit” all other tables from it. Adding a new kind of entity involves just adding a new “inherited” table – it then automatically plugs into the whole like/tag/comment machinery.

Entity-relationship term for this is “category” (see the ERwin Methods Guide, section: “Subtype Relationships”). The category symbol is:

Category

Assuming a user can like multiple entities, a same tag can be used for more than one entity but a comment is entity-specific, your model could look like this:

ER Diagram


BTW, there are roughly 3 ways to implement the “ER category”:

  • All types in one table.
  • All concrete types in separate tables.
  • All concrete and abstract types in separate tables.

Unless you have very stringent performance requirements, the third approach is probably the best (meaning the physical tables match 1:1 the entities in the diagram above).

Leave a Comment