If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Recursive Relationship

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-16-05, 15:29
Spyder_Snyper Spyder_Snyper is offline
Registered User
 
Join Date: Oct 2004
Posts: 5
Arrow Recursive Relationship

Alright... I'm making a very simple database that'll keep track of employess and who their supervisors are. Since Supervisors are employees as well, they all fall into the same table...

Now, the table has 3 main rows, EmployeeID, Name, and SupID.
EmployeeID is an auto-incrementing Integer starting at 0
SupID is a foreign key from the EmployeeID. Since all Sups are also employess, it becomes slightly recursive.

Now the logic behind setting it up is not a problem whatsoever. The problem is, how do I make the relationship?
Reply With Quote
  #2 (permalink)  
Old 01-16-05, 16:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
make the relationship?

Code:
create table Employees
( EmployeeID integer not null primary key auto_increment
, Name varchar(99) not null
, SupID integer null
, foreign key ( SupID ) references Employees ( EmployeeID )
)
note that mysql does not support foreign keys unless you use innodb tables
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-17-05, 10:52
the_spas the_spas is offline
Registered User
 
Join Date: Jan 2005
Posts: 10
hi if not ever emp has a sup o even has 2 of em you should handle this in two tables

emp
- id (primary auto inc)
- name
- position ......

emp_sup
- emp (emp id) (key)
- sup (emp id) (key)

so there is the possibility to add two or more rows for one employee without saving the employe data twice

to select an emps sups just do this
SELECT fields FROM emp, emp_sup WHERE emp.id = emp_sup.sup AND emp_sup.emp = user_to_seach

to select a sups emps just do this
SELECT fields FROM emp, emp_sup WHERE emp.id = emp_sup.emp AND emp_sup.sup = supervisor

hope this usefull a bit
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On