Previously I had only one table (I have more, ofc, but these are the ones I’m struggling with), which was the UNIVERSITY table.
I now want to create two more tables : STATE and CITY.
Table Attributes
UNIVERSITY: ID, NAME, SHORTNAME, FK_STATE, FK_CITY
STATE: ID, NAME, SHORTNAME
CITY : ID, NAME
Problem
I am parsing a excel sheet for the universities (including the city and state in which this university resides). Once I have all this information in memory, the hardest part comes.
How do I:
Insert all Universities in the database,including its state and city, but querying the STATE and CITY databases for their IDs, to set the FK’s of the university correctly.
Example:
Once I add a University named ‘PUC-RIO’, it is presented in ‘RIO DE JANEIRO’, (city) ‘RIO DE JANEIRO’ (state aswell). Adding the university itself is not a problem, but, how can I update its foreign key to the ‘RIO DE JANEIRO’ Key on state and city tables?
You’ll have to do this for each university: