Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi,

I have a database holding family tree data.
I have a table called person, with all relevent details about them personally (name, date of birth, birthplace, gender)

I need some way of managing relationships bewteen the people in my database. I would like to have another table made of a compound foreign key, with two personID's from the person table.

Is this possible? or are there any obvious workarounds?
I will be implementing the database using hsqldb.

Thanks in advance
Joe Sutton
Posted
Comments
Kschuler 14-Mar-11 11:42am    
Might help if you talk about why you want the table. If it's a table of marriages you may have a problem because I've heard of people getting married...then divorced...then married again to the same person. Which would throw a wrench in your process unless you added a date field to the key.
Member 7726070 14-Mar-11 11:46am    
This is a databse to keep family tree data. Divorces/re-marriages won't be a problem right now. I need a way to be able to reference differenct occurences of the same primary key in one table as a compound foreign key in another. i.e. PARENT references Person.ID, CHILD references Person.ID

Is this possible?

1 solution

Of course you can have a table like that! Let's call the relation (table) "DirectAncestors" for example:
Table DirectAncestors:
PersonId bigint; (Foreign key to table Person.ID)
FatherID bigint; (Foreign key to table Person.ID) 
MotherID bigint; (Foreign key to table Person.ID)


The table Person should have an autoincrement primary key and a unique index on the fields marked *:
Table Person:
ID, (autoincrement)
GivenName, *
Name, *
DOB, *
DOD, (nullable),
Birthplace, *
Gender *


You can also have the table Person have a foreign key relation to itself:
Table Person:
ID (autoincrement)
GivenName *
Name *
DOB, *
DOD (nullable),
Birthplace, *
Gender *, 
MotherID, ( Foreign Key to Person.ID nullable ),
FatherID, ( Foreign Key to Person.ID nullable )


The first solution is preferable when you want to traverse the ancestry in both directions.


Hope that help you a little in your decision.

Cheers!
 
Share this answer
 
v4
Comments
Espen Harlinn 17-Mar-11 10:02am    
Good effort, my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900