在MySQL中设置列字符集?
使用字符SET命令设置列字符集。语法如下-
ALTER TABLE yourTableName MODIFY youColumName type CHARACTER SET anyCharcaterSetName;
您可以使用字符集名称utf8或其他elsE。要设置列字符集,让我们首先创建一个表。创建表的查询如下-
mysql> create table setCharsetDemo −> ( −> FirstName varchar(60) −> );
现在,您可以在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; 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)