Table-level character set and collation
Each table has a default character set and a default collation.
Specify the character set and collation for a table
The CREATE TABLE and ALTER TABLE statements have options for specifying the character set and collation for a table. The syntax is as follows:
CREATE TABLE table_name (column_list)
[[DEFAULT] CHARACTER SET [=] charset_name]
[COLLATE [=] collation_name]
ALTER TABLE table_name
[DEFAULT] CHARACTER SET [=] charset_name
[COLLATE [=] collation_name]
The syntax for changing the character set and collation of existing data in a table is as follows:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name COLLATE col_name;
Here is an example:
CREATE TABLE test_collation (c1 INT, c2 VARCHAR(32), c3 VARCHAR(32), PRIMARY KEY (c1),UNIQUE KEY idx_test_collation_c2(c2));
Query OK, 0 rows affected (0.107 sec)
// Change the character set and collation of the table.
ALTER TABLE test_collation CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin;
Query OK, 0 rows affected (0.109 sec)
// Change the character set and collation of the existing data in the table and the corresponding settings.
ALTER TABLE test_collation CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
Query OK, 0 rows affected (0.025 sec)
Select the character set and collation for a table
seekdb selects the character set and collation for a table as follows:
-
If both
CHARACTER SET charset_nameandCOLLATE collation_nameare specified, the character setcharset_nameand collationcollation_nameare used. -
If
CHARACTER SET charset_nameis specified butCOLLATEis not, the character setcharset_nameand its default collation are used. To view the default collation for each character set, use theSHOW CHARACTER SETstatement. -
If
COLLATE collation_nameis specified butCHARACTER SETis not, the character set associated withcollation_nameand the specified collation are used. -
If neither
CHARACTER SETnorCOLLATEis specified, the character set and collation of the database are used.
If the character set and collation are not specified for individual column definitions, the character set and collation of the table are used as the default values for the column definitions.