Ali Raza
Ali Raza

Reputation: 489

Move one column from one table to another mysql

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.

Upvotes: 0

Views: 101

Answers (1)

Matthew Poer
Matthew Poer

Reputation: 1682

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;

Upvotes: 2

Related Questions