购物车表 - 反馈数据库设计

发布于 2024-11-03 01:25:12 字数 3343 浏览 0 评论 0原文

我正在开发网上购物系统。

这就是产品结构的工作原理:

有多个类别。

每个类别都有多个项目。

每个项目都有一个或多个选项

一个选项可以有额外的或没有额外的

我有以下表格:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-11-10 01:25:12

考虑一个项目可能属于多个类别。

在此处输入图像描述

以下是需要考虑的事项:

  • 如果商品的价格随类别而变化,则 ItemPrice 列将变为进入 CategoryItems 表。如果没有,它将进入 Items 表。
  • 如果选项的价格随项目而变化,则 OptionPrice 列将进入 ItemOptions 表。如果没有,它将进入选项表。

Consider that an Item may belong to multiple Categories.

enter image description here

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文