473,799 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Folder-and-file-like object relationship implementation. How ?

Hi All,

I'd like to develop something which has a concept like the
folders-and-files in your PC.

A folder can have subfolders and/or files. The subfolders themselves
have the same characteristics .

This characteristic can also be found in Google Newsgroup, aka Usenet.
A group can have many postings and many other groups.

I want to reapply this concept for listing contents for categories,
not unlike the categories one can find in Yahoo. For example:

The root has many categories: Electronics, Art, Entertainment, etc.
Under Electronics there are subcategories such as cameras, washing
machine and refrigerators. However, here's the twist:

When I select Electronics, I want all items under all subcategories to
be listed as well. That means I need to traverse to every descendant
categories to view all items under them.

If I start out with two tables, CategoryTable and ItemTable, how
should I design their relationship and how should the SQL query be
constructed ?

Any help in whatever form would be very much appreciated.

P.S.:

The CategoryTable, in my mind, should have at least these columns:
- categoryId (PK)
- categoryParentI d (To maintain the hierarchy structure)

The ItemTable
- itemId(PK)
- categoryId (FK pointing towards CategoryTable.c ategoryId.
Many-to-one relationship)
Jul 23 '05 #1
2 1516
Ken Loh wrote:
When I select Electronics, I want all items under all subcategories to
be listed as well. That means I need to traverse to every descendant
categories to view all items under them.

If I start out with two tables, CategoryTable and ItemTable, how
should I design their relationship and how should the SQL query be
constructed ?


SQL doesn't handle heirarchies in a very straightforward way. Oracle
and perhaps some other vendors have invented "recursive" queries to
handle this type of case, but they're not standard SQL. MySQL has no
such extension to the language.

Another method is described in Joe Celko's book "SQL For Smarties".
Instead of storing a parent id in the category table, make another table
that records all the ancestor-descendant relationships:

insert into pathenum (parent, child, pathlength) values
('Electronics', 'cameras', 1),
('cameras', 'digital cameras', 1),
('Electronics', 'digital cameras', 2),
('Electronics', 'refrigerators' , 1);
etc.

Now you can do joins to find all descendant categories of Electronics
(where parent = 'Electronics'), all immediate children (where pathlength
= 1), all ancestors (where child = 'digital cameras'), etc.

Once you have the set of categories you want, then you can do a join to
the ItemTable to get the items in all those categories.

Inserting and deleting members of the heirarchy takes some thought but
with some scribbling on paper to make sure you have covered all the
cases, you can do it.

Read Joe Celko's book for more information.

Regards,
Bill K.
Jul 23 '05 #2
Thank you Bill for your enormously helpful feedback.

Thanks also for reading btw the lines in the paragraph which I made
several major mistakes which would drive other readers towards the
opposite direction. Firstly, I missed out the word NOT when I intended
to say "need NOT traverse". Secondly, thank you for choosing to
interpret correctly that what I really meant by "all subcategories"
was actually "all descendant categories".

Once again, a big 'THANK YOU' to you ! :)

Bill Karwin <bi**@karwin.co m> wrote in message news:<d2******* **@enews1.newsg uy.com>...
Ken Loh wrote:
When I select Electronics, I want all items under all subcategories to
be listed as well. That means I need to traverse to every descendant
categories to view all items under them.

If I start out with two tables, CategoryTable and ItemTable, how
should I design their relationship and how should the SQL query be
constructed ?


SQL doesn't handle heirarchies in a very straightforward way. Oracle
and perhaps some other vendors have invented "recursive" queries to
handle this type of case, but they're not standard SQL. MySQL has no
such extension to the language.

Another method is described in Joe Celko's book "SQL For Smarties".
Instead of storing a parent id in the category table, make another table
that records all the ancestor-descendant relationships:

insert into pathenum (parent, child, pathlength) values
('Electronics', 'cameras', 1),
('cameras', 'digital cameras', 1),
('Electronics', 'digital cameras', 2),
('Electronics', 'refrigerators' , 1);
etc.

Now you can do joins to find all descendant categories of Electronics
(where parent = 'Electronics'), all immediate children (where pathlength
= 1), all ancestors (where child = 'digital cameras'), etc.

Once you have the set of categories you want, then you can do a join to
the ItemTable to get the items in all those categories.

Inserting and deleting members of the heirarchy takes some thought but
with some scribbling on paper to make sure you have covered all the
cases, you can do it.

Read Joe Celko's book for more information.

Regards,
Bill K.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5643
by: Daniel Albisser | last post by:
Hi @ll, I was wondering why I lose the connection to the mail store while retrieving information from it without calling the method store.close()! At the end I found out that the method folder.close(boolean) also close the store! Following you see the debug log of the JavaMail API: > C: QUIT
14
18351
by: Job Lot | last post by:
How can I find path of windows folder using vb.net? It could be winnt or windows depending upon the os. System.Environment.SpecialFolder enumeration does not have a constant for windows folder.
2
5693
by: Chen Zhuo | last post by:
Hi, I need to write some code to test whether a folder has read/write permission. The idea is to try to read sub-directories in this folder, if succeed, then test whether it's read-only. Any exception thrown means we don't have permission to access this folder: bool IsDirectoryAccessible(string path) { try
2
1506
by: Vic.Dong | last post by:
Hello: Is there anybody how to create hidden folder in public folder or system? I have a any problem. I cannot create hidden folder in public or system folder. Is there anybody who knows the answer to create hidden folder ?
2
4794
by: pappu | last post by:
Hello friends, I have one folder Named as MainFolder on server. I have one more folder named as subfolder inside tht main folder.Now I want to save some pictures in tht folder but at runtime I m not getting correct virtual path of tht folder. As Server.MapPath() & MapPath methods returns physical path.So How I can Access tht folder independent of where my application is insatlled. I have used following metthods but it doesnt work,
0
1278
by: Tulgaa | last post by:
When i manually create a shared folder, the shared folder was non password protection. but when i create a shared folder from a program, the shared folder is become password protected. I use following code: dim fs =GetObject("WinNT://" + SystemInformation.ComputerName + "/ LanmanServer") Dim ns = fs.create("fileshare", "My Shared folder name") ns.path = "c:\my folder\"
24
7571
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in the folder but I get the error message when the procedure attempts to delete the folder selected in the BrowseforFolderbyPath function. How can I get it to delete the folder that the user has selected in the Browse for Folder dialog?
0
1060
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
I have an exe that runs in the bin folder of the installed application root folder. I need to load some images dynamically which are in a folder called 'images' in a sub folder of the root folder. How can I access this folder at runtime from my exe running in the bin folder? Thanks.
5
2857
by: bharathreddy | last post by:
How to find the size of a folder using C# code? step1: Here take the folder which u want to find the size, then pass that folder to the recursive function name FolderSize!. DirectoryInfo dirInfo = new DirectoryInfo(strPath) ; decimal dSize = 0; //If the folder exists then only its size will be calculated if not its size will be //zero! if (dirInfo.Exists)
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.