在关系数据库中存储文件夹层次结构

发布于 2024-07-16 23:41:56 字数 312 浏览 14 评论 0原文

我有代表文件夹的对象,我想知道它们是否应该在数据库中表示。

一方面,最简单的方法似乎是不表示文件夹对象,而只存储文件夹中包含的对象的路径值。 我看到的问题是,您无法保留其后代不包含任何项目的文件夹,这没什么大不了的。 另外,我不清楚如何加载要显示的文件夹层次结构(例如在 TreeView 中)而不预先将所有内容加载到内存中,这可能是一个性能问题。

另一种方法是使用一个“文件夹”表来引用其父文件夹。 这似乎应该可行,但我不确定如何允许具有相同名称的文件夹,只要它们不共享父级。 这是否应该是数据库应该关心的事情,或者是我应该在业务逻辑中强制执行的事情?

I have objects representing folders and I'm wondering if they should be represented in the database.

On the one hand it seems like the easiest way would be to not represent folder objects and just store a path value for objects contained in a folder. Problems I see with this is that you can't persist an folder whose descendants do not contain any items, which isn't too big of a deal. Also I don't have a clear idea of how to load the folder hierarchy to display (such as in a TreeView) without loading everything into memory upfront, which would probably be a performance issue.

The alternative is to have a "Folder" table with references to its parent folder. This seems like it should work, but I'm unsure how to allow folders with the same name as long as they do not share a parent. Should that even be something the DB should be concerning itself with or is that something that I should just enforce in the the business logic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

向日葵 2024-07-23 23:41:56

这个想法是这样的(自我引用):

CREATE TABLE FileSystemObject ( 
    ID int not null primary key identity,
    Name varchar(100) not null,
    ParentID int null references FileSystemObject(ID),
    constraint uk_Path UNIQUE (Name, ParentID),
    IsFolder bit not null
)

The idea is something like this (self-referencing):

CREATE TABLE FileSystemObject ( 
    ID int not null primary key identity,
    Name varchar(100) not null,
    ParentID int null references FileSystemObject(ID),
    constraint uk_Path UNIQUE (Name, ParentID),
    IsFolder bit not null
)
↙温凉少女 2024-07-23 23:41:56

查看此页面中间的 ERD。 将层次结构分解到单独的表中允许您支持多种分类法。

Take a look at the ERD in the middle of this page. Factoring out the hierarchy into a separate table permits you to support multiple taxonomies.

不美如何 2024-07-23 23:41:56

SQL Server 有一个支持分层结构的hierarchyid 数据类型。 请注意,仅适用于完整版本。

SQL Server has a hierarchyid data type that has support for hierarchical structures. Works only in the full version, mind you.

是伱的 2024-07-23 23:41:56

首先问问自己,将层次结构保留在数据库中的目的是什么,以及您可以从中获得什么功能。 然后要求考虑执行此操作所需的工作和维护。

如果您只是使用它来填充树控件,则有一些内置控件直接针对文件夹系统。 这对你来说会更好吗? 通过将其存储在数据库中,您还会获得除此之外的其他东西吗? 您计划如何使数据库与可以在数据库外部更改的实际文件夹系统保持同步? 除非您提供虚拟文件系统,否则最好直接针对数据库中存储的相关路径的真实文件系统。

First ask yourself what the purpose is of keeping the hierarchy in the database and what functionality you gain by that. Then ask consider the work and maintenance that goes into doing it.

If you're simply using it to fill a tree control, there are built-in controls that go directly against the folder system. Would that work better for you? Do you gain something beyond that by storing it in the database? How do you plan to keep the database in sync with the actual folder system, which can be changed outside of the DB? Unless your providing a virtual file system it may be better to just go directly against the real thing with relevant paths stored in the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文