D
D
Dmitry Yakovlev2018-11-01 09:49:47
MySQL
Dmitry Yakovlev, 2018-11-01 09:49:47

How to store data in database of indeterminate type?

I have an entity product attribute (size, length, composition, weight, etc.). They can be of different types, for example, size is an integer, product weight is a fractional number, composition can be a string. You can make different columns in attribute_products table like value_int , value_decimal , value_string . But then it's hard to update.
At the moment I did this: attributes
table (id, name, type) - type is the enumerated type of its value (int, decimal, string) attribute_products table (id, id_attribute, id_product, value_int, value_decimal, value_string)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kim, 2018-11-01
@kimono

Use `text`, Luke!

P
ponaehal, 2018-11-01
@ponaehal

Heh ... There is no absolutely correct answer outside the context of the tasks being solved.
For example, if you need to retrieve this data often, and even by sorting (probably there is no need to explain the difference in the sort order of string and numeric values), then your current solution may be the most correct. If the values ​​of all characteristics are packed into one field, then there will be problems with extraction, because it seems pointless to build an index on a text field (due to the heterogeneity of the stored information)
On the other hand, if you do not plan to often sort goods by the value of one of the characteristics (for example, by weight, dimensions), then God would be with him - make a text field
Threat That's interesting what you write? It seems that a lot of online stores are already written for different CMS. But no, you need to make your own, dear ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question