修改数据库表以及字段字符编码

2022/3/28 19:22:43

本文主要是介绍修改数据库表以及字段字符编码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

SELECT
CONCAT(
'ALTER TABLE `',
TABLE_NAME,
'` MODIFY `',
COLUMN_NAME,
'` ',
DATA_TYPE,
'(',
CHARACTER_MAXIMUM_LENGTH,
') CHARACTER SET UTF8 COLLATE utf8_general_ci',
( CASE WHEN IS_NULLABLE = 'NO' THEN ' NOT NULL' ELSE '' END ),
';'
)
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = (select database())
AND DATA_TYPE = 'varchar'
AND ( CHARACTER_SET_NAME != 'utf8' OR COLLATION_NAME != 'utf8_general_ci' );

SELECT
CONCAT( 'ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' )
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = (select database());



这篇关于修改数据库表以及字段字符编码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程