首先,我们将借助version()
方法检查当前正在使用哪个MySQL版本-
查询如下-
mysql> SELECT version();
以下是输出
+-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec)
从上面的输出中可以看到,正在使用8.0.12版本。现在,我们可以使用以下语法检查当前字符编码-
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";
应用上面的查询-
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 = "sample" AND T.table_name = "mytable";
以下是输出:
+--------------------+ | CHARACTER_SET_NAME | +--------------------+ | utf8mb4 | +--------------------+ 1 row in set (0.00 sec)
这是我们可以为数据库设置utf8的查询-
mysql> ALTER DATABASE sample CHARACTER SET utf8 COLLATE utf8_general_ci;
让我们检查一下-
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 = "sample" AND T.table_name = "mytable";
以下是输出
+--------------------+ | CHARACTER_SET_NAME | +--------------------+ | utf8mb4 | +--------------------+ 1 row in set (0.00 sec)