2

I need to store similar products with different attributes (like length or color), should I add this products with different ids or as one product with many attributes?

Main problem that I need possibility to see quantities of products by attribute (4 red boots, 3 blue etc) and need to implement attribute selector on product page.

If create one id how to store attributes in cart table with selected attributes and how to manage quantities?

If create many ids it is easy to manage quantities but how to implement attributes selection?

I think about some SKU for linking product id with attributes and attribute values. But how to link all together?

Sonique
  • 6,670
  • 6
  • 41
  • 60

2 Answers2

1

Tables are there to store any data which you have. If you need any particular information about those entries you have to use different queries. For example assume the following product table:

+----+-------------+-------------+
| ID | ProductNAme | ProductType |
+----+-------------+-------------+
| 1  | Product A   | Type A      |
+----+-------------+-------------+
| 2  | Product B   | Type A      |
+----+-------------+-------------+
| 3  | Product C   | Type B      |
+----+-------------+-------------+

You can get the quantity of Type A Products by writing the following query (view This Link for a better understanding):

Select count(*) as CountOfProduct from Product where ProductType="Type A";

EDIT

You mentioned in your comment that one product could possibly have different types and different quantities. You can do that in one table but that looks messy. If you want this to happen you need two build a 1-Many **relationship** between two tables called product and type.

The type table could be something like this :

| TYPE_ID |   TYPE |
|---------|--------|
|       1 | Type A |
|       2 | Type B |
|       3 | Type C |

and your product table could be something like this:

| PRODUCT_ID | PRODUCTNAME | QUANTITY | TYPE_ID |
|------------|-------------|----------|---------|
|          1 |   Product A |        3 |       1 |
|          2 |   Product B |        2 |       1 |
|          3 |   Product C |        1 |       2 |
|          4 |   Product C |        5 |       3 |

**take note that type_id is the foreign key which builds the relation between these two tables. And since you can have multiple products with the same type (like product C in this example) this table will be your many table and type table will be your one table. Hence, with putting the foreign key in the many table you will establish the one to many relationship.

Now, in order to combine (or in other words to join) these two tables you will need to write a join query as following:

select ProductName,quantity,Type from Product p
inner join type t on t.type_id=p.type_id

and the result will be what you want:

| PRODUCTNAME | QUANTITY |   TYPE |
|-------------|----------|--------|
|   Product A |        3 | Type A |
|   Product B |        2 | Type A |
|   Product C |        1 | Type B |
|   Product C |        5 | Type C |

Check this link out for the fiddle

Payam
  • 479
  • 2
  • 18
  • What to do if we have `Product A` with 3 different types and we need create select on page and product may have different quantity of Types (Product A have Type 1 and 2, types in my case attributes) and they has own values? I don't understand how this example may solve this problem. – Sonique Oct 29 '14 at 14:07
  • @Sonique I have edited the answer. I hope this is what you're looking for. – Payam Oct 29 '14 at 15:28
1

If you're building an ecommerce site, this domain concept is generally known as "variant"; see here.

Typically, you have a "products" table containing the major attributes of the item, and a "variants" table, linked back to the product by product ID.

If you're only ever dealing with similar products, you can have the variant table contain columns for all attributes (e.g. size, price, colour); if you have lots of different types of variant (e.g. clothes, shoes, sports equipment), you are at the "inheritance modeling using relational databases"; see this answer.

Community
  • 1
  • 1
Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52