views:

57

answers:

2

I'm building a database and have run into a problem that I can't seem to wrap my mind around. The database is much more complex than what is pictured, but the problem can be distilled into the table structure below.

The issue is that every employee has a manager and every manager is an employee. It would seem that these tables have to reference eachother. However, this doesn't seem to work correctly when I set this up.

alt text

I'm using cakephp. What is the name of this relationship type? Is this the wrong design? What is the proper design for this situation? I would like it to work as automagically as possible.

+1  A: 

Just scrap employees.manager_id. Every manager will have a pointer to a employee row, but not every employee will point to a manager. This would create a traditional one-to-one relationship.

carl
I may be approaching this problem the wrong way, but the employees table is the primary data set that I am working with. In your design, what would the query look like if I wanted to see all details about an employee named Jason? If there is no employees.manager_id, won't I have to now do two queries?
XL
+5  A: 

If I understand the question correctly, you don't need the Managers table at all. Since a manager is an employee, there is no reason the managers shouldn't be stored in the Employees table. Simply add a field to the Employees table called manager_id, which for any given row references that employee's manager (which is another row from the same table).

This is called a recursive one-to-one relationship, and it is quite common. In your case, it will also prevent you from having to define duplicate fields across the two entities (names, passwords, job titles, etc).

Ash White
I'd do it this way too, but I'd add a "Manager" boolean flag to enable a "Only Managers can be Managers" business rule,and allow for an one-to-one extension table for Manager attributes.
MikeAinOz
Woops. I re-read what you wrote, and think I understand. So, just use the employees.manager_id field and populate it with the correct employees.id of that particular records manager.
XL