MySQL batch modify the character set and collation of the database

Original link of this article: https://blog.csdn.net/xzk9381/article/details/114872488

One, deal with foreign key constraints

When processing a large amount of data, you may encounter the problem of foreign key constraints and the data cannot be successfully modified. Therefore, you can set to ignore foreign key constraints before executing the modification instruction, and then open it after the modification is completed. The command is as follows:

SET FOREIGN_KEY_CHECKS=0;
 
-- Insert your other SQL Queries here...
 
SET FOREIGN_KEY_CHECKS=1;

Two, modify the character set and sorting of the database

To modify the character set and sorting of a library, you can use the following commands:

ALTER DATABASE <yourDB> CHARACTER SET <charset> COLLATE <collation>

For example, modify the wiki database character set to utf8 and sort to utf8_bin:

ALTER DATABASE wiki CHARACTER SET utf8 COLLATE utf8_bin;

Three, modify the character set and sorting of the table

To modify the character set of the table, you can use the following commands:

SELECT CONCAT('ALTER TABLE `',  table_name, '` CHARACTER SET <charset> COLLATE <collation>;')
FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C
WHERE C.collation_name = T.table_collation
AND T.table_schema = '<yourDB>'
AND
(
    C.CHARACTER_SET_NAME != '<charset>'
    OR
    C.COLLATION_NAME != '<collation>'
);

For example, modify the table character set in the wiki database to utf8, and the sort to utf8_bin:

SELECT CONCAT('ALTER TABLE `',  table_name, '` CHARACTER SET utf8 COLLATE utf8_bin;')
FROM information_schema.TABLES AS T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` AS C
WHERE C.collation_name = T.table_collation
AND T.table_schema = 'wiki'
AND
(
    C.CHARACTER_SET_NAME != 'utf8'
    OR
    C.COLLATION_NAME != 'utf8_bin'
);

After the execution of the above command is completed, some statements to modify the table will be output, and these statements will be copied and executed in the database to complete the modification of the table.

Fourth, modify the character set and sorting of the field

If the data type is varchar, use the following commands to modify the character set and sorting of the field:

SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, '(', CHARACTER_MAXIMUM_LENGTH, ') CHARACTER SET <charset> COLLATE <collation>', (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = '<yourDB>'
AND DATA_TYPE = 'varchar'
AND
(
    CHARACTER_SET_NAME != '<charset>'
    OR
    COLLATION_NAME != '<collation>'
);

If the data type is non-varchar, use the following commands to modify the character set and sorting of the field:

SELECT CONCAT('ALTER TABLE `', table_name, '` MODIFY `', column_name, '` ', DATA_TYPE, ' CHARACTER SET <charset> COLLATE <collation>', (CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END), ';')
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = '<yourDB>'
AND DATA_TYPE != 'varchar'
AND
(
    CHARACTER_SET_NAME != '<charset>'
    OR
    COLLATION_NAME != '<collation>'
);

After the above sql is executed, it will output the sql for modifying the modified field. Copy these sql to mysql and execute it to complete the modification of the field.

Original link of this article: https://blog.csdn.net/xzk9381/article/details/114872488

Guess you like

Origin blog.csdn.net/xzk9381/article/details/114872488