MySQL Alter Table Add Column Like Another Column Syntax

alter-tableMySQLsyntax

I was looking into MySQL specification, but couldn't find any solution to my problem. I want to clone specific columns in several tables like this:

ALTER TABLE `table` ADD COLUMN `new_column` LIKE `column` AFTER `column`;

Of course this gives me syntax error: right syntax to use near 'LIKE `column` AFTER `column`', what i was expecting. Is there any possible solution to this or i have to specify directly a new column? I know that "cloning" table structure using LIKE operator is possible. Is it also possible in this case?

Best Answer

No. This is not possible using command.

All you can do is write a procedure to grab the table & column-name. Get the column definition from information_schema. Create alter-table-modify-column syntaxes & exec... Something like following:

call modify_table(target_table, source_table, source_column);