Saving outliers, which is a good approach

Imagine you have a website with several drop-down lists that populate from a database. The goal is to store these values ​​in a database and retrieve them when rendering the form.

I saw two approaches:

1) One table for list type:

profession_type |id|value| hobby_type |id|value| 

2) One table for all search values:

 |id|type |value| |0 |profession_type|value| |1 |profession_type|value| |2 |profession_type|value| |3 |hobby_type |value| |4 |hobby_type |value| |5 |hobby_type |value| 

Are there any objective advantages for any of them? # 2 seems more general (you choose from a table by type to populate a specific drop-down list), but the table will be significantly larger than if you used # 1. In addition, if you use # 2, all foreign keys point to the same same giant table. This doesn't seem like a big deal, but in my eyes this approach seems more confusing.

+2
source share
1 answer

I would go with number 2. You can retrieve and cache all the information, and even if you add new types, you do not need to change your database or search logic.

If you add and change tables, you need to touch your database schema for all changes in the future and change the search code for new tables.

In addition, your concerns about the size of the table are IMPOSSIBLE. If you do not have millions of lines, everything will be fine. Can I imagine a few hundred or thousands of lines? Databases can easily handle this.

+2
source

Source: https://habr.com/ru/post/1395497/


All Articles