Search code examples
mysqlsqlself-referencing-table

Self-referential relationship table design: one or two tables?


CREATE TABLE Employee 
(
     id        INT,
     boss      INT REFERENCES Employee(id),
     PRIMARY KEY (id)
);

One employee can have many bosses and one boss can have many employees.

Does this table function the same as this two-table design?

  CREATE TABLE Employee 
    (
         id        INT,         
         PRIMARY KEY (id)
    );

Create table ManagerRelation (
  id_from int NOT NULL,
  id_to int NOT NULL, 
  PRIMARY KEY (id_from, id_to),
  FOREIGN KEY (id_from) REFERENCES Employee(id),
  FOREIGN KEY (id_to) REFERENCES Employee(id)
);

The second table ManagerRelation stores ids of workers who have boss-employee relationship.

My question is, are these two design right? If right, are they exactly the same functionally?


Solution

  • The two designs are quite different. The first requires that each employee have (at most) one boss, although each boss could have many employees.

    The second allows for employees to have more than one boss.

    From your description of the problem, the second form is the more appropriate data model. From my understanding of boss-employee relationships, I would expect the first to be correct.