Groups | Blog | Home
all groups > sql server (microsoft) > may 2006 >

sql server (microsoft) : Hierarchal Table Design SQL 2005


Maxus
5/17/2006 11:48:23 PM
Hi People,

I'm in the lucky position of building a database from scratch for my
company in SQL 2005. We have a situation where we have customers and
each customer can have a child customers and so on and on.

So the best way I found of doing this was putting everything in one
table called "Customer" each row stores a parentId that links to the ID
of the parent customer. If the customer doesn't have a parent its ID
its parent ID is set to match its own ID (So I can use a relationship
to prevent bad id's from being added). This allows me to use the new
SQL 2005 recursive functions.

Is this a good way of solving the requirement or does anyone have a
better way of doing it? I eventually want to do the same thing with the
items table where each item can be a child of another but it currently
has over 2mil+ records and I'm worried this could slow everything down.

Thanks for you help!
-Alex
Maxus
5/18/2006 6:04:09 PM
Hi JB,

Thanks for the response. So if I use null, can still maintain the
integrity of my ParentId to id relationship? Originally I was using 0
as a parent indicator. But because there is no parent customer with 0
the relationship was violated. But there is obviously nothing crazy
with the way I'm doing it? (I suspect thats hard to say with out
seeing the data :))

Thanks heaps!
-Alex
John B
5/19/2006 12:00:00 AM
[quoted text, click to view]
Yes, if you use null you can still maintian ref integrity if the col
(parentId) allows nulls (which it should IMO).
Doesnt sound crazy to me :)
[quoted text, click to view]
John B
5/19/2006 12:00:00 AM
[quoted text, click to view]
Thats the way I do hierarchical data except I leave the parentid null
for "top" level elements.

[quoted text, click to view]
It all depends on how you are going to be accessing the data.

Do a couple of tests with your typical data access strategy with some
test data in this configuration and you should soon see if it is going
to work or present problems.

Mike C#
5/20/2006 11:07:22 PM
Celko has an excellent book on the subject - "Trees and Hierarchies in SQL".

(Message for Joe: make the referral check payable to Mike C.)

[quoted text, click to view]

Maxus
5/21/2006 8:41:12 PM
Hi Guys!

Thanks heaps guys for your help on this one!

Thanks
-A
AddThis Social Bookmark Button