在数据库中建立用户定义的层次关系的最佳方式是什么? [英] What is the best way to model user defined hierarchical relationships in a database?

查看:114
本文介绍了在数据库中建立用户定义的层次关系的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我希望用户能够定义层次模型,但是我需要允许用户在他们定义的模型中存储数据。这有意义吗?因此,用户将能够以层次的方式创建新的单位类型,并决定如何组织这些类型的单位。一个简单的例子:在我的假设界面中,用户创建三个单元类型,树干,分支和叶子。用户然后定义它们之间的关系。叶可以存在于层次结构中的任何一个点,分支必须有一个中继线作为父节点。然后,用户可以创建这些单元类型(作为单元)的实例,并可以根据模型中定义的规则进行组织...在数据库中是否有一个很好的方法?

Essentially, I want the user to be able to define a hierarchical model, but then I need to allow the user to store data within their defined model. Does this make sense? So the users will be able to create new "unit types" to be organised in a hierarchical way and decide how units of these types are allowed to be organised. A simple example: in my hypothetical interface a user creates three unit types, trunk, branch and leaf. The user then defines the relationships between them. A leaf can exist at any point in the hierarchy, a branch must have a trunk as a parent. The user can then creates instances of these unit types (as units) and can organise them according the rules defined in their model... is there a good way of doing this in the database?

推荐答案

这是一个非常广泛的问题,但这可能会指向正确的方向。请注意,您只能将关系规则存储在数据库中。执行它们将取决于您的客户端代码。

This is an extremely broad question, but this may point you in the right direction. Note that you're only going to be able to store the relationship rules in the database. Enforcing them will be up to your client code. Try this on for size..

unit:
    unit id,
    name,

unit relationship:
    unit id,
    foreign unit id

然后,您可以按照以下方式使用您的单位关系表。

You can then use your unit relationship table in the following way..

单位ID 与其描述的单位有关。
外来单位ID 应为空。

unit id relates to the unit it's describing. foreign unit id should be nullable.

没有关系记录的单位只能存在于父权制的根源。具有 null 外国单位ID 单位可以有任何其他单位作为其父级。否则,单位必须有另一个单位作为其父级,它的类型必须是其关系中定义的类型之一记录。

A unit with no relationship records can only exist at the root of the heirarchy. A unit with a null foreign unit id can have any other unit as its parent. Otherwise, a unit must have another unit as its parent, and it's type must be one of those defined in its relationship records.

至于存储实例本身,应该是直截了当的。

As for storing the instances themselves, that should be straightforward..

instance:
    instance id,
    unit id,
    parent instance_id

我确定会有其他需要的字段(例如姓名),但我假设你得到了漂移。

I'm sure there would be other fields you'd need (name, for instance), but I assume you get the drift.

这篇关于在数据库中建立用户定义的层次关系的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆