开发者

Query to show all tables and their collation

开发者_如何转开发Is there a query that can be run in mysql that shows all tables and their default collation? Even better if there was on that could show all collations on all columns of all tables.


SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLLATION_NAME
    FROM INFORMATION_SCHEMA.COLUMNS


Bear in mind that collation can be defined to tables and also to columns.

A column's collation might be different to its parent table. Here is a query to get the collation from tables (not columns)

SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES;


Output information(status) about all tables in the database as "phpmyadmin":

SHOW TABLE STATUS FROM your_db_name;


Run this to see columns that not matching database collation. If you want to use these columns in your query and conditions you have to use COLLATE hint to match.

SELECT 
     CLM.[TABLE_CATALOG]
    ,CLM.[TABLE_SCHEMA]
    ,CLM.[TABLE_NAME]
    ,CLM.[COLUMN_NAME]
    ,CASE
        WHEN CLM.[CHARACTER_MAXIMUM_LENGTH] IS NOT NULL THEN CLM.[DATA_TYPE] + '(' + CASE WHEN CLM.[CHARACTER_MAXIMUM_LENGTH] = '-1' THEN 'max' ELSE CAST(CLM.[CHARACTER_MAXIMUM_LENGTH] AS NVARCHAR(10)) END + ')'
        ELSE CLM.[DATA_TYPE]
     END AS [DATA_TYPE]
    ,CLM.[CHARACTER_SET_NAME]
    ,CLM.[COLLATION_NAME]
--  ,CLM.* 
FROM INFORMATION_SCHEMA.COLUMNS CLM (NOLOCK)
    LEFT JOIN sys.databases DBS (NOLOCK) ON CLM.[TABLE_CATALOG] = DBS.[name]
WHERE CLM.[COLLATION_NAME] IS NOT NULL AND CLM.[COLLATION_NAME] != DBS.[collation_name]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜