-- View all character encoding SHOWCHARACTERSET; -- view the instructions for creating a database and the database encoding showcreatedatabasedbtest; -- view the database encoding: showvariableslike % char %; -- set char -- View all character codes
Show character set;
-- View the commands for creating a database and the encoding used by the database
Show create database dbtest;
-- View the database encoding:
Show variables like '% char % ';
-- Set character_set_server, set character_set_client, and set character_set_results.
Set character_set_server = utf8; -- the default character set of the server. This statement can be modified successfully, but it will become invalid after the service is restarted. The fundamental method is to modify and configure the MYSQL file MY. INI, character_set_server = utf8, and configure it to the mysqld field.
Set character_set_client = gbk; -- character set of the statements from the client. The server uses the character_set_client variable as the character set used in the query sent by the client.
Set character_set_results = gbk; -- character set used to return query results to the client. The character_set_results variable indicates that the server returns the query result to the character set used by the client. Includes result data, such as column values and result metadata (such as column names ).
-- Set the database encoding method when creating a database
-- Character set: specifies the character set used by the database. UTF-8 cannot be written as UTF-8.
-- COLLATE: specifies the sorting rule of the database character set. the default sorting rule of utf8 is utf8_general_ci (viewed through show character set)
Drop database if EXISTS dbtest;
Create database dbtest character set utf8 COLLATE utf8_general_ci;
-- Modify database encoding
Alter database dbtest character set gbk collate gbk_chinese_ci;
Alter database dbtest character set utf8 COLLATE utf8_general_ci;
-- When creating a table, set the table and Field Encoding
Use dbtest;
Drop table if exists tbtest;
Create table tbtest (
Id int (10) auto_increment,
User_name varchar (60) character set gbk collate gbk_chinese_ci,
Email varchar (60 ),
PRIMARY key (id)
) Character set utf8 COLLATE utf8_general_ci;
-- Modify table encoding
Alter table tbtest character set utf8 COLLATE utf8_general_ci;
-- Modify Field Encoding
Alter table tbtest MODIFY email VARCHAR (60) character set utf8 COLLATE utf8_general_ci;
-- View the encoding used by a field:
Select charset (email) FROM tbtest;