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

时间:2014-09-18 22:59:18

标签: 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">
        <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,CategoryDe​​tails,ItemName,ItemDetails,Type,Price

但是,当然,插入一个小的可乐需要重复以前的字段。

然后我考虑将我的表分成:Categories,Items和Types,每个用外键链接回前一个(只是一个自动递增的整数ID)

但是,跟踪所有这些ID变得非常困难。我打算将来制作一个简单的应用程序,以便更容易地创建这些数据,但在我这样做之前,我希望我的表格能够稳固。

最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

我建议创建类别,项目,类型和价格。我怀疑你会有很多重复的类型,但价格不一。因此,“类型”表格可能为TypeID, TypeDescription,而价格表格为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;