首页 > 解决方案 > 非结构化 XML 的数据库模式

问题描述

假设我有以下 XML:

<fooObjects>
    <id>1</id>
    <name>something</name>
    <randomProperty>
        <subRandomProperty>5</subRandomProperty>
    </randomProperty>
    <anotherRandomProperty>1234</anotherRandomProperty>
    ...
</fooObjects>
<barObjects>
    <id>1</id>
    <url>elsething</url>
    <randomProperty>
        <subRandomPropertyX>28</subRandomProperty>
        <subRandomPropertyY>15</subRandomProperty>
    </randomProperty>
    ...
</barObjects>

也就是说,有两种类型的实体(foos 和bars)遵循基本结构(foo 的 id 和 name,bar 的 id 和 url),然后有一堆可以有子节点的随机属性。

我想创建一个 MySQL 模式来存储这种数据。我想我需要以下表格:

foo: id [int], name [string],

bar: id [int], url [string],

node: id [int], name [string], value [string], parent_id [int, foreign key referencing node(id)].

wherenode是一个垂直键值表,它自己引用,这样嵌套就不成问题了。

我的问题与如何将fooandbar表链接到node. 这种关系是一对多的(一个foo可以有很多nodes),所以理论上我应该foo_idnode表中使用 a (对于 也是如此bar)。但这对我来说看起来很奇怪,因为 a 不能同时node属于 afoo和 a bar。此外,不可能node有一个parent同时属于afoo或abar的a。也就是说,这三个属性以某种方式排除在外。

另一种选择是再创建两个表:

node_foo_rel: id [int], id_node [int], id_foo [int],

node_bar_rel: id [int], id_node [int], id_bar [int].

这对我来说看起来更干净,虽然你有完全相同的问题,这是通常用于多对多的模式。

这样做的标准方法是什么?

标签: mysql

解决方案


推荐阅读