I want to rename columns in database programmatically and MySQL enforces me to specify column definition in CHANGE statement.
ALTER TABLE table_name CHANGE old_name new_name column_definition
So, I want to build column definition from INFORMATION_SCHEMA.COLUMNS table. I’ve read answers which adjust me to parse output of SHOW CREATE TABLE table_name, but I do not want to do that in some reasons.
I want to get column definition as result of single SQL script if it is possible.
Try this:
You’ll need to fill in the right values, but it should give you the precise string you’ll need to fill into the ALTER statement. You will need to do some additional work to deal with default statements and NULL settings.
The complete sample:
EDIT Added quoting of default value, collation and charset.