Official document address: 10.3.5 Column Character Set and Collation
Each "character" column (that is, type CHAR
, VARCHAR
, TEXT
type, or any synonyms column) has a character set and a column collation column. CREATE TABLE
And ALTER TABLE
column definition syntax character set and collation of the specified column optional clause:
col_name {
CHAR | VARCHAR | TEXT} (col_length)
[CHARACTER SET charset_name]
[COLLATE collation_name]
These clauses can also be used for ENUM
sum SET
columns:
col_name {
ENUM | SET} (val_list)
[CHARACTER SET charset_name]
[COLLATE collation_name]
Example:
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 uses the following methods to select column character sets and sorting:
- If the
CHARACTER SET charset_name
sumCOLLATE collation_name
is specified at the same time , the character setcharset_name
and collation are usedcollation_name
.
CREATE TABLE t1
(
col1 CHAR(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci
) CHARACTER SET latin1 COLLATE latin1_bin;
The character set and collation are specified for the column, so they are used. The column has a character set utf8
and collation utf8_unicode_ci
.
- If the specified is
CHARACTER SET charset_name
not specifiedCOLLATE
, the character setcharset_name
and its default collation are used .
CREATE TABLE t1
(
col1 CHAR(10) CHARACTER SET utf8
) CHARACTER SET latin1 COLLATE latin1_bin;
The character set is specified for the column, but the collation is not specified. The column has a default collation of utf8
character set and utf8
, ie utf8_general_ci
. To view the default collation of each character set, you can use a SHOW CHARACTER SET
statement or lookup INFORMATION_SCHEMA
CHARACTER_SETS
table.
- If there is only
COLLATE collation_name
noneCHARACTER SET
, use thecollation_name
associated character set andcollation_name
collation.
CREATE TABLE t1
(
col1 CHAR(10) COLLATE utf8_polish_ci
) CHARACTER SET latin1 COLLATE latin1_bin;
The collation is specified for the column, but the character set is not specified. The column has a utf8_polish_ci
collation, and the character set is the character set associated with the collation, ie utf8
.
- If the character set and collation are not specified, the table character set and collation will be used.
CREATE TABLE t1
(
col1 CHAR(10)
) CHARACTER SET latin1 COLLATE latin1_bin;
No character set or collation is specified for the column, so the default character set or collation of the table is used. The column has a character set latin1
and collation latin1_bin
.
CHARACTER SET
The and COLLATE
clauses are standard SQL.
If you use to ALTER TABLE
convert a column from one character set to another character set, MySQL will try to map the data value, but if the character set is incompatible, it may cause data loss.