Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you only store id and name for color it could very well be a php function, so you don't need a color table:</p> <pre><code>&lt;?PHP function color($color) { $colors['red']=1; $colors['blue']=2; $colors['green']=3; return $colors[$color]; } //example of usage $sql ="... where `color`=".color($_POST['color'])." "; ?&gt; </code></pre> <p>Also since size is a small value (is not a big text for example) then you don't need a size table at all. All you need is a product and a quantity (better rename it to product_info) table:</p> <pre><code>mysql_query(" CREATE TABLE `Product` ( `product_id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `product_name` varchar(30) NOT NULL , unique(`product_name`), PRIMARY KEY (`product_id`) ) ENGINE=InnoDB ROW_FORMAT=COMPACT;") or die(mysql_error()); mysql_query(" CREATE TABLE `Product_Info` ( `product_id` INT UNSIGNED NOT NULL , `color_code` char(3) NOT NULL , `prod_col_size` varchar(15) NOT NULL , `quantity` mediumint UNSIGNED NOT NULL , PRIMARY KEY (`product_id`,color_code,prod_col_size), CONSTRAINT `prod_id_FK` FOREIGN KEY (`product_id`) REFERENCES `Product` (`product_id`) ON DELETE CASCADE ) ENGINE=InnoDB ROW_FORMAT=COMPACT;") or die(mysql_error()); </code></pre> <p>This is not perfect normalization but you already choosed this way. The queries will be very easy this way. To bother with long joins in a well normalized schema makes sense. To bother for no reason at all!? You only need to make a join in two tables in case you need some filed from the product table.</p> <pre><code> /*Find how many red items has a specific product*/ select sum(`Product_Info`.`quantity`) from `Product_Info` where `color_code`='124' and `product_id`='3'; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload