I’m just getting back into DBA after not doing any heavy SQL in the past few years and I’m not sure if I’m missing something or if it’s something idiosyncratic with the application.
I’m trying to update a reference table (company_divisions) using my target table (emps) to discover values the reference table is missing as a way to expedite creating entries on the newly created reference table.
For example target table has a foreign key with values, 1, 2, 3, 4, 5 that are not unique in the target table but should be in the reference table. At time of table creation, the reference table has a primary key with values 1, 3, 5 that the target table FK references.
When I attempt to make the FK in the target table I get an error message that value 2 doesn’t exist in the reference table which violates the foreign key restraint. OK, check – I get that. So, I would like to update the reference table by doing a left join with the current ref table as the join table (right side) and find the nulls in the reference PK that are populated in the would be target table FK. I then would like to select anything where reference table PK is null and insert those rows into the reference table using the target table would be FK values as the new reference table PK values to be inserted.
For now I just want to get the join right. Here is the SQL statement I’m using:
SELECT emps.department, company_divisions.department FROM emps LEFT JOIN company_divisions ON emps.department = company_divisons.department
This returns the following error:
ERROR: missing FROM-clause entry for table "company_divisons" LINE 4: ON emps.department = company_divisons.department
What am I doing wrong?