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 > Extra table or not

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-10-08, 09:34
beechfielder beechfielder is offline
Registered User
 
Join Date: Oct 2007
Posts: 19
Extra table or not

Hello,

I am designing a table where employees can also be managers, giving a recursive relationship. Obviously managers don't need a manager, but all employees have one. I am thinking about creating a table for managers.

The Employees table will look like this
CREATE TABLE Employee
(EmployeeID MEDIUMINT NOT NULL,
EmployeeFirstName VARCHAR(20) NOT NULL,
EmployeeLastName VARCHAR(20) NOT NULL,
CONSTRAINT Employee_PK PRIMARY KEY(EmployeeID)
)
ENGINE=InnoDB;

I am not sure exactly how to create a Manager's table, but was thinking two columns as follows. Then creating referential integrity checks between both columns and the Employee(EmployeeID) column.

CREATE TABLE ManagerEmployee
(ManagerEmployeeID MEDIUMINT NOT NULL,
EmployeeID MEDIUMINT,
CONSTRAINT Employee_PK PRIMARY KEY (EmployeeID, ManagerEmployeeID));

Not sure if this is the best way to do it though. No historical data about past managers will be needed. I could just leave the ManagerEmployeeID column in the Employee table and allow NULL values, which would take care of those without managers. I am interested in how others would do this and why.
Reply With Quote
  #2 (permalink)  
Old 01-10-08, 09:55
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
Quote:
Originally Posted by beechfielder
Obviously managers don't need a manager, but all employees have one.
Really? you work in a two-tiered company? That is a pretty flat organizational structure...
But really, you do not need a separate table. Just add a ManagerID to the Employees table as a foreign key to its own EmployeeID.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 01-10-08, 10:33
beechfielder beechfielder is offline
Registered User
 
Join Date: Oct 2007
Posts: 19
Thanks for that Blindman. This is an assessment I am doing rather than a real company. I am interested in the best way to do things. I have tried your suggestion and added the column to the Employee table, however now I cannot create the table. MySQL gives me an error saying it cannot create the table. I have checked and checked my code, but cannot see a mistake. All the right columns are there.

I have also tried creating the table without the Foreign Key constraints and using an Alter table statement, but get the same result. I get error 1005 cannot create table

Can you see any reason?

CREATE TABLE Employee
(EmployeeID MEDIUMINT NOT NULL,
EmployeeFirstName VARCHAR(20) NOT NULL,
EmployeeLastName VARCHAR(20) NOT NULL,
ManagerEmployeeID MEDIUMINT ,

CONSTRAINT Employee_PK PRIMARY KEY(EmployeeID),
CONSTRAINT Manager_Employee_FK
FOREIGN KEY (ManagerEmployeeID)
REFERENCES Employee (EmployeeID)
)
ENGINE=InnoDB;
Reply With Quote
  #4 (permalink)  
Old 01-10-08, 11:23
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,732
Moving this thread to the MySQL forum...
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #5 (permalink)  
Old 01-10-08, 12:16
beechfielder beechfielder is offline
Registered User
 
Join Date: Oct 2007
Posts: 19
Solved

All, have resolved this by dropping all tables and starting again, although the Foreign Key was only referencing itself, MySQL did not seem to like my adding a Foreign Key afer adding other tables.

Thanks
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