简体   繁体   English

外键可以是可选的吗

[英]Can a foreign key be optional

Table employee
{
employeename  //employeename is unique
}

Table job
{
jobid,job,assignedPerson,status
}

assignedPerson is a foreign key that references to employeename(employee) AssignedPerson是一个外键,它引用了employeename(雇员)
What I want is to know how to delete an employee record without having to remove his record from the job table ie I mean I want to keep the job record and see which person was assigned the job even if that person is no longer an employee.. 我想知道的是如何删除雇员记录,而不必从工作表中删除他的记录,即我想保留该工作记录并查看为谁分配了工作,即使该人不再是雇员。 。

Table employee
{
employeename
}

Table jobemployee
{
jobid,assignedPerson
}

Table job
{
jobid,job,status
}

Even if I do this I will have to delete the record from jobemployee if I want to delete the record of employee from 'employee' table and will not be able to know which person was assigned the job.. 即使我这样做,如果我想从“雇员”表中删除雇员的记录,也将不得不从雇员的记录中删除,并且将无法知道为谁分配了工作。
Please help.. 请帮忙..

The best option is to add a flag to the Employee table for deleted . 最好的选择是在Employee表中添加一个deleted的标志。

A workaround is to add a column assignedNonEmployee to the job table. 一种解决方法是在job表中添加一个assignedNonEmployee列。 Before you delete an employee , you set the assignedPerson column to null , and copy its value into the assignedNonEmployee column. 在删除employee之前,您需要将assignedPerson列设置为null ,并将其值复制到assignedNonEmployee列中。 Since the later column has no foreign key constraint, you are now allowed to delete the employee record. 由于后面的列没有外键约束,因此现在允许您删除employee记录。

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

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