[MySQL] Batch modify the character set of the data table and all fields in the data table

A statement to query all table names in a database:

# 更改 DATABASE_NAME
SELECT TABLE_NAME from information_schema.`TABLES` WHERE TABLE_SCHEMA = 'DATABASE_NAME';

Splice the table name into the above statement to change the table encoding (character set) and the encoding (character set) of all fields in the table, and get the following statement:

# 根据转换字符集 修改 utf8mb4 ---> utf8mb4_general_ci
SELECT
	CONCAT(
		'ALTER TABLE ',
		TABLE_NAME,
		' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;'
	)
FROM
	information_schema.`TABLES`
WHERE
	TABLE_SCHEMA = 'DATABASE_NAME';  # 更改 DATABASE_NAME

The result set obtained by the statement execution is copied out to be the batch update statement, which can be executed in the database

Guess you like

Origin blog.csdn.net/qq_22227087/article/details/108535499