在数据库中混合拥有和引用关系
我已经使用 XML 和分层数据库很长时间了,其中所有权很容易确定。现在我正在尝试一个关系数据库模型,其中我有一个运行时上下文,我正在使用唯一的 id 和关系属性(例如 ProductId 等)加载实体。
我的问题是如何向该模型添加某种所有权?如果我想定义一个关系,其中目标实体是该实体的拥有父级。如何将这种差异定义为引用关系,并以最少的额外信息将其保留在 SQL 数据库中? SQL(MS)是否支持定义关系类型?
最后,我想要的是能够弄清楚当删除其他一些实体(所有拥有的实体)时要删除哪些其他实体。另外,当将此实体序列化为 XML 时,我希望将引用的实体序列化为普通 id 元素,并将拥有的实体序列化为完整的 XML。
<Notebook Id="1">
<LibraryId>5</LibraryId> <!-- Referenced entity -->
<AuthorId>6</AuthorId> <!-- Referenced entity -->
<Notes> <!-- Owned entities -->
<Note Id="2" />
<Note Id="3" />
<Note Id="4" />
</Notes>
</Notebook>
Note 实体通常与 Notebook 分开存储在其自己的带有 NotebookId 列的 SQL 表中。但如何将该列定义为拥有关系呢?我想我可以将其称为 OwnerId 或 OwningNotebookId 并仅分析列的名称。但我希望有更好的方法。你有什么建议?
I've used XML and hierarchical databases for a long time, where ownership is easy to determine. Now I'm trying out a relational database model in which I have a runtime context I'm loading entities to with unique ids and relational properties such as ProductId etc.
My question is how I go about to add some kind of ownership to this model? If I want to define a relationship where the target-entity is the owning parent of the entity. How can I define this difference to a reference relation and persist this in a SQL-database with minimal extra information? Is there any support in SQL (MS) to define the type of relationship?
In the end, what I want is to be able to figure out what other entities to delete when some other entity is deleted (all owned entities). Also when serializing this entity to XML I want to serialize the referenced entities as normal id-elements and the owned entities with full XML.
<Notebook Id="1">
<LibraryId>5</LibraryId> <!-- Referenced entity -->
<AuthorId>6</AuthorId> <!-- Referenced entity -->
<Notes> <!-- Owned entities -->
<Note Id="2" />
<Note Id="3" />
<Note Id="4" />
</Notes>
</Notebook>
The Note-entity is normally stored separated from Notebook in its own SQL-table with a NotebookId-column. But how do I define this column as a owned relationship? I guess I could call it OwnerId or OwningNotebookId and just analyze the name of the column. But I was hoping for a better method. What do you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要区别在于引用保存在一对多链接的多方。
对于图书馆和作者,参考位于笔记本表上,因为每个图书馆和作者可以有许多笔记本。
对于注释,出于同样的原因,引用位于注释表上。
下面的示例架构显示了链接。
Note 和 Notebook 之间的链接有一个“删除级联”选项,这意味着如果删除 Notebook,数据库也会自动删除该 Note。
默认选项“删除限制”将防止在子行存在时删除父行。
使 NotebookId 不为 null 意味着如果没有相应的笔记本就无法创建笔记,因此笔记生命周期始终链接到父笔记本。
The main difference is that the reference is held on the Many side of the One-to-Many link.
For Library and Author, the reference is on the Notebook table, as each Library and Author can have many notes books.
For the Notes, the reference is on the Note table for the same reason.
The sample schema below shows the links.
The link between Note and Notebook has got an "on delete cascade" option, which means that if the Notebook is deleted, the Note is deleted as well automatically by the database.
The default option "on delete restrict" will prevent a parent row from being deleted while a child row exists.
Making the NotebookId not null means that a note can't be created without a corresponding notebook so a notes lifecycle is always linked to the parent.