问题
Now I'm planning the structure of the database products. There was a question of storage options / parameters of the goods. For example, if it were the size of the goods - it would be enough to create three fields to the width / height / depth, but what if dozens of parameters? It is assumed that the list of parameters can be changed and I see about this structure: 1 - width 2 - height ... 11 - color
but I can not tie it like a commodity - it is possible to make the field with json type
{1: "20", 2 "30", ..., 11: "white"} and then treated at a sample of the goods, but I do not know how then do filter parameters
who can advise on the right path?
回答1:
This thing can be achieved with three methods
- Adding all
separate columns
in theproducts table
. - Adding an
extras
column with longtext type at end ofproducts table
to store all extra/dynamic parameters in it by json-encoding or serializing. Last one is to use a separate meta table for storing key,value pair alongwith product_id in that table.
CREATE TABLE product_meta() id BIGINT AUTO_INCREMENT PRIMARY KEY, // Primary key of this table product_id INT, // For joining purpose and reference meta_key VARCHAR(16), meta_value VARCHAR(64) );
So then for product ID 137, you might have several records:
|------------------------------------------|
| id | product_id | meta_key | meta_value|
|------------------------------------------|
| 3137 | 137 | size | large |
| 3138 | 137 | color | green |
| 3139 | 137 | height | 11.3 |
|------------------------------------------|
回答2:
There's basically two ways to do it - you can create a series of "udf" (User Defined Field) columns in your table, like udf1, udf2, etc - then somewhere else in your app you say that "udf1 == color, udf2 == weight".
Another way would be to create a productmeta table, that had a structure like:
id BIGINT AUTO_INCREMENT PRIMARY KEY
product_id INT
property VARCHAR(16)
propvalue VARCHAR(64)
So then for product ID 137, you might have several records:
|------------------------------------------|
| ID | product_id | property | propvalue |
|------------------------------------------|
| 3137 | 137 | size | large |
| 3138 | 137 | color | green |
| 3139 | 137 | height | 11.3 |
|------------------------------------------|
The downside is that these tables can get really big, really fast.
来源:https://stackoverflow.com/questions/33975719/how-to-store-the-properties-of-the-goods-in-mysql