phpmyadmin 更改整个数据库中的列的数据类型- MySQL

p1iqtdky  于 2022-11-09  发布在  PHP
关注(0)|答案(4)|浏览(158)

我是MySQL的新手。我已经设计了一些数据类型为Char的表。现在我想把所有的char数据类型列都改为Varchar,并在Char中提到各自的大小。

For example.
Existing - Phone Char(10) 
Expected - Phone Varchar(10)

有大约50个Plus表。我知道如何改变每个表和列的数据类型,但有没有更好的方法一次改变数据类型。

6ie5vjzr

6ie5vjzr1#

如果你不能写一个脚本来完成这项工作,那就写一个脚本来完成这项工作!
您需要一组ALTER TABLE语句:

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';

这将导致:

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);

跑了就可以早点回家了!
UPDATE:通过添加group_concat_max_len停止截断。使长度基于列长度动态变化。

6pp0gazn

6pp0gazn2#

MySQL唯一的线性解决方案:

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'

以上查询将返回所有ALTER TABLE语句,示例如下所示:

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);

复制结果行并执行,这样您可以更早地回家。
另外,下面是PHP/MySQL解决方案:

// 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);
}
z5btuh9x

z5btuh9x3#

在分析了Web之后,我得到了答案,并使用此查询更改了数据类型。

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';
ffdz8vbo

ffdz8vbo4#

如果您的字段是PK(例如,'id' smallint unsigned),而某些其他数据表的条件约束具有此PK的外部索引键,请使用此查询。
注意:对于现代mysql服务器,从变量-〉sql模式中删除ONLY_FULL_GROUP_BY,默认情况下它包含:

ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

然后插入并执行以下查询:

SET group_concat_max_len = 10000;
SET @database_name = "database1";
SET @table_name = "table1";
SET @change = "mediumint unsigned";
SELECT DISTINCT 
       `table_name`,
       `column_name`,
       `constraint_name`,
       `referenced_table_name`,
       `referenced_column_name`,
       CONCAT(
           GROUP_CONCAT('ALTER TABLE `',table_name,'` DROP FOREIGN KEY `',constraint_name, '`' SEPARATOR ';'),
           ';',
           GROUP_CONCAT('ALTER TABLE `',table_name,'` CHANGE `',column_name,'` `',column_name,'` ',@change SEPARATOR ';'),
           ';',
           CONCAT('ALTER TABLE `',@table_name,'` CHANGE `',referenced_column_name,'` `',referenced_column_name,'` ',@change, ' NOT NULL AUTO_INCREMENT'),
           ';',
           GROUP_CONCAT('ALTER TABLE `',table_name,'` ADD CONSTRAINT `',constraint_name,'` FOREIGN KEY(',column_name,') REFERENCES ',referenced_table_name,'(',referenced_column_name,')' SEPARATOR ';'), 
           ';'
       ) AS query
FROM   `information_schema`.`key_column_usage`
WHERE  `referenced_table_name` IS NOT NULL
   AND `referenced_column_name` IS NOT NULL
   AND `constraint_schema` = @database_name
   AND `referenced_table_name` = @table_name
GROUP BY `referenced_table_name`;

注意:选中EXTRA OPTIONS以显示结果中字段的全文。2然后复制并执行所有生成的查询。3在变量中恢复ONLY_FULL_GROUP_BY。
好好享受吧!

相关问题