Search code examples
sqlselect-into

Select into without touching the autoincrement ID field


I want to migrate data from one table to another without copying the autoincremented ID field.

source_table:

ID LABEL

-- -----

5 text

6 text2

dest_table:

ID LABEL

-- -----

SELECT LABEL
INTO dest_table
FROM source_table

Will the above statement work knowing that ID in dest_table is an auto-incremented primary key?


Solution

  • Yes, it will work ans result in

    1 text

    2 text2

    But the problem is if you have a second table that is created and will referece to the created on. Then you have to leave the values of the primary keys as they are and not ignore them.

    The query is not correct:

    INSERT INTO destination (label) SELECT label FROM source