mysql modify the character set of tables, fields, and libraries

MySQL modify the character set of tables, fields, and libraries (transfer)

Original link: http://fatkun.com/2011/05/mysql-alter-charset.html

There are four levels of default character set settings in MySQL: server level, database level, table level. Ultimately it's field-level charset settings. Note that the first three are the default settings, and don't code that your fields will end up using this charset setting. So we recommend to use show create table table; or show full fields from tableName; to check the character set settings of the fields in the current table.


If the field character sets of the two tables are inconsistent, the following error will be reported:

ERROR 1267 (HY000): Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (latin1_swedish_ci,COERCIBLE) for operation '='



Solution

show create table table1;

show create table table2;

Check whether the character set of its fields is the same, if it is different, directly change it to the same by the following method to solve the problem




Modify the database character set:



ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...]


; All character columns (CHAR, VARCHAR, TEXT) are changed to the new character set:



ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name [COLLATE ...]
such as: ALTER TABLE logtest CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;


just modify the default character set of the table:



ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name [COLLATE...];
such as : ALTER TABLE logtest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


modify the character set of the field:



ALTER TABLE tbl_name CHANGE c_name c_name CHARACTER SET character_name [COLLATE ...];
such as: ALTER TABLE logtest CHANGE title title VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci ;


View database code:



SHOW CREATE DATABASE db_name;


View table code:



SHOW CREATE TABLE tbl_name;


View field code:



SHOW FULL COLUMNS FROM tbl_name;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326468475&siteId=291194637