MySQL to view, modify the character set and Collation

Introduction
in the use of MySQL, the situation or the character set used Collation initial design does not meet the current demand may occur. As used in Table utf8 (i.e. utf8mb3 MySQL in utf8) To support Emoji, but does not support utf8mb3 emoji (emoji 4 bytes, but only supports up utf8mb3 3 bytes), it is necessary to modify the character set is utf8mb4 .

Collation i.e. aligned character sets, each character set has a corresponding arrangement of the one or more character sets. If a column uses utf8mb4_unicode_ci, when you need to use when searching emoji, because of utf8mb4_unicode_ci alternate characters, it is possible to find out erroneous data results.

Real examples
there is a need to generate a unique sequence of emoji demand in the company's projects. The development process did not find the problem, because there have been repeated insertion fails, but before inserting been queried database did not find duplicates in the testing process. To confirm whether the problem is the program directly into a sequence that does not exist by prisma, but still appeared in the case insert failure. Query the structure of the tables in the database and found the character set used is utf8mb4, confirmed the support of emoji. After nearly tangled half a day, find the arrangement of the character set for this column is utf8mb4_unicode_ci, the arrangement of character sets is considered certain emoji alternative cause this problem, modify the Collation column is resolved after utf8mb4_bin problem.

Get the current support for all character sets and Collation
MySQL provides SHOW CHARACTER SET command to view all of the characters, but also provides a command to view the SHOW COLLATION so the Collation. Meanwhile, MySQL also provides information SHOW CHARACTER SET LIKE 'charset-name ' and SHOW COLLATION WHERE Charset = 'charset- name' See specify the character set and character set specified all permutations of character sets.

- See all character set information
SHOW CHARACTER the SET;
- view the utf8 character set information
SHOW CHARACTER SET LIKE 'utf8';

- See all character sets are arranged
SHOW the COLLATION;
- See all permutations of the characters utf8
SHOW COLLATION WHERE Charset = 'utf8' ;
in MySQL, the entire character set and character set information array information_schema are stored in the library. In addition to the above method, further into the database view CHARACTER_SETS information_schema and COLLATIONS table.

Information_schema the USE;
- to see all character set information
the SELECT * the FROM CHARACTER_SETS;
- View all permutations of the character set information
SELECT * FROM COLLATIONS;
running MySQL Server, you can also use --character-set-server = charset- name and - -collation-server = collation-name parameter specifies the server default character set and collation.

$ Mysqld --character-set-server = utf8mb4 --collation-server = utf8mb4_bin
database-level
query all database character set
and the last section of said character set and character set of arrangement information are stored in the same information_schema library, each library , tables, columns are also stored therein. To find information about all libraries, query information_schema library SCHEMATA table can be obtained. SCHEMATA table included CATALOG_NAME, SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME and SQL_PATH five fields, character set and character set information which DEFAULT_CHARACTER_SET_NAME arrangement with DEFAULT_COLLATION_NAME is what we need.

SELECT SCHEMA_NAME 'database', DEFAULT_CHARACTER_SET_NAME ' charset', DEFAULT_COLLATION_NAME 'collation' FROM information_schema.SCHEMATA;
query and update the character set and Collation specified database
query specifying the database, the database can be queried on the basis of all of the increase in section SCHEMA_NAME = 'database -name 'conditions may be.

SELECT DEFAULT_CHARACTER_SET_NAME 'charset', DEFAULT_COLLATION_NAME ' collation' FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'database-name';
addition to the above methods, the further into the designated database, and view @@ character_set_database @@ collation_database the database queries are two variables character set and character set are arranged. The difference between the two approaches is the first information can be found in all the libraries in any database, while the second can only query the current library.

USE database-name;
for query operations in addition to the library, but also modify the character set and character set of the arrayed library. You can make changes to the database via ALTER DATABASE statement, you can specify the character set of the library, and the arrangement of character set changes. It should be noted, does not modify the database character set to bring COLLATE specify the character set arrangement, the arrangement will be set as the default character set arrangement character set character set. COLLATE not used directly to specify the character set, it will automatically modify the arrangement of characters corresponding to character set character set.

ALTER DATABASE database-name CHARACTER SET charset -name COLLATE collation-name;
a database table-level
query all tables
MySQL provides SHOW TABLES STATUS command, you can query all the information tables in the database. The method described above may also be used, directly query the table in TABLES information_schema library. TBALES table information included in all of the libraries of the database server, in order to query table information specified library, the library can be screened by name TABLE_SCHEMA field.

- Direct information_schema query data in
the SELECT * = the FROM information_schema.tables the TABLE_SCHEMA the WHERE 'Table-name';
- a lookup table information from the specified library
the USE-Database name;
SHOW TABLES the STATUS;
query and modify the table specifies
all the information in the database information_schema are stored in the table, you can directly query all database tables through the table, column information. However, the library requires higher security restrictions prevent malicious changes, the following will not continue to introduce directly by the library operation. Query library specified in the table all the way to query the same table, just add corresponding constraints can be.

Database-name the USE;
SHOW the STATUS the WHERE NAME TABLE the LIKE 'Table-name';
the same manner as the character set changes to the table and modify the library, the library simply modifying statements in the character set DATABASE TABLE to modify.

ALTER TABLE table-name CHARACTER SET charset -name COLLATE collation-name;
column-level
all the columns
in the specified library, use SHOW FULL COLUMNS FROM table-name statement can query the information in the table all columns.

Database-name the USE;
SHOW the COLUMNS the FROM FULL-Table name;
designated column
to query information specified column, may be added to limit Field field conditions in the above statement.

SHOW FULL COLUMNS FROM table-name WHERE Field = 'column-name'
modification statements in both the information listed above is slightly different, it should be noted that the modified table to specify columns in the statement. And the need to specify the type of the column, without modify.

ALTER TABLE table-name MODIFY column-name column-type CHARACTER SET charset-name COLLATE collation-name;

 

Programmer tools site: grassroots tool www.idevtool.com  

Personal Notes site: grassroots notes note.idevtool.com

Guess you like

Origin www.cnblogs.com/benpao/p/11519029.html