我正在尝试创建一个数据库(使用PHP和MYSQL,使用XAMPP),它存储有关各种快餐店及其分支机构的信息,以及在其分支机构提供的菜单。我在我的数据库中创建了一些表详细介绍快餐店及其分店,菜单的类别和子类别及其在表格中提供的项目的信息。表格的提示/样本如下所示。
Restaurants
+-------------+----------------+
| RestaurantID| RestaurantName |
+-------------+----------------+
| 1 | PizzaHut |
| 2 | BurgerKing |
| 3 | KFC |
| 4 | SubWay |
+-------------+----------------+
Branches
+---------------+----------------+
| RestaurantName| BranchAddress |
+---------------+----------------+
| KFC | XXX-1234 |
| KFC | AAA-1234 |
| KFC | YYY-1234 |
| BurgerKing | DDD-1234 |
+---------------+----------------+
Categories
+---------------+----------------+
| BranchAddress | Categories |
+---------------+----------------+
| XXX-1234 | Fries |
| AAA-1234 | Burgers |
| YYY-1234 | Drinks |
| DDD-1234 | Burgers |
+---------------+----------------+
SubCategories
+---------------+----------------+
| Categories | SubCategories |
+---------------+----------------+
| Fries | Cheese |
| Burgers | Chicken |
| Drinks | Carbonated |
| Burgers | Beef |
+---------------+----------------+
Items
+---------------+----------------+
| SubCategories | Items |
+---------------+----------------+
| Cheese | CheeseFries |
| Chicken | Zinger |
| Carbonated | Pepsi |
| Beef | Whopper |
+---------------+----------------+
从上面可以看出,每个餐厅可以有几个分支,每个分支可以有几个类别,每个类别可以有几个子类别,然后可以有几个项目。分支可以有相同的类别和/或子类别但它们内部有不同的项目。
我不认为集中的类别,子类别和项目表在这种情况下会起作用,因为每个餐馆的每个分支都是它自己独立的个体实体,这需要它自己的一组类别,子类别和项目如果是这样,我如何创建表格,以及设置必要的外键?
Categories
+----------------+
| Categories |
+----------------+
| Drinks |
| Burgers |
| Fries |
| SetMeals |
+----------------+
SubCategories
+----------------+----------------+
| Category | SubCategories |
+---------------+-----------------+
| Drinks | Carbonated |
| Drinks | Carbonated |
| Drinks | Carbonated |
| Drinks | Non-Carbonated |
| Burgers | Beef |
| Burgers | Beef |
| Burgers | Chicken |
+----------------+----------------+
SubCategories
+----------------+----------------+-------------+
| Category | SubCategories | Items |
+---------------+-----------------+-------------+
| Drinks | Carbonated | Pepsi |
| Drinks | Carbonated | Root Beer |
| Drinks | Carbonated | Cola |
| Drinks | Non-Carbonated | Orange Juice |
| Burgers | Beef | Whopper |
| Burgers | Beef | Cheese Burger|
| Burgers | Chicken | McChicken |
+----------------+----------------+--------------+
当然,我可能是错的,并且可以在这种情况下使用集中表。如果是这样,我可以提供一些关于如何进行/或设置表格的示例和/或说明吗?
我想到了以下解决方案:
Restaurants
+-------------+----------------+
| RestaurantID| RestaurantName |
+-------------+----------------+
| 1 | PizzaHut |
| 2 | BurgerKing |
| 3 | KFC |
| 4 | SubWay |
+-------------+----------------+
Branches
+---------------+----------------+
| RestaurantName| BranchAddress |
+---------------+----------------+
| KFC | XXX-1234 |
| KFC | AAA-1234 |
| KFC | YYY-1234 |
| BurgerKing | DDD-1234 |
+-------------+------------------+
Categories
+----------------+----------------+----------------+
| RestaurantName | BranchAddress | Category |
+----------------+-----------------+----------------+
| KFC | XXX-1234 | Fries |
| KFC | AAA-1234 | Burgers |
| KFC | YYY-1234 | Drinks |
| BurgerKing | DDD-1234 | Burgers |
+----------------+----------------------------------+
SubCategories
+----------------+----------------+----------------+----------------+
| RestaurantName | BranchAddress | Category | SubCategories |
+----------------+-----------------+---------------+----------------+
| KFC | XXX-1234 | Fries | Cheese |
| KFC | AAA-1234 | Burgers | Chicken |
| KFC | YYY-1234 | Drinks | Carbonated |
| BurgerKing | DDD-1234 | Burgers | Beef |
+----------------+---------------------------------+----------------+
Items
+----------------+----------------+----------------+----------------+-------------+
| RestaurantName | BranchAddress | Category | SubCategories | Items |
+----------------+-----------------+---------------+----------------+-------------+
| KFC | XXX-1234 | Fries | Cheese |CheeseFries |
| KFC | AAA-1234 | Burgers | Chicken |Zinger |
| KFC | YYY-1234 | Drinks | Carbonated |Pepsi |
| BurgerKing | DDD-1234 | Burgers | Beef |Whopper |
+----------------+---------------------------------+----------------+-------------+
虽然上面的表格给每个餐厅及其分支机构提供了各自的类别,子类别,项目,但它首先打破了将它们分开的整个目的,而且非常愚蠢。
编辑:我试图对上面的表进行非规范化,以便我可以使用
进行查询$query="SELECT * FROM Items WHERE Restaurantname='BurgerKing' AND
BranchAddress='XXX-1234' AND Category='Fries';
$result=mysqli_query($cxn,$query) or die("Error");
while($row=mysqli_fetch_assoc)
{
extract($row);
echo "$Item";
}
注意:我计划将数据库扩展到未来包括非快餐店甚至非F& B相关商店,因此各种表格(类别/子类别和项目)将会必须能够包括非F& B相关商店的类别(例如电子产品/书籍等)
我为非常长的问题道歉,并希望得到任何建议和帮助。
谢谢!
答案 0 :(得分:0)
不确定为什么你要正常化。您可以从分类和子类别中取出与分支相关的列,从子类别中取出类别名称等。通过将它们留在您中,您可以复制大量数据,而不是传统上使用关系数据库的方式!您还应该按ID而不是通过名称关联这些表,因为加入时的比较更快。
您可以考虑使用多对多链接表将项链接到分支。这样,您就可以满足您的要求,即能够将类别/子类别的特定项目与分支相关联,而无需关联整个类别/子类别。
CREATE TABLE item_link (
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
branch_id INT(11) NOT NULL,
item_id INT(11) NOT NULL,
KEY composite (`branch_id`, `item_id`)
);
请参阅我为您制作的fiddle详细说明了我对架构的建议更改,并举例说明了如何使用此新表来显示分支的菜单/库存。