10.3.5 Column character set and collation

Official document address: 10.3.5 Column Character Set and Collation


Each "character" column (that is, type CHAR, VARCHAR, TEXTtype, or any synonyms column) has a character set and a column collation column. CREATE TABLEAnd ALTER TABLEcolumn 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 ENUMsum SETcolumns:

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_namesum COLLATE collation_nameis specified at the same time , the character set charset_nameand collation are used collation_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 utf8and collation utf8_unicode_ci.

  • If the specified is CHARACTER SET charset_namenot specified COLLATE, the character set charset_nameand 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 utf8character set and utf8, ie utf8_general_ci. To view the default collation of each character set, you can use a SHOW CHARACTER SETstatement or lookup INFORMATION_SCHEMA CHARACTER_SETStable.

  • If there is only COLLATE collation_namenone CHARACTER SET, use the collation_nameassociated character set and collation_namecollation.
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_cicollation, 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 latin1and collation latin1_bin.

CHARACTER SETThe and COLLATEclauses are standard SQL.

If you use to ALTER TABLEconvert 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.

Guess you like

Origin blog.csdn.net/wb1046329430/article/details/114824911