views:

407

answers:

2

I'm not aware how deep my tree will be. So I think the NSM is fit for me, reading some docs. In sql, this model suppose I'm using an integer value as primary key. I thought to create a twin table only to store the ints (PK,left,righ) connected by a relation one-to-one with the real table. Things are complicating and it is a waste of space disk, especially when the server is not mine and I have to pay each megabyte. Help!!

UPDATE

Excellent! Fabolous!! Thanks Macka and Bill, I could skip reading a whole book, for now. Celko is a future order on Amazon. ;-)

+3  A: 

It doesn't matter what type your primary key is as the left/right values will still be integers. eg.

CREATE TABLE [dbo].[Demo](
    [ID] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_Demo_ID]  DEFAULT (newid()),
    [Name] [varchar](50) NOT NULL,
    [Lft] [int] NOT NULL,
    [Rgt] [int] NOT NULL,
 CONSTRAINT [PK_Demo] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

--add some test data

INSERT INTO demo(name,lft,rgt)
SELECT 'node1',1,6
UNION
SELECT 'node2a',2,3
UNION
SELECT 'node2b',4,5

--check it works

SELECT *
FROM demo
WHERE lft>=2
ORDER BY lft
Macka
thanks Macka. I'm going to do some query with fake data. Do you have any links where I can find an implementation ? I'm fresh with NSM, i need to improve my knowledge about it..
Marco Mangia
I'll just add that you want to make sure that you have proper constraints on your columns. For example, if you are not going to use a PK on lft and rgt at least add a unique index on it and also add a check constraint that lft < rgt
Tom H.
Joe Celko (who devised this) wrote a book called SQL for Smarties which covers this and some other cool ways of storing data - def. recommend that. Start here http://www.intelligententerprise.com/001020/celko.jhtml and after that a Google search for 'Joe Celko' should have you sorted. Good luck!
Macka
Good point Tom. Also, I ended up writing triggers which maintained the integrity of my tree (eg. lft/rgt values when inserting/moving/deleting) It's okay having your app do this, but it only takes a "clever" developer in management studio to screw up your entire tree!
Macka
Some more links on here: http://stackoverflow.com/questions/143226/cloning-hierarchical-data
Macka
+1  A: 

As @Macka writes, the left and right values are not foreign keys to tree nodes, so they don't have to be the same type. They can be integers while the tree node primary key is a GUID.

Celko also wrote "Trees and Hierarchies in SQL for Smarties" which goes into more detail about Nested Set Model, and other solutions. Reading this book will save you a lot of time and a lot of mistakes.

There are other solutions to storing hierarchical data in a database. See my answer here: http://stackoverflow.com/questions/192220/what-is-the-most-efficient-elegant-way-to-parse-a-flat-table-into-a-tree/192462#192462

Bill Karwin