dbTalk Databases Forums  

Self-Referencing Foreign Key with Many-to-Many Relationship

comp.databases comp.databases


Discuss Self-Referencing Foreign Key with Many-to-Many Relationship in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
isporter@gmail.com
 
Posts: n/a

Default Self-Referencing Foreign Key with Many-to-Many Relationship - 09-23-2006 , 05:26 AM






Imagine you have a Table Employees, with values ID (PK), Name,
Supervisor (FK). Supervisors are also employees, so 'Supervisor' is a
self-referencing foreign key to ID.

We realise that supervisors supervise more than one employee. Now
imagine that employees can have more than one supervisor - thus, a
many-to-many, self-referencing foreign key.

How can this be done?

(The example is for discussion's sake - in reality, my table is Tag ID,
Tag Name, and Parent, etc.)

Many thanks


Reply With Quote
  #2  
Old   
strawberry
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-23-2006 , 06:13 AM







isporter (AT) gmail (DOT) com wrote:
Quote:
Imagine you have a Table Employees, with values ID (PK), Name,
Supervisor (FK). Supervisors are also employees, so 'Supervisor' is a
self-referencing foreign key to ID.

We realise that supervisors supervise more than one employee. Now
imagine that employees can have more than one supervisor - thus, a
many-to-many, self-referencing foreign key.

How can this be done?

(The example is for discussion's sake - in reality, my table is Tag ID,
Tag Name, and Parent, etc.)

Many thanks
I guess this would work:

employees(id*,name)
employees_supervisors(employee_id*,supervisor_id*)

- supervisors are all those in the employees_supervisors whose
supervisor_id is NULL
- both employee_id & supervisor_id reference employees.id



Reply With Quote
  #3  
Old   
David Cressey
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-23-2006 , 07:26 AM




<isporter (AT) gmail (DOT) com> wrote

Quote:
Imagine you have a Table Employees, with values ID (PK), Name,
Supervisor (FK). Supervisors are also employees, so 'Supervisor' is a
self-referencing foreign key to ID.

We realise that supervisors supervise more than one employee. Now
imagine that employees can have more than one supervisor - thus, a
many-to-many, self-referencing foreign key.

How can this be done?

(The example is for discussion's sake - in reality, my table is Tag ID,
Tag Name, and Parent, etc.)

Many thanks

The term I'm familiar with is "reflexive relationship" rather than "self
referencing key", but that's just a matter of terminology.

To deal with your question, the answer is the same as it would be for any
other kind of many to many relationship: you need another table, say table
Supervision with columns (EmployeeId (FK) and SupervisorId (FK)). The PK
of this table is the entire row.

EmployeeId references Employees.ID, and so does SupervisorID, but they
don't reference the same row. (unless an employee is his own supervisor).

The most common situation where a table like this is needed is parts
explosion. When parts are components of other parts, the relationship is
many-to-many.

HTH.






Reply With Quote
  #4  
Old   
isporter@gmail.com
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-23-2006 , 07:41 AM



Thus, is the following correct? I have tags that are components of
other tags. Articles are tagged. Each article has a primary tag.
Articles are either content or a rule to select other articles based on
tags. tags have parents and a child rank. This way I can build a
navigation based on tags. I'm not sure if there is a better way to do
this.

http://www.intraspin.com/db.gif

Thanks again

David Cressey wrote:
Quote:
isporter (AT) gmail (DOT) com> wrote in message
news:1159007214.155843.67870 (AT) m73g2000cwd (DOT) googlegroups.com...
Imagine you have a Table Employees, with values ID (PK), Name,
Supervisor (FK). Supervisors are also employees, so 'Supervisor' is a
self-referencing foreign key to ID.

We realise that supervisors supervise more than one employee. Now
imagine that employees can have more than one supervisor - thus, a
many-to-many, self-referencing foreign key.

How can this be done?

(The example is for discussion's sake - in reality, my table is Tag ID,
Tag Name, and Parent, etc.)

Many thanks


The term I'm familiar with is "reflexive relationship" rather than "self
referencing key", but that's just a matter of terminology.

To deal with your question, the answer is the same as it would be for any
other kind of many to many relationship: you need another table, say table
Supervision with columns (EmployeeId (FK) and SupervisorId (FK)). The PK
of this table is the entire row.

EmployeeId references Employees.ID, and so does SupervisorID, but they
don't reference the same row. (unless an employee is his own supervisor).

The most common situation where a table like this is needed is parts
explosion. When parts are components of other parts, the relationship is
many-to-many.

HTH.


Reply With Quote
  #5  
Old   
FreeData
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-23-2006 , 10:46 PM



Quote:
Imagine you have a Table Employees, with values ID (PK),
Name, Supervisor (FK). Supervisors are also employees, so
'Supervisor' is a self-referencing foreign key to ID.

We realise that supervisors supervise more than one
employee. Now imagine that employees can have more than
one supervisor - thus, a many-to-many, self-referencing
foreign key.
How can this be done?

(The example is for discussion's sake - in reality, my
table is Tag ID, Tag Name, and Parent, etc.)

Many thanks

It's a manytomany relationship. It just so happens to be
with itself. You need to create an associative entity just
like any other many to many. The first relationship
identifies all of the supervised employees. The second
relationship identifies all of the supervising employees.
The is your standard network data structure.


Reply With Quote
  #6  
Old   
Lennart
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-24-2006 , 02:41 AM




strawberry wrote:
Quote:
isporter (AT) gmail (DOT) com wrote:
[...]
- supervisors are all those in the employees_supervisors whose
supervisor_id is NULL
IMO both attributes in employees_supervisors should be "not null", and
together they are the p.k. of the table. A *proper* supervisor is those
emploees that exist as supervisors but not as employees in
employees_supervisors


/Lennart



Reply With Quote
  #7  
Old   
strawberry
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-24-2006 , 06:20 AM




Lennart wrote:
Quote:
strawberry wrote:
isporter (AT) gmail (DOT) com wrote:
[...]
- supervisors are all those in the employees_supervisors whose
supervisor_id is NULL

IMO both attributes in employees_supervisors should be "not null", and
together they are the p.k. of the table. A *proper* supervisor is those
emploees that exist as supervisors but not as employees in
employees_supervisors


/Lennart
I'd go along with that.



Reply With Quote
  #8  
Old   
David Cressey
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-24-2006 , 06:42 AM




"Lennart" <Erik.Lennart.Jonsson (AT) gmail (DOT) com> wrote

Quote:
strawberry wrote:
isporter (AT) gmail (DOT) com wrote:
[...]
- supervisors are all those in the employees_supervisors whose
supervisor_id is NULL

IMO both attributes in employees_supervisors should be "not null", and
together they are the p.k. of the table.
You are right.

Quote:
A *proper* supervisor is those
emploees that exist as supervisors but not as employees in
employees_supervisors

In most companies, supervisors have supervisors, although they may have the
title "managers" or something like that.
The employee_supervisors table is capable of recording the structure of a
tree structured hierarchical supervision relationship all the way up to the
CEO.






Reply With Quote
  #9  
Old   
Lennart
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-24-2006 , 07:05 AM




David Cressey wrote:
Quote:
"Lennart" <Erik.Lennart.Jonsson (AT) gmail (DOT) com> wrote in message
[...]
A *proper* supervisor is those
emploees that exist as supervisors but not as employees in
employees_supervisors


In most companies, supervisors have supervisors, although they may have the
title "managers" or something like that.
The employee_supervisors table is capable of recording the structure of a
tree structured hierarchical supervision relationship all the way up to the
CEO.
Yes, I'm aware of that. I was just trying to mimic what I thought was
the OP's intention with
the NULL row in employees_supervisors.


/Lennart



Reply With Quote
  #10  
Old   
--CELKO--
 
Posts: n/a

Default Re: Self-Referencing Foreign Key with Many-to-Many Relationship - 09-24-2006 , 11:37 AM



Quote:
How can this be done?
Get a copy of TREES & HIERARCHIES IN SQL for several orther ways to
model this problem. In poarticular, look up "Nested Sets"



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.