Question

I'm trying to make 2 "one to many" relationships on the same table, as I have employees managed by TLs supervised by SPVs.

Employees table consists of (ID -PK- , name , hire_date ,,,,,) it's the same data for TL and SPV also

on the ERD I've made it as a one to many on the same table but I've no idea how to make it on SQL (I can't detect what should be refer to who)

I've thought about giving up the one to many relationship idea and add a "type" as 1 if employee and 2 if TL and 3 if SPV but i need to know every TL's employees for example (TL#1 have the employees John , Paul, Smith ,... in his team) and so on

**Note: I'm not sure if it's the right thing to make it as a one to many relationship , if there's any other way I'd really appreciate it :)

Was it helpful?

Solution

sql? just put the same table in the REFERENCES clause.

if you can't do it, do it in a separate SQL sentence.

CREATE TABLE EMPLOYEE (
    id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    spv INTEGER UNSIGNED NULL,
    ...,
    PRIMARY KEY(id)
);

ALTER TABLE employee ADD FOREIGN KEY ( spv ) REFERENCES employee (id) ON DELETE RESTRICT ON UPDATE CASCADE 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top