Change the Datatype of a Column in the entire Data

2019-07-11 18:12发布

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.

3条回答
时光不老,我们不散
2楼-- · 2019-07-11 19:02

After analyzing the web, I got the answer and changed the datatype using this query.

select 
concat('ALTER TABLE ', table_name,' MODIFY COLUMN ', column_name,' ', REPLACE (column_type, 'char', 'varchar'),';')
from information_schema.columns
where 
data_type='char' 
and table_schema='DBName';
查看更多
smile是对你的礼貌
3楼-- · 2019-07-11 19:06

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:

SET SESSION group_concat_max_len = 1000000; -- Bumps the limit of 1028
SELECT GROUP_CONCAT(
    CONCAT(
        'ALTER TABLE `',
        table_name,
        '` MODIFY COLUMN `',
        column_name,
        '` VARCHAR(',
        character_maximum_length,
        ')'
    )
SEPARATOR ';\n') your_alter_statements
FROM information_schema.columns
WHERE table_schema = 'concrete'
AND data_type = 'char';

This'll result in:

ALTER TABLE `table1` MODIFY COLUMN `col1` VARCHAR(10);
ALTER TABLE `table1` MODIFY COLUMN `col2` VARCHAR(10);
ALTER TABLE `table2` MODIFY COLUMN `col1` VARCHAR(10);
ALTER TABLE `table3` MODIFY COLUMN `col1` VARCHAR(10);
ALTER TABLE `table3` MODIFY COLUMN `col2` VARCHAR(10);

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.

查看更多
趁早两清
4楼-- · 2019-07-11 19:11

MySQL only one liner solution:

SELECT 'ALTER TABLE ',TABLE_NAME, ' MODIFY COLUMN ',COLUMN_NAME, CONCAT(' VARCHAR(', CHARACTER_MAXIMUM_LENGTH, ');')
    FROM INFORMATION_SCHEMA.COLUMNS where DATA_TYPE = 'char' AND TABLE_SCHEMA='concrete'

Above query will return all ALTER TABLE statements, example shown below:

ALTER TABLE     tablename1   MODIFY COLUMN  col_name1    VARCHAR(17);
ALTER TABLE     tablename2   MODIFY COLUMN  col_name2    VARCHAR(17);
ALTER TABLE     tablename3   MODIFY COLUMN  col_name3    VARCHAR(60);

Copy the resulting rows and execute, so you can go home even more early.

Additionally, below is PHP/MySQL solution:

// Initialise Connection
define('DB_HOST', 'HOST_NAME_HERE');
define('DB_NAME', 'DB_NAME_HERE');
define('DB_USER_NAME', 'DB_USER_NAME_HERE');
define('DB_PASSWORD', 'DB_PASSWORD_HERE');

$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER_NAME, DB_PASSWORD, [
    PDO::ATTR_PERSISTENT => false,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
]);

// Get a list of CHAR columns
$sql = "SELECT TABLE_NAME, COLUMN_NAME, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE, COLUMN_DEFAULT
        FROM INFORMATION_SCHEMA.COLUMNS where DATA_TYPE = 'char' AND TABLE_SCHEMA='" . DB_NAME . "'";

$stmt = $pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Change CHAR to VARCHAR
foreach ($data as $rec) {
    $ddq = 'ALTER TABLE ' . $rec['TABLE_NAME'] . ' CHANGE ' . $rec['COLUMN_NAME'] . ' ' . $rec['COLUMN_NAME'] . ' VARCHAR(' .
           $rec['CHARACTER_MAXIMUM_LENGTH'] . ') ' . ($rec['IS_NULLABLE'] == 'YES' ? 'NULL' : 'NOT NULL') . (isset($rec['COLUMN_DEFAULT']) ? " DEFAULT '{$rec['COLUMN_DEFAULT']}'" : '');
    $pdo->query($ddq);
}
查看更多
登录 后发表回答