I've been trying to create an Apache Derby RDB, but 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 is to populate it manually.
However, the foreign keys did stop me from having different values in the two columns.
Do foreign keys constrain the values in a column without necessarily populating that column?
If so, what would be a function/method that would achieve this goal?
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