简体   繁体   中英

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)
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 .

A workaround is to add a column assignedNonEmployee to the job table. Before you delete an employee , you set the assignedPerson column to null , and copy its value into the assignedNonEmployee column. Since the later column has no foreign key constraint, you are now allowed to delete the employee record.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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