database - SQL Filter by attributes (Design) -
i have database of products i'd filter arbitrary categories. let's sake of example run garage. have section of products cars.
each car should have collection of attributes, cars have same number , type of attributes; instance colour:red, doors:2, make:ford ; same attributes set various values on cars.
gut feeling tells me best add "colour", "doors" , "make" columns product table.
however: not products in table cars. perhaps list tyres on page of cars. obviously, "colour" , "doors" won't apply tires. so, if user selects colour=red filter, still tires shown lack colour attribute.
mulling on (and i'm not database guy apologise if approach horrible) considered having single "attributes" column fill arbitrary number of arbitrarily named attributes, use sqls string functions filtering. guess use bit field here if planned carefully. seems hackish me though, i'd interested know how of larger sites such amazon this.
what issues these approaches, can recommend alternatives or shed light on subject me?
thanks in advance
you should read database normalization. not idea use concatenated strings values in single column. made small sqlfiddle start playing around. not solve problems, may lead in right direction.
schema:
create table product (id int, name varchar(200), info varchar(200)); insert product (id, name, info) values (100, "porsche", "cool"); ... insert product (id, name, info) values (103, "tires", "you need them!"); create table attr (id int, product_id int, a_name varchar(200), a_value varchar(200)); insert attr (id, product_id, a_name, a_value) values (1, 100, "color", "black"); insert attr (id, product_id, a_name, a_value) values (2, 100, "doors", "2"); ... a query:
select * product inner join attr on attr.product_id=product.id attr.a_name="doors" , attr.a_value = "2"
Comments
Post a Comment