在 MySQL 中设置列​​字符集?

mysqlmysqli database

使用字符集命令设置列字符集。语法如下 −

ALTER TABLE yourTableName MODIFY youColumName type CHARACTER SET anyCharcaterSetName;

您可以使用字符集名称 utf8 或其他名称。要设置列字符集,让我们首先创建一个表。创建表的查询如下 −

mysql> create table setCharsetDemo
   −> (
   −> FirstName varchar(60)
   −> );
Query OK, 0 rows affected (2.09 sec)

现在您可以借助 show 命令检查当前列字符集。查询如下 −

mysql> show create table setCharsetDemo;

以下是输出 −

+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table          | Create Table                                                                                                                                                           |
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| setCharsetDemo | CREATE TABLE `setcharsetdemo` (`FirstName` varchar(60) COLLATE utf8mb4_unicode_ci DEFAULT NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci |
+----------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

现在您可以查看上面的示例输出。默认字符集是 utf8mb4。让我们将字符集设置为 utf8mb4 以外的字符集。

我们将字符集设置为 utf8。查询如下 −

mysql> alter table setCharsetDemo modified FirstName varchar(60) CHARACTER SET utf8;
Query OK, 0 rows impacted, 1 warning (2.81 sec)
Records: 0 Duplicates: 0 Warnings: 1

让我们检查字符集是否已更改。查询如下 −

mysql> show create table setCharsetDemo;

以下输出显示我们已成功更新字符集−

+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table          | Create Table                                                                                                                                                                          |
+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| setCharsetDemo | CREATE TABLE `setcharsetdemo` (`FirstName` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci |
+----------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

相关文章