我正在尝试创建一个数据库(使用 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";
}
注意:我计划将来扩展数据库以包括非快餐店甚至非餐饮相关的商店,因此各种表(类别/子类别和项目)必须能够包括非餐饮的类别相关商店(例如电子产品/书籍等)
对于非常长的问题,我深表歉意,并感谢我能得到的任何建议和帮助。
谢谢!