I am new to MySQL. I have designed few tables with Char datatype. Now I want to change all the char datatype columns to Varchar with respective size mentioned in Char.
For example.
Existing - Phone Char(10)
Expected - Phone Varchar(10)
There are around 50 Plus tables. I know how to alter the datatype for each and every table and column but is there any better way to change the datatype in a single shot.
After analyzing the web, I got the answer and changed the datatype using this query.
If you can't write a script to do the job, write a script that writes a script to do the job!
You want a bunch of ALTER TABLE statements:
This'll result in:
Run that and you can go home early!
UPDATE: Stopped the truncation by adding
group_concat_max_len
. Made the length dynamic based on the columns length.MySQL only one liner solution:
Above query will return all
ALTER TABLE
statements, example shown below:Copy the resulting rows and execute, so you can go home even more early.
Additionally, below is PHP/MySQL solution: