将酒店菜单存储在数据库中

Storing a hotel menu in a database

本文关键字:数据库 存储 酒店 菜单      更新时间:2023-09-26

如何在数据库中存储餐厅菜单。假设我有一个名为hotel的数据库,其中有一个称为restaurant的表。这是模式。

restaurant(Type, Name, Description, Pictures, Address, Menu)

Type、Name、Description、Address是字符串,类似于varchar.图片为blob类型。

现在如何存储菜单?假设一家餐厅有100多种商品。每个菜单项都有一个列,这变得毫无意义。那么我该如何存储它呢?

我能用Json来克服这个问题吗?如果是,怎么办?

菜单示例:

Pasta: 5 euros
Pizza: 10 euros
.
.
.
similarly many items

这不是我想要的方式:

item(restrnt id, item, price)

以上内容适用于一家有大量菜肴的餐厅。

您有一个菜单项表,该表的一列将是外键,该外键将与restaurant表中的主键相匹配,以将菜单项与特定餐厅相关联。

如果菜单项可以在多个餐厅之间共享,请使用连接表(第三个表有两列,都是外键,一列在restaurant表上,一列位于menu_items表上。

制作一个表items(id, name, description)。这张桌子将容纳你餐厅里的所有食物。您可以随时更新它。

制作另一张表item_prices(id, item_id,restaurant_id, price, update_date)。这里item_id是您从items表中引用的项目的id。从长远来看,这个模式会很有帮助,因为它很容易维护。您只需要更新item_prices表以了解任何价格变化。

类似地,您制作restaurants(id, reatauran_name, ... )

并且更好地将图像存储在文件系统中,而不是作为blob。

我会从这样的东西开始,当然它必须经过编辑以满足您的需求。

item (id, title, description)
menu (id, restaurant_id, title, description, active_from, active_to)
menu_item (id, menu_id, restaurant_id, price)
picture (id, title, description, filename, active)
restaurant (id, type, name, description, address)
restaurant_picture (id, restaurant_id, picture_id)

使用适当的规范化模型,一家餐厅可以有几个菜单,其中可以有几个项目。

我建议您阅读数据库规范化的相关内容。