每个 “字符” 列(即 CHAR、VARCHAR、TEXT 类型,或任何同义词列)都具有列字符集和列排序规则。CREATE TABLE 和 ALTER TABLE 的列定义语法具有可选子句,用于指定列字符集和排序规则:Every “character” column (that is, a column of type
CHAR,
VARCHAR, a
TEXT type, or any synonym) has a
column character set and a column collation. Column definition
syntax for CREATE TABLE and
ALTER TABLE has optional clauses
for specifying the column character set and collation:
col_name{CHAR | VARCHAR | TEXT} (col_length) [CHARACTER SETcharset_name] [COLLATEcollation_name]
这些子句也可以用于 ENUM 和 SET 列:These clauses can also be used for
ENUM and
SET columns:
col_name{ENUM | SET} (val_list) [CHARACTER SETcharset_name] [COLLATEcollation_name]
示例:Examples:
CREATE TABLE t1
(
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_german1_ci
);
ALTER TABLE t1 MODIFY
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
MySQL 以以下方式选择列的字符集和排序规则:MySQL chooses the column character set and collation in the following manner:
如果同时指定了
CHARACTER SET charset_name和COLLATE collation_name,将使用字符集charset_name和校对规则collation_name。If bothCHARACTER SETandcharset_nameCOLLATEare specified, character setcollation_namecharset_nameand collationcollation_nameare used.CREATE TABLE t1 ( col1 CHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ) CHARACTER SET latin1 COLLATE latin1_bin;该列指定了字符集和校对规则,因此将被使用。该列的字符集为
utf8mb4,校对规则为utf8mb4_unicode_ci。The character set and collation are specified for the column, so they are used. The column has character setutf8mb4and collationutf8mb4_unicode_ci.如果指定了
CHARACTER SET charset_name而没有指定COLLATE,将使用字符集charset_name及其默认校对规则。IfCHARACTER SETis specified withoutcharset_nameCOLLATE, character setcharset_nameand its default collation are used.CREATE TABLE t1 ( col1 CHAR(10) CHARACTER SET utf8mb4 ) CHARACTER SET latin1 COLLATE latin1_bin;该列指定了字符集,但未指定校对规则。该列的字符集为
utf8mb4,默认校对规则为utf8mb4的utf8mb4_0900_ai_ci。要查看每个字符集的默认校对规则,请使用SHOW CHARACTER SET语句或查询INFORMATION_SCHEMACHARACTER_SETS表。The character set is specified for the column, but the collation is not. The column has character setutf8mb4and the default collation forutf8mb4, which isutf8mb4_0900_ai_ci. To see the default collation for each character set, use theSHOW CHARACTER SETstatement or query theINFORMATION_SCHEMACHARACTER_SETStable.如果指定了
COLLATE collation_name而没有指定CHARACTER SET,则使用与collation_name相关联的字符集和排序规则collation_name。IfCOLLATEis specified withoutcollation_nameCHARACTER SET, the character set associated withcollation_nameand collationcollation_nameare used.CREATE TABLE t1 ( col1 CHAR(10) COLLATE utf8mb4_polish_ci ) CHARACTER SET latin1 COLLATE latin1_bin;列指定了排序规则,但未指定字符集。该列的排序规则为
utf8mb4_polish_ci,字符集为与该排序规则关联的字符集,即utf8mb4。The collation is specified for the column, but the character set is not. The column has collationutf8mb4_polish_ciand the character set is the one associated with the collation, which isutf8mb4.否则(未指定
CHARACTER SET或COLLATE),则使用表的字符集和排序规则。Otherwise (neitherCHARACTER SETnorCOLLATEis specified), the table character set and collation are used.CREATE TABLE t1 ( col1 CHAR(10) ) CHARACTER SET latin1 COLLATE latin1_bin;列未指定字符集或排序规则,因此使用表的默认值。该列的字符集为
latin1,排序规则为latin1_bin。Neither the character set nor collation is specified for the column, so the table defaults are used. The column has character setlatin1and collationlatin1_bin.
CHARACTER SET 和 COLLATE 子句是标准 SQL。The CHARACTER SET and
COLLATE clauses are standard SQL.
如果你使用 ALTER TABLE 将某列从一个字符集转换为另一个字符集,MySQL 会尝试映射数据值,但如果字符集不兼容,可能会导致数据丢失。If you use ALTER TABLE to convert
a column from one character set to another, MySQL attempts to
map the data values, but if the character sets are incompatible,
there may be data loss.