Mysql – How should I handle Products with different attributes in the database structure

database-designeavMySQL

We have an e-commerce site (web application) built with PHP.

There are different types of products like mobiles, tshirts, watches, books.

  • A mobile can have attributes like, for example, ram, model name, os.
  • A t-shirt can have attributes like, for instance, color, size.

My questions are the following:

  1. Is it better to save all attributes in a single table called Products (even if there will be/it will create a lot of 'NULL' marks)?

  2. Save different types of products in different tables like Mobiles, Tshirts, etc.?

In the second case, more tables will be created, see the two images below:

Diagram 1

Diagram 2

As suggested via comments, I am using the EAV model, so I drew the following design [ image 3 ] :

enter image description here

as suggested in answer, instead of eav , i combined all 3 tables in one table as product_information + attributes_list + attributes_values = products information i drew all in one table [image 4 ]:

enter image description here

Please suggest me is i need to go with image 3 or image 4 ?

Best Answer

What you can also do, is following. You create a table with three columns: product_id, property and value. So you get not more tables then neccessary. But it's a matter of taste.