Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Reference same table question.

Author  Topic 

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2009-10-10 : 17:10:00
Hi.
This is just a curiosity question.
I have seen some tables to have a FK relationship in one of their own columns.
p.e. an id column with a relation on another column on the same table.
Since i can't find anything solid on the net, can someone tell why and in what occasion this kind of relationship is useful?
An example would be nice.
Thanks.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-10 : 18:13:37
Think about employee and managers or set products and their childs. The are saved in the same table.
Each entry has an ID and a referenceID to the ID that is NULL (if for example it is the manager himself) or a pointer to a parent.

Do you know what I mean?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2009-10-10 : 22:23:40
Hi.
Yes i think so, but what is better?To have a columns that says if it's a manager or not (0,1) or to have fk to the same table?And how will you find if it's a manager or not in a query?Won't you look for the value (of 0 or 1)?How will the fk to the same table help?
Go to Top of Page

dportas
Yak Posting Veteran

53 Posts

Posted - 2009-10-11 : 03:41:52
quote:
Originally posted by sapator

Hi.
Yes i think so, but what is better?To have a columns that says if it's a manager or not (0,1) or to have fk to the same table?And how will you find if it's a manager or not in a query?Won't you look for the value (of 0 or 1)?How will the fk to the same table help?




The point of the foreign key is that it tells you who the manager of each person is. In practice you would probably have another column to indicate a person's job title or seniority.
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2009-10-11 : 19:02:44
Ah, ok made clearer to my mind.
Thanks.
Go to Top of Page
   

- Advertisement -