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

mysqlmysqli database

要检查 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";

应用上述语法来查看"student"表和"business"数据库的列名'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)

相关文章