用于目录结构的数据结构? [英] Data structure used for directory structure?
问题描述
这就是我所做的:
数据库中的每个记录都有两个字段:ID和ParentID。 ID为4-5个字符(Base36,a-z:0-9或类似的东西)。父ID是父级完整结构的连接...
所以...
此结构:
根
文件夹1
文件夹2
文件夹3
文件夹4
文件夹5
文件夹6
将如下所示:
ID ParentID名称
0000 NULL ROOT
0001 0000 Folder1
0002 0000 Folder2
0003 00000002 Folder3
0004 0000 Folder4
0005 00000004 Folder5
0006 000000040005 Folder6
I像这样的结构,因为如果我需要找到一个文件夹下的所有文件,我可以做一个查询,如:
SELECT * FROM文件夹WHERE ParentID LIKE'0000%' - 查找Folder1下的所有文件夹
要删除文件夹和所有其孩子:
DELETE FROM文件夹WHERE ID ='0004'和ParentID LIKE'00000004%'
移动文件夹及其子,您必须将所有使用相同父级的记录更新为新的父级。
而且我不想使用文件夹或子文件夹级别
对此的明显限制是子文件夹的数量限制为您的ParentID字段的大小。 / p>
I'm making a program which the user build directories (not in windows, in my app) and in these folders there are subfolders and so on; every folder must contain either folders or documents. What is the best data structure to use? Notice that the user may select a subfolder and search for documents in it and in its subfolders. And I don't want to limit the folders or the subfolders levels.
This is what I do:
Every record in the database has two fields: ID and ParentID. IDs are 4-5 characters (Base36, a-z:0-9 or something similar). Parent IDs are a concatenation of the parent's complete structure...
So...
This structure:
Root
Folder1
Folder2
Folder3
Folder4
Folder5
Folder6
Would be represented like this:
ID ParentID Name
0000 NULL ROOT
0001 0000 Folder1
0002 0000 Folder2
0003 00000002 Folder3
0004 0000 Folder4
0005 00000004 Folder5
0006 000000040005 Folder6
I like this structure because if I need to find all the files under a folder I can do a query like:
SELECT * FROM Folders WHERE ParentID LIKE '0000%' -- to find all folders under Folder1
To delete a folder and all its children:
DELETE FROM Folders WHERE ID='0004' AND ParentID LIKE '00000004%'
To move a folder and its children, you have to update all the records that use the same parent, to the new parent.
And I don't want to linit the folders or the subfolders levels
An obvious limitation to this is that the number of subfolders are limited to the size of your ParentID field.
这篇关于用于目录结构的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!