4
Adding to what David Whittaker posted, I have created a query that generates the complete table and columns alter statement that will convert each table. It may be a good idea to run
除了David Whittaker所发布的内容之外,我还创建了一个查询,生成将转换每个表的完整表和列alter语句。跑步也许是个好主意
SET SESSION group_concat_max_len = 100000;
设置会话group_concat_max_len = 100000;
first to make sure your group concat doesn't go over the very small limit as seen here.
首先要确保你的组不会超过这里看到的很小的限制。
SELECT a.table_name, concat('ALTER TABLE ', a.table_schema, '.', a.table_name, ' DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci, ',
group_concat(distinct(concat(' MODIFY ', column_name, ' ', column_type, ' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ', if (is_nullable = 'NO', ' NOT', ''), ' NULL ',
if (COLUMN_DEFAULT is not null, CONCAT(' DEFAULT \'', COLUMN_DEFAULT, '\''), ''), if (EXTRA != '', CONCAT(' ', EXTRA), '')))), ';') as alter_statement
FROM information_schema.columns a
INNER JOIN INFORMATION_SCHEMA.TABLES b ON a.TABLE_CATALOG = b.TABLE_CATALOG
AND a.TABLE_SCHEMA = b.TABLE_SCHEMA
AND a.TABLE_NAME = b.TABLE_NAME
AND b.table_type != 'view'
WHERE a.table_schema = ? and (collation_name = 'latin1_swedish_ci' or collation_name = 'utf8mb4_general_ci')
GROUP BY table_name;
A difference here between the previous answer is it was using utf8 instead of ut8mb4 and using t1.data_type with t1.CHARACTER_MAXIMUM_LENGTH didn't work for enums. Also, my query excludes views since those will have to altered separately.
前一个答案的不同之处在于它使用的是utf8而不是ut8mb4,使用的是t1。data_type t1。CHARACTER_MAXIMUM_LENGTH对枚举无效。此外,我的查询不包括视图,因为视图必须分别修改。
I simply used a Perl script to return all these alters as an array and iterated over them, fixed the columns that were too long (generally they were varchar(256) when the data generally only had 20 characters in them so that was an easy fix).
我简单地使用一个Perl脚本来将所有这些alters作为数组返回,并遍历它们,固定那些太长的列(通常它们是varchar(256),因为数据通常只有20个字符,所以很容易修复)。
I found some data was corrupted when altering from latin1 -> utf8mb4. It appeared to be utf8 encoded latin1 characters in columns would get goofed in the conversion. I simply held data from the columns I knew was going to be an issue in memory from before and after the alter and compared them and generated update statements to fix the data.
我发现在改变latin1 -> utf8mb4时,一些数据被破坏。似乎是utf8编码的latin1列字符在转换过程中会出错。我简单地从我知道的列中获取数据,这将是在修改和比较它们之前和之后的内存中的一个问题,然后生成更新语句来修复数据。