Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing an E-Commerce Database - MySQL

I'm doing an e-commerce project and am confused about the database design for storing products. There are 3 ways I've speculated the database can be made:

1. There can be separate tables for each product category.

Table: Categories
------------------
cat_ID
cat_name

Table: Sub_Categories
---------------------
sub_cat_ID
categories_cat_ID
sub_cat_name

Table: Books
-------------
book_ID
sub_categories_sub_cat_ID
book_title
book_author
book_ISBN
book_price
etc

Table: Clothes
---------------
clothes_ID
sub_categories_sub_cat_ID
clothes_name
clothes_color
clothes_size
clothes_description
clothes_price
etc

Table: Perfumes
----------------
perfumes_ID
sub_categories_sub_cat_ID
perfume_name
perfume_size
perfume_weight
perfume_description
perfume_price
etc

2. Group all products together in one table and allow some values to be null

Table: Categories
------------------
cat_ID
cat_name

Table: Sub_Categories
---------------------
sub_cat_ID
categories_cat_ID
sub_cat_name

Table: Products
---------------
product_ID
sub_categories_sub_cat_ID
title
description
price
author (can be null for everything except books)
size
weight (can be null for everything except perfumes)
ISBN (can be null for everything except books)
color (can be null for everything except clothes)
etc

3. Group similar column fields together in a table called products, and provide separate tables for specific data.

Table: Categories
------------------
cat_ID
cat_name

Table: Sub_Categories
---------------------
sub_cat_ID
categories_cat_ID
sub_cat_name

Table: Products
----------------
product_ID
sub_categories_sub_cat_ID
title
description
price

Table: Books
-------------
products_product_id
sub_categories_sub_cat_ID
author
publisher
ISBN

Table: Perfumes
----------------
products_product_id
sub_categories_sub_cat_ID
size
weight

Table: Clothes
--------------
products_product_id
sub_categories_sub_cat_ID
color
size (this can be a one to many relationship to cater to multiple sizes of one product?)

I would really appreciate enlightenment, thank you

like image 772
a7omiton Avatar asked Jan 31 '13 12:01

a7omiton


People also ask

Is MySQL good for ecommerce?

The scalability of MySQL makes it a very useful application for ecommerce applications. Businesses, of course, plan on growing and this platform can grow right along with them. MySQL is a platform that can be integrated with many other technologies besides ecommerce.

What DB is used for ecommerce?

Ecommerce transactions are not ideally suited for document databases, however. Relational databases work much better for transactional data. MongoDB, CouchDB, and Redis are examples of document databases that are used by stores.

What's an example of good ecommerce database design?

Relational e-commerce database example Examples include MySQL, PostgreSQL, MariaDB, Microsoft SQL, Amazon RDS, and Azure SQL Database. Many e-commerce sellers use a relational database design centered around the following tables: products table, customers table, and orders table.


2 Answers

I think it depends on the products to use method 1 or 2. I would never use method 3.

If your products are completly different like Books, Perfumes and Clothes I would use method 1

One sidenote: Why use 2 tables for your catagories? Use one table and add a column Parent_ID that way you can use unlimited subcatagories in the future.

for example:

table: categories

|id|description|parentid|
|1 |books      |NULL    |
|2 |clothes    |NULL    |
|3 |perfumes   |NULL    |
|4 |Sci-Fi     |1       |
|5 |Comedy     |1       |
|6 |Jeans      |2       |
|7 |Sweater    |2       |
|8 |Underwear  |2       |
|9 |Long sleeve|7       |
|10|Roses      |3       |
  • Books, Clothes en Perfumes do not have a parent (those are the main categories).
  • Sci-Fi and Comedy is a subcategory of Books (ID 1).
  • Jeans, Sweater and Underwear is a subcategory of clothes (ID 2).
  • Long Sleeve is a subcategory of Sweater (ID 7).
  • Roses is a subcategory of perfumes (ID 3).
like image 36
AgeDeO Avatar answered Oct 11 '22 09:10

AgeDeO


I assume a product can belong to many categories, and a category (obviously) has many products in it. This relationship is called a many-to-many relation.

In this instance, you would have three tables: categories, products, and categories_products. The first two tables are self-explanatory. The third table stores a relation between the two with two foreign keys. The tables would look like this:

CREATE TABLE `categories` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `parent_id` int(10) unsigned DEFAULT NULL,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `categories_products` (
  `category_id` int(10) unsigned NOT NULL,
  `product_id` int(10) unsigned NOT NULL,
  KEY `category_id` (`category_id`),
  KEY `product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `products` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `categories_products`
  ADD CONSTRAINT `categories_products_ibfk_2`
    FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
    ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `categories_products_ibfk_1`
    FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)
    ON DELETE CASCADE ON UPDATE CASCADE;

Obviously these are the table schemas at their simplest. You will need to add your additional columns to the categories and products table—I’ve only included the columns relavant to the relation.

EDIT: I also added a parent_id column to the categories table for nesting categories. It’s generally a bad idea to create a separate sub_categories table—what happens if you want to make a sub-category a top-level category? Or vice versa? You’re buggered for want of a better phrase.

like image 154
Martin Bean Avatar answered Oct 11 '22 10:10

Martin Bean