购物车表 - 反馈数据库设计
我正在开发网上购物系统。
这就是产品结构的工作原理:
有多个类别。
每个类别都有多个项目。
每个项目都有一个或多个选项
一个选项可以有额外的或没有额外的
我有以下表格:
mysql> desc categories;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| cat_id | int(11) | NO | PRI | NULL | auto_increment |
| company_id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
例如:12、2、“Google”
项目表:
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 | |
+-------------+--------------+------+-----+---------+----------------+
例如:2、12、“项目 1”、 “描述... 1”
例如:3、12、“项目 2”、“描述... 2”
选项表
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 | |
+-----------+--------------+------+-----+---------+----------------+
例如:45、2、“常规”、“2.20”
例如: 46, 3, "Small", "1.20"
例如:47, 3, "Large", "2.20"
附加表:
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 | |
+-----------+--------------+------+-----+---------+----------------+
例如:64, 47, "With Bag", 0.10"
这是好的数据库设计吗?还有哪些可以改进的地方?
我没有创建关系表,如果有必要,我不确定如何创建关系表。
目前我使用多个 SELECT 查询来获取关系表 。这些表,如下所示:
<?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'];
}
}
?>
当我想要删除项目并包含选项时,我使用类似上面的 php 代码
编辑:忘记添加选项表
编辑:更新了一些数据示例。
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 |
+------------+--------------+------+-----+---------+----------------+
| 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑一个项目可能属于多个类别。
以下是需要考虑的事项:
Consider that an Item may belong to multiple Categories.
Here are things to consider: