简体   繁体   中英

Database structure to house products with variable multiple options

I'm looking for some ideas for a mySQL database to store products that have multiple (and variable) options and prices per product. Eg some products may require several columns to store details for each of the product options:

Product 1:
SKU
Name
General product description
--------------------------
options for product 1:
Height   Width   Depth   Weight   Material   Other_1   Other_2  Etc...   Price


Product 2:
SKU
Name
General product description
--------------------------
options for product 2:
Flow-rate   Material   Weight   Other_1   Other_2  Etc...   Price

I'm a little stumped because the number of columns and column names might be different for each product and each product may contain numerous options/prices.

So far I'm using a 'products' table and a 'prices' table:

CREATE TABLE IF NOT EXISTS `products` (
product_id int(11)  NOT NULL AUTO_INCREMENT
,product_type text
,product_page_1 text
,product_page_2 text
,product_page_3 text
,product_page_4 text
,product_sku text
,product_name text
,product_head text
,product_desc text
,product_image text
,product_sequence text
,product_flags text
,product_status text
,PRIMARY KEY (product_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=102;


CREATE TABLE IF NOT EXISTS `prices` (
price_id int(11) NOT NULL AUTO_INCREMENT
,price_type text
,price_assoc text
,price_desc text
,price_option text
,price text
,price_sequence text
,price_flags text
,price_status text
,PRIMARY KEY (price_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=109;

There can be any number of 'prices' per product. 'prices' are associated with a product ID via 'price_assoc' field.

So I can have countless price entries for each product, but whats the best way to add multiple price options per price being that each product may have varying options for its associated prices?

What you are looking for, is a EAV table. Entity-attribute-value. Thats the kind of structure where u have table of records, and then a table of subrecords.

Records table:

id name active

Subrecords table:

id record_id key value

And then you can do a joined query where u get a record, and all the subrecords with it. So subrecords are in one to many relationship with records.

There is php example available in github: here

I would probably use the same column for prices even though it is the same products. So instead of having to specify 4 columns for prices and descriptions for each row. Why not create a table with only on row for prices, one row for description/options/text etc. and then maybe ad a 'price-ID' field to keep track of different levels of prices.

Lets say you have one product, Product A, which have 4 different prices, then your prices table would look something like below:

product_id      product_name     price      description     option     text       price_ID
1               Product A        100        'Bla bla'         A          bla        1
1               Product A        250        'Bla bla'         B          bla        2
1               Product A        400        'Bla bla'         C          bla        3
1               Product A        500        'Bla bla'         D          bla        4

This way you can have as many prices for each product as you wish, and it takes down the complexity of your database a bit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM