views:

456

answers:

3

Say I have a database of clothing products. In addition to many other fields, the database needs to store ratings (marks out of 10) on different categories for each product.

For example, one category could be 'occasions'. The occasions could be as follows:

Wear to the office Wear to a wedding Wear on a date ... Wear to a funeral

Each product needs to be assigned a score out of 10 for each of these. So one product could have:

Office: 5 Wedding: 7 Date: 10 Funeral: 0

Which would indicate the item is something cheerful, not too understated, not too formal perhaps.

There are a number of categories like this, and these will be used as part of a search algorithm so speed could potentially be an issue. Also I don't want my product table to get huge. This means I'm uneasy about storing this in the product table as each response having it's own column.

Possibly store this way except in a different table with a join? Just looking for a reasonably flexible and elegant solution.

If I haven't been clear please let me know.

P.S Am using MySQL if that makes a difference...

EDIT: I don't think having a category table and a join table with product_id, category_id and score would work very well. You would either have to name each score column (e.g. wedding_score etc) in the join as aliases, or get multiple score columns returned (one for each category).

+1  A: 

You're on the right track. Have a table for "category", with an intermediate table with two columns, one the PK for products, the other the PK for the product; plus a third for the score. Pretty standard pattern. Then typically the intermediate table will have a composite PK (e.g. productid + categoryid) and another non-unique index on categoryid.

le dorfier
Well the join table would have to have 3 cols: product_id, category_id, score? Or have I missed something?
@rmbarnes: yes, also score. And throw in another id for PK... combined PKs are trouble
tehvan
I differ on the trouble; unless you're using a framework that requires the overhead.
le dorfier
I really don't have a problem with dual PKS, I know some people do, probably because frameworks hate it.
I don't think what your saying will work that well, would be too hard to work out which score is for which category (in terms of column names, you'd just have multiple score columns....)
It would be easy. You'd know which category was what based on the ID of the row. You can case the select statement to get the average category scores. The only way this isn't that great is if the categories are dynamic. Are the categories dynamic?
Robert C. Barth
It's more that this structure is going to be part of a complex(ish) search algorithm, plus client will prob want to add categories. I also wrote the post badly because categories have properties and those properties have scores. I can do it, just wondered if there was a better solution.
A: 

Well, A relational database is optimised for storing relationships so a table for storing item descriptions, another for Scoring Categories (including Score Description) and a joining table (item to score) seems the perfect answer.

What are the drawbacks? Well, each item will have several score records, thats true. It also wouldn't be that readible in a query browser. But with the correct keys and indexes it would be fast enough to use the way you need it.

Brody
+1  A: 

EDIT: I don't think having a category table and a join table with product_id, category_id and score would work very well. You would either have to name each score column (e.g. wedding_score etc) in the join as aliases, or get multiple score columns returned (one for each category).

I think this IS the way to go, because it lets you treat the different scores identically. Most of your code won't need to be adjusted when a score type gets added, renamed, removed, etc.

If you're worried about complicated joins for aggregating this info, maybe a view or two would help.

grossvogel