单个MySQL行中的多个项目?

1 php mysql

使用MySQL + PHP,我想在MySQL数据库中food_item为一个餐馆存储几个order.

我有两张桌子:orders&food_items:

许多food_item_ids将被存储为单个order.

我只是想知道,food_item_id在orders_table中存储s 的最佳方法是什么?

我应该将每个存储food_item_idorders表格的单独一行中,还是将所有特定订单food_item_ids放在一行中?

pax*_*blo 6

如果你想要3NF(你应该除非并且直到你发现性能有问题),你应该:

  • 将订单ID存储在food_item表的每一行中(如果food_items是按订单表);
  • 或使用多对多关系(如果food_items只是您可以订购的东西).

使用第一个选项,这样的东西就足够了:

Orders:
    order_id
    other stuff
FoodItems:
    order_id
    item_id
    other stuff.
Run Code Online (Sandbox Code Playgroud)

对于第二个选项,单独的表提供了多对多关系:

Orders:
    order_id
    other stuff
FoodItems:
    item_id
    other stuff.
FoodItemsInOrder:
    order_id
    item_id
    count
Run Code Online (Sandbox Code Playgroud)

在我看来,所有数据库表都应该以第三范式设计.一旦你的表变得如此之大以至于性能可能成为一个问题(它必须非常大),那么你就可以开始考虑对速度进行去标准化.

对于一家餐馆来说,我无法想象订单和食品项目表的大小足以保证去标准化.