简体   繁体   中英

changing datatype in the entire database

By mistake I have created my tables with data type int(200) , varchar(200) , double(200,2) in MySQL. Now I want to change it to int(20) , varchar(20) , double(20,2) . Since the database is very big changing them one by one is a very very difficult task. I am not sure but I think following SQL can do it-

UPDATE INFORMATION_SCHEMA.COLUMNS
SET COLUMN_TYPE='INT(20)'
WHERE COLUMN_TYPE='INT(200)'
  AND TABLE_SCHEMA = 'DATABASE_NAME';

Will it solve my problem and is it safe to do that.

Thanks in advance.

ALTER TABLE table_name CHANGE collumn_Name1 collumn_Name2 INT(20) ;

it should work.

Note:Collumn_name2 is new Collumn name if you do not want to change its name then type the same collumn name.

So guys

I am not going to change anything in my database because after reading the docs I believe that there is nothing wrong to use int(200), double(200,2), varchar(200) and it will not going to make me any trouble as per my requirements.

Still I want to add one solution for my original question-

we can write a php script to change data type in the entire database together with the following code-

$loop2=$con->query("SELECT COLUMN_NAME, TABLE_NAME, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'database_name' AND COLUMN_TYPE = 'int(200)'");

    while ($row2 =mysqli_fetch_array($loop2))
    {
         $row1=$con->query("alter table database_name.$row2[TABLE_NAME] change column $row2[COLUMN_NAME] $row2[COLUMN_NAME] int(20) default '$row2[COLUMN_DEFAULT]'");

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM