所以我有这个 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;
关于mysql - 如何组织 MySQL 数据库来存储此 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923776/