Query to show all tables and their collation

MysqlSchemaCollation

Mysql Problem Overview


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.

Mysql Solutions


Solution 1 - Mysql

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

Solution 2 - Mysql

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;

Solution 3 - Mysql

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

SHOW TABLE STATUS FROM your_db_name;

Solution 4 - Mysql

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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParris VarneyView Question on Stackoverflow
Solution 1 - MysqlJoe StefanelliView Answer on Stackoverflow
Solution 2 - MysqlThiagoPXPView Answer on Stackoverflow
Solution 3 - MysqlAndrinuxView Answer on Stackoverflow
Solution 4 - MysqlEmrah SaglamView Answer on Stackoverflow