Friday, February 24, 2012

Celko's Netsed Sets vs Adjacency List Models

I am currently modelling a db to store geneaology information (family tree)
Each node on the tree will always have zero, one or two parent nodes,
depending upon whether the details of both parents are known.
I have been initially modelling this using a simple adjacency list model
however I would appreciate any feedback on the advantages and
implementation of a nested set model for this application.
CREATE TABLE persons (
PersonID INT,
MotherID INT,
FatherID INT,
Surname VARCHAR(50),
Firstname VARCHAR(25)
)
In this scenario both the MotherID & FatherID would relate back to PersonID.
Thanks
MurphHi
I'd go with below design
CREATE TABLE Parents
(
[ID] INT NOT NULL PRIMARY KEY,
[NAME]CHAR(1) NOT NULL
)
INSERT INTO Parents VALUES (1,'A')
INSERT INTO Parents VALUES (2,'B')
INSERT INTO Parents VALUES (3,'C')
CREATE TABLE Child
(
[ID] INT NOT NULL PRIMARY KEY,
ParentId INT NOT NULL FOREIGN KEY REFERENCES Parents([ID])ON DELETE
CASCADE ON UPDATE CASCADE,
[NAME]CHAR(2) NOT NULL
)
INSERT INTO F VALUES (1,1,'AA')
INSERT INTO F VALUES (2,1,'AA')
INSERT INTO F VALUES (3,2,'BB')
INSERT INTO F VALUES (4,2,'BB')
INSERT INTO F VALUES (5,2,'BB')
INSERT INTO F VALUES (6,3,'CC')
--OR
CREATE TABLE Persons
(
Child_Id int NOT NULL,
Parentid int NULL,
ChildName varchar(25) NOT NULL,
CONSTRAINT PK_Persons_Childid PRIMARY KEY(Child_Id),
CONSTRAINT FK_Persons_Parentid _Child_Id
FOREIGN KEY(Parentid )
REFERENCES Persons(Child_Id)
)
"Murphy" <m@.urphy.com> wrote in message
news:_jO1e.15253$C7.5562@.news-server.bigpond.net.au...
> I am currently modelling a db to store geneaology information (family
tree)
> Each node on the tree will always have zero, one or two parent nodes,
> depending upon whether the details of both parents are known.
> I have been initially modelling this using a simple adjacency list model
> however I would appreciate any feedback on the advantages and
> implementation of a nested set model for this application.
> CREATE TABLE persons (
> PersonID INT,
> MotherID INT,
> FatherID INT,
> Surname VARCHAR(50),
> Firstname VARCHAR(25)
> )
> In this scenario both the MotherID & FatherID would relate back to
PersonID.
> --
> Thanks
> Murph

No comments:

Post a Comment