1

I am working on a project where I need to design a table like directory management. I am just a beginner in DB, so I really need your guys' expertise. my current thought for database design can be illustred as below:

 id   name       type        create_time       parent_id
  1   folder1    folder      2011-2-3             
  2   folder2    folder      2011-2-3             1
  3   folder3    folder      2011-2-3             1
  4   folder4    folder      2011-2-3             1
  5   file1      file        2011-2-3             4
  ....

as you can see the parent_id is pointing its own table PK id. the constrain complys with the real world like folders can contain folders, files can not has children,etc...

most used query scenario would be:

  1. given an id, finds its all subfiles(include folder and file), for each file, indicates whether it has children or not.

  2. given an id, finds its all ancestors id(parents, grandparent...)

considering a large scale application, questions:

  1. do you think the schema design reasonable? if not,please suggest one.
  2. for those two scenarios, how can I write robot queries that won't suffer the performance.

thanks for any help.

4
  • if parent_id is FK to the same table 0 is not allowed and has to be NULL Commented Jul 8, 2011 at 12:18
  • Identify the queries you want to write and create indices on same column (in same order) as you use in the where clause. Commented Jul 8, 2011 at 12:19
  • please dont need to conside that...let us make it to 1 Commented Jul 8, 2011 at 12:19
  • 1
    no, folder1 cannot be contained in itself!! has to be NULL or you create a record called ROOT with ID = 0 Commented Jul 8, 2011 at 12:20

2 Answers 2

1

You can consider this way:

id    name     -----   type    ----    create_time       parent_id
  1   folder1  ---  folder  --- 2011-2-3               
  2   folder2  ---  folder    ---  2011-2-3       -----      1
  3   folder3   --- folder    ---  2011-2-3      -----       2-1
  4   folder4  ---  folder    ---  2011-2-3       -----      3-2-1
  5   file1    -----  file      -----  2011-2-3    -----         4-3-2-1

Put the hierarchy info in parent_id which states all its ancestors.

When you wanna add a new folder under folder4 for e.g. You can simply attach 4- to the parent_id value of folder4 and make it your new folder's parent_id.

In this way you dont have to recursively find out all the ancestors.

Sign up to request clarification or add additional context in comments.

Comments

0

Architecture with parent_id isn't good for listing all parent nodes and all child nodes - you will need a recursive procedure to do this.

take a look to this article http://www.sitepoint.com/hierarchical-data-database-2/ , only problem is adding records - but can be simplified via triggers

For correct indexes see comment from Davide Piras

2 Comments

the article refered is really useful, I am thinking taking this approach. 'but can be simplified via triggers' ?? could you please elaborate a bit about this. thank you for your answer.
triggers ( MySQL 5.0+ ) - it is procedure which is called when some kond of operation is executed on table - in your case this will be a trigger on insert or on update, trigger will recalculate all left and right indexes - try to find out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.