0

I've a requirement to design a database for an ecommerce app that has vast scope of product categories ranging from pin to plane. All products have different kinds of features. For example, a mobile phone has specific features like memory, camera mega pixel, screen size etc whilst a house has land size, number of storeys and rooms, garage size etc. Such specific features go on and on as much as we've products. Whist all have some common features, there are mostly very different and specific features of all. So, it has gotten bit confusing while designing its database. I'm doing it for the first time.

My query is about database design. Here is what I'm planning to do:

  1. Create a master table with all fields, that tells if a field is common or specific and map them with respective category of the product. All products will have "common" fields but "specific" will be shown only for one category.

table: ALL_COLUMNS

columns: id, name, type(common or specific), category(phone, car, laptop etc.)

  1. Fetch respective fields from all_columns table while showing the fields on the front.

  2. Store the user data in another table along with mapped fields

table: ALL_USER_DATA

columns: id, columnid, value

I don't know what is the right way and how it is done with established apps and site. So, I'm looking forward if someone could tell if this is the right way of database architecture of an ecommerce app with highly comprehensive and sparse set of categories and features.

Thank you all.

chuck
  • 125
  • 3
  • 16

1 Answers1

2

There are many possible answers to this question - see the "related" questions alongside this one.

The design for your ALL_USER_DATA table is commonly known as "entity/attribute/value" (EAV). It's widely considered horrible (search SO for why) - it's theoretically flexible, but imagine finding "airplanes made by Boeing with a wingspan of at least 20 metres suitable for pilots with a new qualification" - your queries become almost unintelligible really fast.

The alternative is to create a schema that can store polymorphic data types - again, look on Stack Overflow for how that might work.

The simple answer is that the relational model is not a good fit for this - you don't want to make a schema change for each new product type your store uses, and you don't want to have hundreds of different tables/columns.

My recommendation is to store the core, common information, and all the relationships in SQL, and to store the extended information as XML or JSON. MySQL is pretty good at querying JSON, and it's a native data type.

Your data model would be something like:

Categories
---------
category_id
parent_category_id
name

Products
--------
product_id
price
valid_for_sale
added_date
extended_properties (JSON/XML)

Category_products
-----------------
category_id
product_id
Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
  • It's not the RM is a bad fit, it's that DBMSs don't adequately support the appropriate DDL. (Via restricted DDL to be called by user interfaces and via optimized table operators.) It's only that lack of support that makes us "not want" to do that. Querying tables from a changing schema is exactly the same as querying changing JSON/XML values in a static schema. Both EAV & JSON/XML encode the tables the user actually cares about in other data structures. If you really wanted to query or implement that data in the non-tabluar form (never the case for EAV ugh), ok, use those data types. – philipxy Jun 02 '17 at 23:05
  • Thanks @neville-k, as mentioned in the referenced links, I think EAV is not completely an evil idea but I like the approach that you've recommended. Can you shed some light on how and what exactly do we save on tables and JSONs so that your approach is more clearer? – chuck Jun 04 '17 at 03:38
  • It's hard to be specific on what to save in the relational tables without an idea of your schema. Typically, I'd save the data that all product types have in the relational table, and everything else in JSON... – Neville Kuyt Jun 06 '17 at 07:30