Let's say I have 2 tables: Students and Groups.
- The Group table has 2 columns: id, GroupName
- The Student table has 3 columns: id, StudentName and GroupID
- The GroupID is a foreign key to a Group field.
I need to import the Students table from a CSV, but in my CSV instead of the Group id appears the name of the group. How can I import it with pgAdmin without modifying the csv?
Based on Laurenz answer, use follwoing scripts:
Create a temp table to insert from CSV file:
Then, import the CSV file:
Now, create
stdandgrptables for students and groups:It's
grptable's turn to be populated based ondistinctvalue of group name. Consider howrow_number() is use to provide value forid`:And the final step, select data based on the
jointhen insert it into thestdtable:At the end, retreive data from final
stdtable: