如何组织MySQL数据库来存储这些XML数据

如何组织MySQL数据库来存储这些XML数据,mysql,xml,Mysql,Xml,我有一个XML文件: <menu> <!-- Drinks --> <category name="Drinks"> <item name="Coke"> <type desc="0,5L" price="1"/> <type desc="1,0L" price="2"/> </item> <item name="Pepsi">

我有一个XML文件:

<menu>

<!-- Drinks -->
<category name="Drinks">
    <item name="Coke">
        <type desc="0,5L" price="1"/>
        <type desc="1,0L" price="2"/>
    </item>
    <item name="Pepsi">
        <type desc="0,5L" price="1"/>
        <type desc="1,0L" price="2"/>
    </item>
    <item name="Mountain Dew">
        <type desc="1,0L" price="2"/>
    </item>
</category>

<!-- Pizza -->
<category name="Pizza" details="Small (6 slices) - Medium (8 slices) - Large (10 slices) - XXL (12 slices)">

    <item name="Spicy Samba" details="Carioca sauce, Italian style sausage, ham, onions, fresh tomatoes and Italian style three-cheese blend">
        <type desc="Large Original" price="15"/>
        <type desc="Large Stuffed" price="17"/>
        <type desc="Large Thin" price="16"/>
    </item>

    <item name="Sausage and Pepperoni">
        <type desc="Large Original" price="15"/>
        <type desc="Large Stuffed" price="17"/>
        <type desc="Large Thin" price="16"/>
    </item>

</category>


<!-- Chicken -->
<category name="Chicken">

    <item name="Piri Piri Chicken Wings (6)"><type price="5.5"/></item>
    <item name="Plain Roasted Chicken Wings (6)"><type price="4.5"/></item>

</category>

</menu>

我想建立一个MySQL数据库来存储这些数据

首先,我考虑了一个包含以下列的大型表:RestaurantID、CategoryName、CategoryDetails、ItemName、ItemDetails、Type、Price

但当然,插入一个小可乐需要大量重复前面的字段

然后,我考虑将我的表分为:类别、项和类型,每个表都用一个外键链接回上一个表(只是一个自动递增的整数ID)

但是,跟踪所有这些ID变得非常困难。我确实打算在将来制作一个简单的应用程序,使创建这些数据变得更加容易,但是在我这样做之前,我希望我的表是可靠的


最好的方法是什么?

我建议创建类别、项目、类型和价格。我想你们会有很多重复的类型,但价格会有所不同。因此,Types表可能有
TypeID,TypeDescription
,Prices表可能有
ItemID,TypeID,Price
,具有复合键
(ItemID,TypeID)

只要数据易于读取,它就不会以原始形式存在。但是,当您使用任何复杂程度的标准化数据时,这是非常常见的。这就是我们要问的。编写查询以使数据可读将相当简单:

SELECT c.CategoryName
     , c.CategoryDetails
     , i.ItemName
     , i.ItemDetails
     , t.TypeDescription
     , p.Price
  FROM categories c
  JOIN items      i ON c.CategoryID = i.CategoryID
  JOIN prices     p ON i.ItemID = p.ItemID
  JOIN types      t ON p.TypeID = t.TypeID;