Copy a few of the columns of a csv file into a table

后端 未结 7 459
难免孤独
难免孤独 2020-11-28 03:39

I have a CSV file with 10 columns. After creating a PostgreSQL table with 4 columns, I want to copy some of 10 columns into the table.

the columns of my CSV table ar

相关标签:
7条回答
  • 2020-11-28 04:20

    If the number of imported rows is not important for you as result, you could also:

    create two tables:

    • t1 (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10):with all the columns of the csv file
    • t2 (x2 x5 x7 x10): as you need it

    then create:

    • a trigger function, where you insert the desired columns into t2 instead and return NULL to prevent this row being inserted in t1

    • a trigger for t1 (BEFORE INSERT FOR EACH ROW) that calls this function.

    Especially with larger csv files BEFORE INSERT triggers are also useful to filter out rows with certain properties beforehand, and you can do type conversions as well.

    0 讨论(0)
提交回复
热议问题