0

I have two tables, Products and Images. The Products table has an "image" column, I want to move the images inside said column to the Images table. The Images table has column "product_id", that's how both tables are related

Products table

id
image

Images Table

id
image
product_id

The ID from the Products table should work as a foreign key in the Images table under product_id for image, it should works the same as a primary key in Products table as for images. One more thing, Images Table is not empty it already has data in it.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Ali Raza
  • 673
  • 8
  • 19

1 Answers1

2

You can use INSERT INTO with raw data, but you can also combine it with a SELECT query.

For the new Id values on your Images table, you can use the existing Product.Id values or generate new ones with a function like UUID(), which I would recommend because it makes longer more unique IDs that can be globally unique.

Something like this will probably work:

INSERT INTO Images (id, image, product_id)
SELECT UUID(), image, id FROM Products;
Matthew Poer
  • 1,682
  • 10
  • 17