mysql character set operation

 

 

1. Check the character set encoding settings

show variables like '%character%';

   

 

 

     

 

2. Set the character set encoding

set names 'utf8';

 

 

Equivalent to simultaneously:

 

  

set character_set_client = utf8;

set character_set_results = utf8;

set character_set_connection = utf8;

 

 

 

 

3. Create a database and set character set encoding and checking.

 

CREATE DATABASE IF NOT EXISTS <dbName> DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

 

 

 

By default:

Database server encoding -> database encoding -> data table encoding -> field encoding

 

Therefore, the subsequent creation of data tables and fields will be encoded in the database character set by default.

 

 

4. Modify the character set encoding/checking of the database

ALTER DATABASE <dbName> CHARACTER SET utf8 COLLATE utf8_general_ci;

 

 

The modified encoding only affects the default encoding of the table created later, and has no effect on the existing table.

 

5. Modify the character set encoding/checking of the data table

 

SHOW FULL COLUMNS FROM <tableName>;

    # View collation type

 

5.1 Only the character set of the table is modified, which affects the default definition of the new columns in the table, and the character set of the existing columns is not affected.

 

alter table <tableName> character set utf8

 

 

5.2 Modify the character set of the table and the character set of the existing column, and convert the existing data to the character set encoding

alter table <tableName> convert to character set utf8 collate utf8_unicode_ci;

 

 

6. The relationship between check and character set

 

Proofreading Rules Features:

 

  ①Two different character sets cannot have the same collation rules;

 

  ②Each character set has a default collation rule;

 

  ③ There is a naming convention for proofreading rules: it starts with its relevant character set name, includes a language name in the middle, and ends with _ci (case-insensitive), _cs (case-sensitive), or _bin (binary).

 

Notice:

 

  The system uses the utf8 character set. If the utf8_bin collation rule is used to execute SQL queries, it is case-sensitive, and utf8_general_ci is case-insensitive (the default collation rule corresponding to the utf8 character set is utf8_general_ci).

 

7. When modifying the character set encoding, if the check is not set, the system will automatically set the default check, and the default check is case-insensitive.

 

If no check is set, the default check will be set according to the character set encoding

 

ALTER DATABASE <dbName> CHARACTER SET utf8 COLLATE utf8_general_ci;

 

Equivalent to

 

ALTER DATABASE <dbName> CHARACTER SET utf8;

 

 

 

Reference document: https://www.cnblogs.com/geaozhang/p/6724393.html?utm_source=itdadao&utm_medium=referral

 

 

 

 

 

Guess you like

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