Answer the question
In order to leave comments, you need to log in
[DB] What is the best way to store product attributes if their quantity is known and will not change?
Good afternoon.
Need advice from people who understand database design. The initial conditions are as follows:
1) there is one type of product (and there will be no others)
2) the product has a fixed and known number of properties (maximum 2 different colors, shape, material)
In the future, according to these properties, it is necessary to search for products.
The most obvious solution is to add as many fields to the product table as we have properties:
PRODUCT - id - name -color_one -color_two - style - fabric
PROPERTY - id - property_name - property_value
Answer the question
In order to leave comments, you need to log in
I would make style_id int instead of style varchar in the main table and keep the styles(int, varchar/unique) table. This way there will be no duplicates, and the main table will only hold field id's.
When a new style appears, it is added to the styles table.
Well, the classics of the genre Entity-attribute-value model
I would do it in the standard way:
Product
- id
- name
- color_one_id
- color_two_id
- style_id
- fabric_id
And the corresponding tables: COLOR, STYLE, FABRIC. If you don’t have a super-visited project, then you don’t need to invent anything else.
Optimization 1. If, for example, the style is clearly defined, then it can be stored as ENUM.
Optimization 2. If additional requests bother you, then store lists of colors, styles and materials in the form of server configs (in the form of simple arrays).
For the client side, use <select></select>
.
My God. Looking at the advice, I was horrified. You will intimidate the author of the question now. To answer this most banal question, it is enough for him to read not just a book, but some article on database design, this is the basis of the basics.
Judging by the question:
1. You obviously do not have thousands of items of goods there.
2. you are not a programmer
Proceeding from this - do it as it is easier, clearer and faster to do it for you. “Correctness” in this case is absolutely useless.
Wanted to change the list — wrote ALTER TABLE. And to store everything in ENUM - the simplicity is obvious, and the styles are the same, although they are not known in advance, but they do not change every 3 minutes. And you don't have to write any JOINs. JOIN'y are actual not when in directories <100 records, and when it is really the DIRECTORY. Otherwise, ENUM and ALTER TABLE are quite a silver bullet.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question