Question

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.

Was it helpful?

Solution 2

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]'");

    }

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top