views:

165

answers:

2

Hello,

I'm working on a shopping cart and my problem is, how should I design database for size selection for products? Tshirts can be "XL, L, M, S" etc. and shoes can be "36,37,38,39...blabla" Should I do just one size table or many tables for several types (tshirt, shoes etc.)?

Thanks in advance...

+5  A: 
Table: size_selection

Item_id numeric
Item_size varchar
Seq numeric

Item_id -> item table (say id = 1 -> tshirt. id = 2 -> shoes.)

So, your table would be

Item_ID   Item_size   Seq
__________________________
   1         S         1
   1         M         2
   1         L         3
   1         XL        4
   2         36        1
   2         37        2
   2         38        3
   2         39        4

Then, on the page, just get your dropdown values as

Select item_size from size_selection
where item_id = :p_ID
order by seq

Sequel syntax may vary in your flavour.

glasnt
Upvoted for an answer that would work. Nice job, @TomatoSandwich, but I'd have two different tables, shirtSize and pantSize. Just my $0.02. This is especially true when trying to make a "Size" class or enum and using that as a property to a Shirt class, a Pant class, or a Garment class.
Rap
Not sure why you'd want to split shirts and pants, where there's still shoes, hats, etc (probably). My solution is just a generic Item code value table, with a FK into a Item type table. 2 tables, unlimited item types.
glasnt
This answers his stated problem quite well. I just wanted to add, that if you ever need two or more pulldowns on an item (like selecting size and color) you can expand/generalize this a bit, call the table item_options, and add a field for which attribute. so then you have item_id, attribute (color, size, etc), value (large, pink, etc), seq
JasonWoof
If you wanted to be really sneaky, you could have an attribute_id ;)
glasnt
Always keep in mind the business goal: to select a specific product number among a class of similar products. Using attribute numbers makes sense if that is the most convenient way for customers to select a product. However, there are other approaches too -- like simply listing all of the variations and asking the user to select the appropriate one. They all go back to the problem of searching, and there is no one right answer. In this case, it seems to be a small number of options, so the only bad way to solve it is by over-complicating it ;)
Jeff Davis
+1  A: 

I'm not quite clear what your reasoning is for wanting multiple tables. Perhaps you were getting hung up on the fact that some sizes are alpha and some are numeric.

If thats the reason then forget it straightaway. There is no useful numeric meaning whatsoever for different variants like this so they should all be stored as a varchar to support alpha numeric characters.

If you need to sort then put a sortorder in too. That should be separate. Don't try sorting on the item size itself - as far as you application is concerned they are all just individual choices and its not impotant that they are numeric.

Simon_Weaver