views:

230

answers:

2

Hi, I new in database design. What is better option for product attribute database design for cms?(Please suggest other options also).

option 1: 1 table

products{
id
product_name
color
price
attribute_name1
attribute_value1
attribute_name2
attribute_value2
attribute_name3
attribute_value3
}

option 2: 3 tables

products{
id
product_name
color
price
}

attribute{
id
name
value
}

products_attribute{
products_id
attribute_id
}

Thanks, Yosef

+1  A: 

That depends on what do you want from your database. If all of your products are of the same type and have same attributes then you just need to do something like that:

products{id: integer, product_name: string, color: string, attribute_name1: string, attribute_name2: string...}. Attribute_name{} should a meaningful word, just like "color" (which is an attribute too).

parnas
No, they are not the same because that I add 2 fields: 1. name 2. value
Yosef
+2  A: 

You're making a common mistake of database design, storing name in one column and value in another column. This is not a relational database design.

Each attribute should be named by the column name. Color, pages, shirt size, publish date, should be column names.

If each product type has a distinct set of attributes, there are other solutions. See my answers to:

Also please read this story: Bad CaRMa: Introducing Vision before you implement a database designed around name-value pairs as you are doing.

Bill Karwin
thank you Bill, I give my votes also to other my questions, thank you for tell me.
Yosef
Hi Bill, I read You links Thanks,I have question: Why do you prefer to use **Class Table Inheritance** and not **Single Table Inheritance**?Please explain,Thanks,Yosef
Yosef
@Yosef: With Class Table Inheritance it's more clear which groups of columns go together. Also you can add a new subtype table at any time, with its own set of type-specific columns, with no need to alter the parent table that contains columns common to all subtypes. This is important for example for MySQL, where `ALTER TABLE` is an expensive operation.
Bill Karwin
Thank you very much for explanation Bill
Yosef