Recently I’ve been trying to create a RDB, and the problem I’m running into is that the columns I have with a foreign key constraint will not populate with the values of the referenced column (it will have null values). The only way I’ve managed to have the correct values is to populate it manually.
I will say, however, that the foreign keys did stop me from having different values in the two columns.
TLDR/Conclusion:
So, do foreign keys simply constrain the values in a column without necessarily populating that column?
If so, what would be a function/method that would achieve this goal?
Thanks,
Michael
Advertisement
Answer
Foreign key constraints simply constrain the values in a column without populating that column.
Your application is expected to set the value of the foreign key column in the referring table to the value of the referenced column in the referenced table.
Typically, the rows are related, and your application knows that it is working with a particular row that is in a particular relationship to the referenced row in the other table.
For example, in a simple workforce management application, you might have tables EMPLOYEE
, and DEPARTMENT
, and your EMPLOYEE
table might have foreign key columns MANAGER_NAME
and DEPARTMENT_NAME
. When inserting a new row for a new employee, your data entry form has already asked the operator to pick the employee’s department from a list of departments, and the employee’s Manager from a list of managers, and so your application then sets the MANAGER_NAME
and DEPARTMENT_NAME
with that data at the time you perform your INSERT