How do I see what character set a MySQL database / table / column is?

前端 未结 15 1452
失恋的感觉
失恋的感觉 2020-11-22 06:33

What is the (default) charset for:

  • MySQL database

  • MySQL table

  • MySQL column

相关标签:
15条回答
  • 2020-11-22 06:48

    Here's how I'd do it -

    For Schemas (or Databases - they are synonyms):

    SELECT default_character_set_name FROM information_schema.SCHEMATA 
    WHERE schema_name = "schemaname";
    

    For Tables:

    SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
           information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
    WHERE CCSA.collation_name = T.table_collation
      AND T.table_schema = "schemaname"
      AND T.table_name = "tablename";
    

    For Columns:

    SELECT character_set_name FROM information_schema.`COLUMNS` 
    WHERE table_schema = "schemaname"
      AND table_name = "tablename"
      AND column_name = "columnname";
    
    0 讨论(0)
  • 2020-11-22 06:48

    For databases:

    SHOW CREATE DATABASE "DB_NAME_HERE";
    

    In creating a Database (MySQL), default character set/collation is always LATIN, instead that you have selected a different one on initially creating your database

    0 讨论(0)
  • 2020-11-22 06:49

    For databases:

    Just use these commands:

    USE db_name;
    SELECT @@character_set_database;
    -- or:
    -- SELECT @@collation_database;
    
    0 讨论(0)
  • 2020-11-22 06:50

    For tables and columns:

    show create table your_table_name
    
    0 讨论(0)
  • 2020-11-22 06:53
    SELECT TABLE_SCHEMA,
           TABLE_NAME,
           CCSA.CHARACTER_SET_NAME AS DEFAULT_CHAR_SET,
           COLUMN_NAME,
           COLUMN_TYPE,
           C.CHARACTER_SET_NAME
      FROM information_schema.TABLES AS T
      JOIN information_schema.COLUMNS AS C USING (TABLE_SCHEMA, TABLE_NAME)
      JOIN information_schema.COLLATION_CHARACTER_SET_APPLICABILITY AS CCSA
           ON (T.TABLE_COLLATION = CCSA.COLLATION_NAME)
     WHERE TABLE_SCHEMA=SCHEMA()
       AND C.DATA_TYPE IN ('enum', 'varchar', 'char', 'text', 'mediumtext', 'longtext')
     ORDER BY TABLE_SCHEMA,
              TABLE_NAME,
              COLUMN_NAME
    ;
    
    0 讨论(0)
  • 2020-11-22 06:54

    For tables:

    SHOW TABLE STATUS will list all the tables.

    Filter using:

    SHOW TABLE STATUS where name like 'table_123';
    
    0 讨论(0)
提交回复
热议问题