Monday, September 9, 2019

How to duplicate a column in mysql

I wanted to copy a column's data into a new column in my MySQL database or rather say I wanted to create a new column and copy data from an already existing column in same table.

If you too want to do the same just issue below commands either in MySQL console or SQL tab in phpmyadmin:

ALTER TABLE `table_name` ADD `new_column` varchar(700);  
UPDATE `table_name` SET `new_column` = `old_column`;

First command creates the new column. Change the "varchar(700)" to whatever you want.
Second command copies the data from old column to new column.

No comments:

Post a Comment