简体   繁体   中英

Importing data into a SQL table from a CSV file and considering foreign keys

How do I import data into a SQL table from a CSV file when I have to consider foreign key relationships between the table I am importing information to, and an already existing table in the database?

I have two tables:

CREATE TABLE project_info (
    project_id SERIAL PRIMARY KEY;
    project_accountnum int,
    name text
);

CREATE TABLE project_forecast  (
    forecast_id SERIAL PRIMARY KEY,
    project_id int REFERENCES project_info (project_id),
    forecast_amount int
);

I have a CSV file that contains the project names and their respective forecasts. It would be redundant to have the project names in the project_forecast table as it would always be the same--hence the foreign key reference. However, I want to upload the CSV using the COPY FROM function but how do I take in account the foreign keys? Should the CSV be in a different format? Should it have additional columns?

This is too long for a comment.

My recommendation is to copy into a staging table. Then you can load the final table from the staging table. The query looks something like this:

with i as (
      insert into project_info(projectname)
          select distinct projectname
          from project_forecast_staging
          returning *
     ),
insert into project_forecast (project_id, forecast_amount)
    select i.project_id, pfs.forecast_amount
    from project_forecast_staging pfs join
         i
         on pfs.projectname = i.projectname;

(This is perhaps oversimplified if you have additional columns or other considerations.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM