699

What is the (default) charset for:

  • MySQL database

  • MySQL table

  • MySQL column

Pacerier
  • 76,400
  • 86
  • 326
  • 602
Rory
  • 48,706
  • 67
  • 174
  • 234
  • 1
    For tables and columns, simply use `SHOW CREATE TABLE`, as discussed in https://stackoverflow.com/questions/57628217/how-can-i-figure-out-the-default-charset-collation-in-my-mysql – Rick James Aug 23 '19 at 19:57

15 Answers15

808

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";
Manuel Jordan
  • 11,959
  • 15
  • 69
  • 111
Zenshai
  • 9,197
  • 2
  • 17
  • 18
  • 4
    Should be noted that information_schema is only in MySQL 5 onwards I believe. – Vex Jun 26 '09 at 16:54
  • 4
    As far as I can tell the closest you can get to retrieving column specific character set information in MySQL < 5 is to do SHOW FULL COLUMNS FROM tableName – Robin Winslow Jun 07 '11 at 18:03
  • 17
    This answer was very helpful, but if you want to trouble shoot a character_set / collation issue you would probably also need to check connection character_set, client_character_set etc... : SHOW VARIABLES LIKE 'character_set%'; SHOW VARIABLES LIKE 'collation%'; – BenL Feb 06 '15 at 09:54
  • 2
    the table operation returns "Empty set (0.00 sec)" for me – resgef Dec 02 '15 at 17:26
  • 13
    For Schema-Dummies: Note, that `schemaname` may just be the database name. – BurninLeo Dec 17 '17 at 21:12
  • Great answer, thanks. But I also was disoriented with `schemaname`. In fact it was just a `database name` :) – Paul Basenko Jun 06 '18 at 15:07
486

For columns:

SHOW FULL COLUMNS FROM table_name;
Pacerier
  • 76,400
  • 86
  • 326
  • 602
serhat
  • 4,885
  • 1
  • 12
  • 2
  • 44
    Hello, this is the future speaking! For anyone checking this answer, this method only shows Collation, rather than charset. I believe this changed at MySQL 5. (See answer with more points for a better method). – fideloper Jan 28 '14 at 17:40
  • 25
    @fideloper, **With the collation you can tell the charset.** That is because the first part of collation includes the charset, e.g. if the collation is `latin1_swedish_ci`, the charset can't be anything else besides `latin1`. If the collation is `utf8mb4_general_ci`, the charset can't be anything else besides `utf8mb4`. – Pacerier Aug 20 '15 at 03:31
  • 1
    This tells you the table character set. It doesn't tell you the character set of tables created in the future when no character set is specified in the create table syntax (you'll need the schema character set for that). – HoldOffHunger Aug 29 '16 at 15:28
223

For databases:

USE your_database_name;
show variables like "character_set_database";
-- or:
-- show variables like "collation_database";

Cf. this page. And check out the MySQL manual

Pacerier
  • 76,400
  • 86
  • 326
  • 602
J. Polfer
  • 11,281
  • 9
  • 52
  • 81
168

For all the databases you have on the server:

mysql> SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;

Output:

+----------------------------+---------+--------------------+
| database                   | charset | collation          |
+----------------------------+---------+--------------------+
| information_schema         | utf8    | utf8_general_ci    |
| my_database                | latin1  | latin1_swedish_ci  |
...
+----------------------------+---------+--------------------+

For a single Database:

mysql> USE my_database;
mysql> show variables like "character_set_database";

Output:

    +----------------------------+---------+
    | Variable_name              |  Value  |
    +----------------------------+---------+
    | character_set_database     |  latin1 | 
    +----------------------------+---------+

Getting the collation for Tables:

mysql> USE my_database;
mysql> SHOW TABLE STATUS WHERE NAME LIKE 'my_tablename';

OR - will output the complete SQL for create table:

mysql> show create table my_tablename


Getting the collation of columns:

mysql> SHOW FULL COLUMNS FROM my_tablename;

output:

+---------+--------------+--------------------+ ....
| field   | type         | collation          |
+---------+--------------+--------------------+ ....
| id      | int(10)      | (NULL)             |
| key     | varchar(255) | latin1_swedish_ci  |
| value   | varchar(255) | latin1_swedish_ci  |
+---------+--------------+--------------------+ ....
Nabeel Ahmed
  • 14,549
  • 4
  • 50
  • 54
  • What version of mysql show this output for `type` ? I have mysql 5.7.9 and `type` show the `data type` of the column not the `character set`. Some like `int(10)` `varchar(255)` ... etc and not `utf8` – MTK Feb 21 '18 at 23:29
  • My output shows `type` as the `data type` as well @MTK, perhaps above is a copy paste error in the "output:" section. – tronmcp Feb 02 '19 at 02:16
68

For tables:

SHOW TABLE STATUS will list all the tables.

Filter using:

SHOW TABLE STATUS where name like 'table_123';
Pacerier
  • 76,400
  • 86
  • 326
  • 602
Vex
  • 1,389
  • 12
  • 20
  • 9
    Please note. The collation shown in the show table status is not the character set of the table. The collation tells you how the characters are sorted / compared. e.g. utf8_bin_ci compares data without regarding the case (case insensitive, so "m" and "M" are the same), utf8_bin_cs compares with case sensitivity (so "m" and "M" are distinct). This is not the same as the character set of a table. – Daan Oct 08 '14 at 07:43
  • 7
    @Daan, Stop spreading misinformation. See http://stackoverflow.com/questions/1049728/how-do-i-see-what-character-set-a-mysql-database-table-column-is/1049776#comment52112197_4805510 , with the collation you can tell the charset. – Pacerier Aug 20 '15 at 03:56
45

For databases:

Just use these commands:

USE db_name;
SELECT @@character_set_database;
-- or:
-- SELECT @@collation_database;
Raphael
  • 59
  • 1
  • 2
  • 9
DC-
  • 761
  • 7
  • 12
  • This is the answer from the official mysql doc. https://dev.mysql.com/doc/refman/8.0/en/charset-database.html – Rick Jul 30 '20 at 03:13
37
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
;
Eric
  • 371
  • 3
  • 2
  • 2
    Very nice, Eric. Just paste that code into the mysql command line, hit return and you get the character set of every column in every table in every database :) – Jerry Krinock Oct 01 '16 at 16:48
  • 1
    @JerryKrinock You get every columns of the current database and nothing if no database is selected. – Ortomala Lokni Apr 27 '18 at 08:12
35

To see default collation of the database:

USE db_name;
SELECT @@character_set_database, @@collation_database;

To see collation of the table:

SHOW TABLE STATUS where name like 'table_name';

To see collation of the columns:

SHOW FULL COLUMNS FROM table_name;

To see the default character set of a table

SHOW CREATE TABLE table_name;
user1012513
  • 1,269
  • 12
  • 11
29

I always just look at SHOW CREATE TABLE mydatabase.mytable.

For the database, it appears you need to look at SELECT DEFAULT_CHARACTER_SET_NAME FROM information_schema.SCHEMATA.

chaos
  • 115,791
  • 31
  • 292
  • 308
  • 3
    in mysql databases can have default character sets – James Jun 26 '09 at 15:28
  • 4
    `select default_character_set_name from information_schema.schemata` is not enough because you can't tell which row correlate with which database. Use `select default_character_set_name,schema_name from information_schema.schemata` or simply: **`select*from information_schema.schemata`**. – Pacerier Aug 20 '15 at 04:10
  • I used `SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '' \G;` And it worked great :) Thanks! – Sankalp Nov 06 '15 at 10:41
22

For tables and columns:

show create table your_table_name
Pacerier
  • 76,400
  • 86
  • 326
  • 602
James
  • 3,161
  • 4
  • 19
  • 26
  • 3
    It tells you the full SQL that would be used to create the table as it currently stands, which should include it's character set. – James Jul 14 '12 at 18:09
  • Also, if the column doesn't state a particular charset, then it is using the table's default charset. – Pacerier Aug 20 '15 at 04:03
18

For databases:

SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;

Example output:

mysql> SELECT SCHEMA_NAME 'database', default_character_set_name 'charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;
+----------------------------+---------+--------------------+
| database                   | charset | collation          |
+----------------------------+---------+--------------------+
| information_schema         | utf8    | utf8_general_ci    |
| drupal_demo1               | utf8    | utf8_general_ci    |
| drupal_demo2               | utf8    | utf8_general_ci    |
| drupal_demo3               | utf8    | utf8_general_ci    |
| drupal_demo4               | utf8    | utf8_general_ci    |
| drupal_demo5               | latin1  | latin1_swedish_ci  |

...

+----------------------------+---------+--------------------+
55 rows in set (0.00 sec)

mysql> 
Pacerier
  • 76,400
  • 86
  • 326
  • 602
sjas
  • 15,508
  • 11
  • 75
  • 80
  • Isn't this a repeat of [the first answer on top](http://stackoverflow.com/a/1049958/632951) and http://stackoverflow.com/a/1049753/632951 ? – Pacerier Aug 20 '15 at 04:18
  • 1
    @Pacerier did you actually compare my answer to these properly? – sjas Sep 01 '15 at 12:43
15

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

Pacerier
  • 76,400
  • 86
  • 326
  • 602
amenko
  • 159
  • 1
  • 5
  • 3
    [Citation needed](http://xkcd.com/285) for *"default character set/collation is always LATIN"* in MySQL. – Pacerier Aug 20 '15 at 04:20
  • Citation needed? Have you ever used a MySQL database? Everyone knows that the default character set/collation is `latin1_swedish_ci` because Monty Widenius, the creator of MySQL, is Swedish and was not thinking as Big Picture as he should have when he started. – Spencer Williams Feb 21 '17 at 20:07
5

As many wrote earlier, SHOW FULL COLUMNS should be the preferred method to get column information. What's missing is a way to get charset after that without reaching metadata tables directly:

SHOW FULL COLUMNS FROM my_table WHERE Field = 'my_field'
SHOW COLLATION WHERE Collation = 'collation_you_got'
WowPress.host
  • 134
  • 1
  • 6
2

For database : USE db_name; SELECT @@character_set_database;

0

show global variables where variable_name like 'character_set_%' or variable_name like 'collation%'

Naveen Yalla
  • 123
  • 1
  • 11