ALTER A DATA TYPE.

HI ALL,
IS THERE A WAY I CAN  ALTER A DATA-TYPE  OF A COLUMNS OF ALL TABLES IN A DATABASE WITH SINGLE COMMAND LINE IN MySQL

THANKS
RotebergAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mukesh YadavFull Stack DeveloperCommented:
You can check column data type by checking table structure.

desc <tableName>;

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
in the case for multiple tables you will need multiple Alter Table SQL statements
Vikas GargAssociate Principal EngineerCommented:
Hi,

You can use this

ALTER TABLE webstore.Store
  MODIFY COLUMN ShortName VARCHAR(100),


If you want to change all columns of a certain type to another type, you can generate queries using a query like this:

select distinct concat('alter table ',
                       table_name,
                       ' modify ',
                       column_name,
                       ' <new datatype> ',
                       if(is_nullable = 'NO', ' NOT ', ''),
                       ' NULL;')
  from information_schema.columns
  where table_schema = '<your database>' 
    and column_type = '<old datatype>';

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SolarWinds® Network Configuration Manager (NCM)

SolarWinds® Network Configuration Manager brings structure and peace of mind to configuration management. Bulk config deployment, automatic backups, change detection, vulnerability assessments, and config change templates reduce the time needed for repetitive tasks.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
alternatively, you can create a Stored Procedure (SP) which loop through the user tables with Alter Table SQL statements, and then you may call that SP to do the work.
RotebergAuthor Commented:
Thanks Vikas!

i will try this command.
RotebergAuthor Commented:
Thanks for the suggestion Ryan but how can I can structure that?
pcelbaCommented:
The only single line command which can change data types of many columns in MySQL database is named mysqlimport. More info: http://webcheatsheet.com/sql/mysql_backup_restore.php

Everything else would need more lines. OTOH, single line does not mean better comfort or better results...
RotebergAuthor Commented:
Thanks Vikas!
But this is for all columns. so  how can i generate a query for  all tables or multiple tables ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.