In your example, you confused the attribute name and attribute value, so put them in 1 column. Attributes must be moved to a separate table (brand, model, motor, etc.). and on topic:
/* группы продуктов*/ table product_groups: group_id, group_name, ... /* свойства продукта (а еще лучше названия хранить отдельно т.к. бывают разные одинаковые названия)*/ table product_properties: product_id, product_name, ... /* связки свойств по группам и продуктам. где все поля часть первичного ключа */ table product_properties_def: product_group_id, product_id, related_product_id бинд конкретных брендов: insert into product_properties_def values (1, 1, 2); -- привязка БМВ к брендам insert into product_properties_def values (1, 1, 2); -- привязка Ауди к брендам бинд конкретных моделей автомобилей: insert into product_properties_def values (1, 4, 5); insert into product_properties_def values (1, 4, 6); привязка моторов: insert into product_properties_def values (1, 9, 10); insert into product_properties_def values (1, 9, 11);
If you are planning a tree-based product walk:
1) first select the brand (top of the tree)
2) select the model (the model is tied to the brand)
3) other attributes are selected (tied to a specific model)
then at every step you get a simple query like:
select * from product_properties where product_id in (select product_id from products where name = ? /* brand name */)
If you need to create complex filters for data, then you need another product_properties_def_flat
table product_properties_def_flat
similar to product_properties_def
table. the difference is that the latter will store the expanded data:
связывание атрибутов без прямой зависимости (): insert into product_properties_def_flat values (1, 1, 5); insert into product_properties_def_flat values (1, 1, 6); insert into product_properties_def_flat values (1, 1, 10); insert into product_properties_def_flat values (1, 1, 11);
the product_properties_def_flat
table must be updated with the triggers hung on product_properties_def
and contain all valid attribute combinations, its size will be much larger and it will contain all the data from product_properties_def
.