There is a chat, it has group dialogs. And there is one room shared by all users. What is the best way to do when designing a database?

  1. Create a common room with id 1.

  2. Do not add this room to the database, and in other tables refer to it via dialog_id = 0.

I tend to the second method, but then you will not be able to use a foreign key that refers to dialogs.dialog_id.

  • How about making a room a sign - its type (general, private, admin, etc.)? - Kromster
  • The type of dialogs indicates the question - add a common room to the database or not. - Eugene

1 answer 1

I would create a common room with id 0 or -1 . Even better is -1 , since this is not a default value. And in general: https://ru.stackoverflow.com/users/-1 - SO developers agree.

  • Under the word "created" you mean adding a general chat to the table of dialogs? Or just in other tables referring to the dialog, specify dialog_id = -1? - Eugene
  • @ Eugene, yes, adding to the table. Otherwise, the keys will be unverifiable. - Qwertiy
  • Then what is the advantage of ID 0 or -1 before ID = 1? Moreover, the ID field is autoincrement and I am not even sure that 0 or -1 can be added to it. - Eugene
  • @ Eugene, you can accurately insert specific id in mssql with auto increment, I don’t know about mysql, but I think that it is also possible. The advantage is that it is constant and does not depend on auto-increment. - Qwertiy