mysql 字符集排查

mysql 字符集排查

庫級別

SELECT
    * 
FROM
    information_schema.schemata 
WHERE
    schema_name NOT IN ( 'information_schema', 'performance_schema', 'mysql', 'sys' ) 
    AND ( default_character_set_name <> 'utf8' OR default_collation_name <> 'utf8_general_ci' );

表級別

SELECT 
    TABLE_SCHEMA,
    TABLE_NAME,
    TABLE_TYPE,
    TABLE_ROWS,
    TABLE_COLLATION,
    CREATE_OPTIONS
FROM
    information_schema.TABLES
WHERE
    TABLE_TYPE = 'BASE TABLE'
        AND TABLE_SCHEMA NOT IN ('information_schema' , 'performance_schema', 'mysql', 'sys')
        AND TABLE_COLLATION <> 'utf8_general_ci';

字段級別

SELECT 
    TABLE_SCHEMA AS '庫名',
    DEFAULT_CHARACTER_SET_NAME AS '庫的字符集',
    DEFAULT_COLLATION_NAME AS '庫的校對字符集',
    TABLE_NAME AS '表名',
    TABLE_COLLATION AS '表的校對規則',
    COLUMN_NAME AS '字段名',
    CHARACTER_SET_NAME AS '字段的字符集',
    COLLATION_NAME AS '字段的校對規則'
FROM
    (SELECT 
        C2.TABLE_SCHEMA,
            C2.TABLE_NAME,
            C2.CHARACTER_SET_NAME,
            T2.TABLE_COLLATION,
            C2.COLUMN_NAME,
            C2.COLLATION_NAME,
            S2.DEFAULT_CHARACTER_SET_NAME,
            S2.DEFAULT_COLLATION_NAME,
            CASE
                WHEN
                    S2.DEFAULT_CHARACTER_SET_NAME = 'utf8'
                        AND S2.DEFAULT_COLLATION_NAME = 'utf8_general_ci'
                        AND (C2.CHARACTER_SET_NAME = 'utf8'
                        OR C2.CHARACTER_SET_NAME IS NULL)
                        AND (C2.COLLATION_NAME = 'utf8_general_ci'
                        OR C2.COLLATION_NAME IS NULL)
                        AND T2.TABLE_COLLATION = 'utf8_general_ci'
                THEN
                    'true'
                ELSE 'false'
            END STATUS
    FROM
        information_schema.COLUMNS AS C2
    JOIN (SELECT 
        TABLE_SCHEMA,
            TABLE_NAME,
            TABLE_TYPE,
            TABLE_ROWS,
            TABLE_COLLATION,
            CREATE_OPTIONS
    FROM
        information_schema.TABLES
    WHERE
        TABLE_TYPE = 'BASE TABLE'
            AND TABLE_SCHEMA NOT IN ('information_schema' , 'sys', 'performance_schema', 'mysql')) T2 ON C2.TABLE_NAME = T2.TABLE_NAME
        AND C2.TABLE_SCHEMA = T2.TABLE_SCHEMA
    JOIN (SELECT 
        SCHEMA_NAME,
            DEFAULT_CHARACTER_SET_NAME,
            DEFAULT_COLLATION_NAME
    FROM
        information_schema.SCHEMATA) S2 ON T2.TABLE_SCHEMA = S2.SCHEMA_NAME) charset_status
WHERE
    STATUS = 'false'
ORDER BY TABLE_SCHEMA , TABLE_NAME;
  • 表默認排序規則。輸出未顯式列出表默認字符集,但排序規則名稱以字符集名稱開頭。

相關文章
相關標籤/搜索