I am using MySQL and want to be sure of the best way of doing this before moving forward. I want a properties table to describe aspects of products such as size, price, color, and so forth. This is pretty much the idea:
http://stackoverflow.com/questions/2050431/database-design-for-multiple-product-types
But I do not want two separate tables, one for Measurement and one for Trait. I would like to have one table with the property values if possible.
Is it acceptable to store values that are text or numbers in the same column? Can I store a price like 16.99 in a column of data type varchar or text? I read that MySQL will convert strings to numbers and vice versa as necessary. So if a price is stored in a text field, can it be sorted? Can calculations be performed on it or would I have to do the type conversion?
properties_table
id | property | description
---------------------------------------------------
1 | color | The color of the item
2 | price | Per unit price in USD
3 | size | Product size
4 | weight | Per unit weight in pounds
product_properties_table
id | product_id | property_id | value
-----------------------------------------------
1 | 1 | 1 | red
2 | 1 | 2 | 39.99
3 | 1 | 3 | 11
4 | 2 | 2 | 2.79
5 | 2 | 4 | 2.8
products_table
id | category_id | product_name | product_description
---------------------------------------------------------------------------
1 | 5 | Nike | Athletic shoe
2 | 16 | Something else | Something else
This is a simplified example. A product may have multiple properties and there will be multiple types of products. Listing the product properties by column really is not possible in this case.
Am I on the right track? The tables would be joined to get the information I need. To get a product’s price, I would query the product_properties_table with the appropriate product_id and select property_id of 2. See the link above for a better general explanation. Can I store text and numbers in product_properties_table.value? Is there a better way of doing this?
Multiple types of products (or services) and multiple properties is what I need. Any advise will be appreciated.