Shopping Cart tables - Feedback Database Design
I am developing online shopping system.
This is how the product structure work:
There are number of categories..
Each Category has number of items.
Each Item have one or more Options
An Option can have extra(s) or without extra(s)
The following tables I have:
mysql> desc categories;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+开发者_JAVA百科-----+---------+----------------+
| cat_id | int(11) | NO | PRI | NULL | auto_increment |
| company_id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
Eg: 12, 2, "Google"
Items Table:
mysql> desc items;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| item_id | int(11) | NO | PRI | NULL | auto_increment |
| cat_id | int(11) | NO | | NULL | |
| name | varchar(150) | NO | | NULL | |
| description | varchar(150) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Eg: 2, 12, "Item 1", "Desc... 1"
Eg: 3, 12, "Item 2", "Desc... 2"
Options Table
mysql> desc items_options;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| option_id | int(11) | NO | PRI | NULL | auto_increment |
| item_id | int(11) | NO | | NULL | |
| name | varchar(150) | NO | | NULL | |
| price | decimal(6,2) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
Eg: 45, 2, "Regular", "2.20"
Eg: 46, 3, "Small", "1.20"
Eg: 47, 3, "Large", "2.20"
Extras Table:
mysql> desc items_options_extras;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| extra_id | int(11) | NO | PRI | NULL | auto_increment |
| option_id | int(11) | NO | | NULL | |
| name | varchar(150) | NO | | NULL | |
| price | decimal(6,2) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
Eg: 64, 47, "With Bag", 0.10"
Is this good database design? What could be improved?
I did not create a relationship table, is this necessary? If so, im not sure how do I create a relationship table.
At the moment I use multiple SELECT queries to get the relationship between those tables, like this below:
<?php
$q = mysql_query("SELECT item_id, name FROM items where cat_id = '3'");
while($row = mysql_fetch_assoc($q)) {
echo $row['name'];
$q2 = mysql_query("SELECT price FROM items_options_extras where item_id =" . $row['item_id']);
while($row2 = mysql_fetch_assoc($q2))
echo $row2['price'];
}
}
?>
When I want to delete an Item and including options, I use similar php code like above.
Edit: Forgot to add Options table
Edit: Updated some data example.
Consider that an Item may belong to multiple Categories.
Here are things to consider:
- If the price of an item changes with the category, then the ItemPrice column goes into the CategoryItems table. If not, it goes into the Items table.
- If the price of an option changes with the item, then the OptionPrice column goes into the ItemOptions table. If not, it goes into the Option table.
精彩评论