在 MySQL 中设置列字符集?
使用 CHARSET 命令设置列字符集。语法如下所示:
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 modify FirstName varchar(60) CHARACTER SET utf8; Query OK, 0 rows affected, 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)
广告