Get the App
SLTechnology News&Howtos  ›  Database  › 

The method of batch Modification of character set by mysql

Shulou Source: shulou.com Published: 2022-06-01 01:42:39 09月10日 Update

This article mainly introduces the method of batch modification of character set by mysql, which is very detailed and has certain reference value. Friends who are interested must finish it!

Mysql how to modify the character set in batches: first, you need to use statements to generate all the statements actually executed; then, based on the metadata table of MySQL, you can get a set of SQL lists that can be executed directly; finally, you can paste and execute the statements directly.

How to modify character sets in batches by mysql:

1. Modify database coding and character set

This step is relatively simple and can be performed directly:

ALTER DATABASE db_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin

two。 Modify the encoding and character set of data table and fields in the table

It takes two steps here.

First, you need to use statements to generate all the statements that are actually executed:

SELECT CONCAT ("ALTER TABLE `", TABLE_NAME, "`CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;") AS target_tablesFROM INFORMATION_SCHEMA.TABLESWHERE TABLE_SCHEMA= "db_name" AND TABLE_TYPE= "BASE TABLE"

Based on the metadata table of MySQL, this statement results in a list of SQL that can be executed directly, as follows:

ALTER TABLE `table1` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;ALTER TABLE `table2` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;ALTER TABLE `table3` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;ALTER TABLE `table4` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;ALTER TABLE `table5` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;ALTER TABLE `table6` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin

Where table1 to table6 are all the data tables in the database.

Then, paste and execute the statement directly.

Note that CONVERT TO is used instead of DEFAULT because the latter does not modify the encoding and character set of the fields in the table.

In addition, for databases with a large number of data tables, you can first export the results of the first step to a .sql file, and then execute it through the SQL file.

The above is all the contents of this article "how to modify the character set in batch by mysql". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Data characters character sets statements datasheets methods databases codes content fields actual files articles generation value interests guys buddies more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft Shulou Technology MariaDB macOS