如何查看 MySQL 数据库/表/列的字符集?


要检查 MySQL 数据库/表/列的字符集,让我们看一个例子:使用一个名为“business”的数据库,其中已经存在名为“student”的表。

检查表字符集的语法。

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 = "yourDatabaseName"
AND T.table_name = "yourTableName";

应用上述语法显示表 student 的字符集。

mysql> 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 = "business"
   -> AND T.table_name = "student";

以下是输出结果。

+--------------------+
| CHARACTER_SET_NAME |
+--------------------+
| utf8mb4            |
+--------------------+
1 row in set (0.13 sec)

要查找数据库的字符集,请使用以下语法。

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

应用上述语法查看数据库的字符集。

mysql> SELECT default_character_set_name FROM information_schema.SCHEMATA
   -> WHERE schema_name = "business";

以下是输出结果。

+----------------------------+
| DEFAULT_CHARACTER_SET_NAME |
+----------------------------+
| utf8mb4                    |
+----------------------------+
1 row in set (0.00 sec)

要查找列的字符集。

SELECT character_set_name FROM information_schema.`COLUMNS`
   WHERE table_schema = "yourDatabaseName"
   AND table_name = "yourTableName"
   AND column_name = "yourColumnName";

应用上述语法查看“business”数据库中“student”表的列名“Name”的字符集。

mysql> SELECT character_set_name FROM information_schema.`COLUMNS`
   -> WHERE table_schema = "business"
   ->  AND table_name = "student"
   -> AND column_name = "Name";

以下是输出结果。

+--------------------+
| CHARACTER_SET_NAME |
+--------------------+
| utf8mb4            |
+--------------------+
1 row in set (0.00 sec)

更新于:2019-07-30

4K+ 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告