HummingBird24
HummingBird24

Reputation: 191

Universal Table/Join Table A Good Idea?

Let's say you have a list of entities: Post, Question, and Gallery and these entities can be liked and commented on by a user and also hold tags.

Would would it be a good idea to compact things so there's not a bunch of tables (Option 1) or is it better to create as many tables as necessary although the tables will look very similar to each other (Option 2).

Option 1

enitity

entity_type

post

gallery

question

tag

like_

comment

tag_to_entity

Option 2

tag

post

gallery

question

like_to_post

like_to_gallery

like_to_gallery

comment_to_post

comment_to_gallery

comment_to_question

tag_to_post

tag_to_gallery

tag_to_question

Upvotes: 0

Views: 91

Answers (1)

George D.
George D.

Reputation: 265

I would say option 2 is preferable because:

  1. It allows the database to enforce foreign key constraints on you join tables
  2. Performance will likely be better because the size of your joins will be much smaller

Upvotes: 1

Related Questions