mysql - 非结构化 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>
也就是说,有两种类型的实体(foo
s 和bar
s)遵循基本结构(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
是一个垂直键值表,它自己引用,这样嵌套就不成问题了。
我的问题与如何将foo
andbar
表链接到node
. 这种关系是一对多的(一个foo
可以有很多nodes
),所以理论上我应该foo_id
在node
表中使用 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]
.
这对我来说看起来更干净,虽然你有完全相同的问题,这是通常用于多对多的模式。
这样做的标准方法是什么?
解决方案
推荐阅读
- javascript - 我可以使用 val() 插入或编辑文件输入吗?
- mysql - 如何用 MySQL 查询替换列值
- keras - 如何创建基于批次而不是时期更新的 Keras 学习率计划
- linux - 如何在不以“特定字符”结尾的bash中显示所有用户
- ios - iOS应用提示保存密码时,如何检测用户是否选择了Not Now?
- java - 如何用 Java 编写财务方程?
- c++ - 在 C++ 中使用二叉搜索树对数组进行排序
- mysql - 如何通过 Docker 容器访问 MYSQL
- jquery - 如何将 JSON 对象数组发布到 mongodb?
- c++ - 用奇数节点首先分离链表中的偶数和奇数节点