简体   繁体   English

同一表中的关系

[英]Relationships in same table

I'm trying to make 2 "one to many" relationships on the same table, as I have employees managed by TLs supervised by SPVs. 我试图在同一张桌子上建立2个“一对多”关系,因为我有受TL管理的员工受SPV监督。

Employees table consists of ( ID -PK- , name , hire_date ,,,,,) it's the same data for TL and SPV also 员工表由( ID -PK-,name,hire_date 、、、、)组成,它对于TL和SPV也是相同的数据

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) 在ERD上,我已经在同一张桌子上做到了一对多,但是我不知道如何在SQL上做到这一点(我无法发现应该指的是谁)

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 我已经考虑过放弃一对多关系的想法,并添加一个“类型”作为1(如果为雇员),2(如果是TL)和3(如果是SPV),但是我需要知道每个TL的雇员(例如,TL​​#1的雇员为John,保罗,史密斯...在他的团队中)等等

**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 :) **注意:我不确定将其建立为一对多关系是否正确,如果还有其他方法,我将不胜感激:)

sql? SQL吗? just put the same table in the REFERENCES clause. 只需将相同的表放在REFERENCES子句中即可。

if you can't do it, do it in a separate SQL sentence. 如果您无法执行此操作,请在单独的SQL语句中执行。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM