B
B
blare2013-05-30 10:03:03
Database design
blare, 2013-05-30 10:03:03

Enum or multiple tables?

Good afternoon!

I asked myself such a question, for example, let's take the same Vkontakte. We have several entities in the database, for example, a wall, a photo, a group.

You need to comment on them. Which way would you choose:

1. Make a single table of comments, which will have an enum field with enumerations 'user', 'photo', 'group'.

2. Make three different tables user_comments, photo_comments, group_comments

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
EugeneOZ, 2013-05-30
@EugeneOZ

Three different tables, of course. Different entities need to be stored in different tables.

G
gleb_kudr, 2013-05-30
@gleb_kudr

Why three tables are inconvenient - then you get tired of carrying out a search case by comment. Which of the three tables contains the desired comment with the text "Me and my friend Masha"? All three will need to be bypassed. And this is an unnecessary complication of the code.
What is a foreign key to be afraid of? Integrity is checked by means of a DB.
About relationships. They can be placed in separate tables so as not to fence extra columns. And the enum would address them.

B
basht, 2013-07-16
@basht

Postgres has table inheritance!
You can make one empty without a binding field to the entity id, and inherit three non-empty more specific ones from it.

A
AlexeyVD, 2013-05-30
@AlexeyVD

I can't speak for other DBMSs, but in MySQL, using ENUM is generally considered bad practice, because this type allows the insertion of elements that are not included in the enumeration (they will be assigned an index of 0).
If you do not want to violate the integrity of the data, then definitely make 3 different tables with foreign keys.

S
StrangeAttractor, 2013-06-03
@StrangeAttractor

I would put all the comments on everything in a separate table in general, and then one way or another connect it with the commented one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question