Product Database Structure Tips

Hi

I have just started designing my structure for a mysql database that will contain a product catalog for a photohgraphy site. I am struggling how best to structure the design though.
Each photograph item will have a Title (i.e Lakehouse), Category (i.e Scenery), Description (i.e blah blah), Price (i.e £120), Sizes (i.e 20x30, 25x35, 30x40 etc…) and Colours (i.e Red, Blue, Black etc…)

I have tried splitting the various fields into different tables to then join together with the below tables currently set up:

Category table

7721 ¦ Cities ¦ Category Style
3991 ¦ Scenery ¦ Category Style
4551 ¦ Beaches ¦ Category Style

Products table:

7721 ¦ New York ¦ Description ¦ £120
3991 ¦ Lakehouse ¦ Description ¦ £190
3991 ¦ Summer Field ¦ Description ¦ £120
3991 ¦ Farmhouse ¦ Description ¦ £220
4551 ¦ Florida Beach ¦ Description ¦ £350

Sizes table:

0001 ¦ 20x30
0002 ¦ 25x35
0003 ¦ 30x40

Colours table:

0001 ¦ Red
0002 ¦ Blue
0003 ¦ Black

Is this the best way? I’m stuck with how best to structure the sizes and colours as each photograph could have 7-8 different options for both.

Thank you in advance

You’ll need one or two tables to link colors and sizes to the photographs. Or do all photographs come in all colors and sizes?
And do all colors and sizes have the same price? Or is there a standard calculation to get the price for each size from the ‘standard’ price in the products table?

here’s a suggestion: design your tables but exclude the colour and size for a moment

is the price the same for all sizes and colours of the same photo? if not, leave that out too

oops, sorry, guido beat me to it

in any case my hint is still valid

oops sorry i made a slight mistake with the price, that should go in the size table yes. So it would be the below instead with the prices within the size table. The colour does not effect the price.
Not all photographs come in all colours and sizes, for example the New York Canvas comes only in Blue and Red, and in 20x30 and 30x40, where as Lakehouse comes in all colours and all sizes.
Sorry if i didn’t make it clear - this is my first time at structuring databases with multiple tables!

Category table

7721 ¦ Cities ¦ Category Style
3991 ¦ Scenery ¦ Category Style
4551 ¦ Beaches ¦ Category Style

Products table:

7721 ¦ New York ¦ Description ¦ Other Value
3991 ¦ Lakehouse ¦ Description ¦ Other Value
3991 ¦ Summer Field ¦ Description ¦ Other Value
3991 ¦ Farmhouse ¦ Description ¦ Other Value
4551 ¦ Florida Beach ¦ Description ¦ Other Value

Sizes table:

0001 ¦ 20x30 ¦ £120
0002 ¦ 25x35 ¦ £160
0003 ¦ 30x40 ¦ £220

Colours table:

0001 ¦ Red
0002 ¦ Blue
0003 ¦ Black

now you need a table for product sizes

your products table holds the description and other columns which are common to all sizes and colours

now you need a table that holds one rows for each available size

7721 01
7721 03

is a 20 x 30 always the same price regardless which photo? that would seem rather limiting if you wanted to offer a rare or valuable photo

yep the price is only relevant to the size, not what is actually photographed. (although i do agree with you about the rare/valuable suggestion)

So on top of my 4 original tables: Category, Products, Sizes, Colours i now have 2 extra tables:

Product Sizes:

7721 ¦ 01
7721 ¦ 03
3991 ¦ 01
3991 ¦ 02
3991 ¦ 03

Product Colours:

7721 ¦ 02
7721 ¦ 01
3991 ¦ 01
3991 ¦ 02
3991 ¦ 03

Is this correct?

Yes, if size and color are not related. That is, if each size can be delivered in each color specified for that photograph.

Size and colour is not related in any way. The only thing that determines the colour and size is the product itself. The colours specified for each photograph will be available in all sizes specified. So my final tables would be these 6 with no more needed:

Category table

7721 ¦ Cities ¦ Category Style
3991 ¦ Scenery ¦ Category Style
4551 ¦ Beaches ¦ Category Style

Products table:

7721 ¦ New York ¦ Description ¦ Other Value
3991 ¦ Lakehouse ¦ Description ¦ Other Value
3991 ¦ Summer Field ¦ Description ¦ Other Value
3991 ¦ Farmhouse ¦ Description ¦ Other Value
4551 ¦ Florida Beach ¦ Description ¦ Other Value

Sizes table:

0001 ¦ 20x30 ¦ £120
0002 ¦ 25x35 ¦ £160
0003 ¦ 30x40 ¦ £220

Colours table:

0001 ¦ Red
0002 ¦ Blue
0003 ¦ Black

Product Sizes:

7721 ¦ 01
7721 ¦ 03
3991 ¦ 01
3991 ¦ 02
3991 ¦ 03

Product Colours:

7721 ¦ 02
7721 ¦ 01
3991 ¦ 01
3991 ¦ 02
3991 ¦ 03

In terms of then querying the database - if i wanted to display the photograph Lakehouse on a php page with a dropdown option for the customer to choose the size and colour, would I use the INNER JOIN method to join all 6 tables together?

All six I don’t know. The ones you need to get the info you want.

sorry i worded that wrong, if i was to have the following code below to pull the sizes that Lakehouse (3991) is available in:


<?
$sql="SELECT*sizes*
FROM*Product_Sizes"
WHERE id=3991;
$result=mysql_query($sql);

$options="";

while*($row=mysql_fetch_array($result))*{
      $sizes=$row["sizes"];
      $options.="<option*value=\\"$sizes\\">".$sizes;
}
?>

This would then populate an html option form with the relevant sizes for the Lakehouse photo (3991):


<select name=sizes>
<option value=0>Choose
<? =$options ?>
</select>

How could i go about combining the size query with also the colour query so that i only have to do 1 query for everything instead of for each? (If that makes sense? I just want to ideally limit the amount of queries to the server to as few as possible)

You’d have to join the sizes table to get the description of each size to display in the drop down. Size ‘01’ wouldn’t be very clear for your average customer :wink:

And if the id’s and the descriptions in the sizes table and the colors table are of the same types, then you might use a UNION like this:


SELECT
    'size' AS type
  , ps.sizeid
  , s.description
FROM Product_Sizes AS ps
INNER JOIN Sizes AS s
ON ps.sizeid = s.id
WHERE ps.id=3991 
UNION
SELECT
    'colour' AS type
  , pc.colourid
  , c.description
FROM Product_Colours AS pc
INNER JOIN Colours AS c
ON pc.colourid = c.id
WHERE ps.id=3991 

Of course, looping through the result set you’ll have to check if you’re dealing with a size or a colour.

great thank you guido, is there a quick or easy way of checking whether it’s a size or colour within the loop or would i just have to use if and else PHP conditions?

An if…else… would work fine.
You could also load everything in a multiple array, and then loop through the $array[‘size’] and $array[‘colour’] arrays, but maybe that’s overkill.