Sql-server – Modelling products along with their attributes and discounts for an e-Commerce database

database-designdatabase-diagramseavsql server

Scenario

My application will have products belonging to 1 company. Each of those products will:

  1. Have at least 1 quantity
  2. Have 0 or more discounts
  3. Have 1 or more attributes
  4. Belong to 1 category, which can have parent a parent category

A quantity can be 1 unit, 1 box, 1 pallet… Each quantity will thus have it's own price and that price can change over time. So I've added a fromDate attribute to know which price was active at which time. Is necessary when a product gets bought so I know the price of the quantity at the time of purchase.

A discount can be something like 50 euro, 5%, 2 for 1… Also having a fromDate so I know which discount is applicable.

Attributes will always belong to 1 product. And these will contain a name of the attribute and the value of the attribute. Their goal is to give the company the possibility to add more detailed information next to the existing required information.

Examples of attributes:

  • Key Feature 1 => Key Value 1
  • Name => Something
  • Color => Black
  • Size => 10 cm (h) x 5 cm (w)
  • Weight => 450 gr

Diagram

The following diagram represents my complete database schema as I have it now (only the section regarding products is relevant here):

enter image description here

Questions

  1. How do I create my schema so that my requirement for discounts is met? How do you handle the ability to have different types of discounts? And how do you best keep track when there is an active discount for the product and where there is none?
  2. Does it make sense to simply store attributes as name and value? It's possible that the same attribute name will be used for multiple products. Do I keep a list of all possible attribute names? What is the most dynamic way of tackling this issue?

Best Answer

regarding question 1): A table with the following fields 1) date ,to set the date-from which the discount is valid 2) product, to define which product we are talking about 3) discount_type, i.e. 1=percentage, 2=how_many_product_for_this_price, 3=price 4) percentage, needed for discount_type=1 5) quantity, needed for discount_type=2 6) price, needed discount_type=3

Example: If you have this data (I will leave out date for simplicity):

product, discount_type, percentage, quantity, price
1, 1, 0.5, null, 42
2, 2, null, 2, 42
3, 3, null, null, 25

product 1 has a discount of 0.5*100 = 50%, and a price of $42

product 2 can be sold 2 for the price of one, so 2 for $42

product 3 has a discount for $25