在数据库中存储目录结构 [英] Storing a directory structure in database

查看:64
本文介绍了在数据库中存储目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,用户可以具有目录结构,该目录结构在子文件夹中包含文件夹和文件.哪一种是存储此类数据的最佳方法?另外,哪个数据库提供了最佳方法?

In my rails app a user can have a directory structure which has folders and files in sub-folders. Which is the best way to store such data ??
Also, which database offers best way to do so?

推荐答案

通过使表具有自引用性,您可以使用任何SQL数据库将目录树存储在单个表中.一个很好的例子是 Windows Installer的目录表,您将在其中看到这样的结构:

You can store a directory tree in a single table using any SQL database, by making the table self-referential. A good example is the Windows Installer's Directory table, where you will see a structure like this:

  • 目录 =主键ID字段,通常为整数
  • Directory_Parent =外键" ID字段,它指向同一表中另一个目录的ID
  • =包含目录/文件夹名称的字符串
  • Directory = primary key id field, typically an integer
  • Directory_Parent = "foreign key" id field, which points to the id of another Directory in the same table
  • Value = string containing the directory/folder name

您的文件表将具有引用目录ID的外键.要找到完整路径,您必须遵循整个链,并从末尾(右)开始构建路径,然后将每个父目录附加到前部(左).例如,文件将指向目录ID为'4'且值为'subfolder',然后获取父级的值'folder',然后再次获取父级的值,直到到达根为止,并创建类似于/根/文件夹/子文件夹/文件名.

Your file table would then have a foreign key referencing the Directory id. To find the full path, you must follow it up the chain and build up the path from the end (right), tacking each parent directory onto the front (left). For example, the file would point to Directory id '4' with the Value 'subfolder', then you fetch the parent's value 'folder', then the parents value again until you get to the root, creating a path like /root/folder/subfolder/filename.

这篇关于在数据库中存储目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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