Use SQL statements to operate MYSQL character encoding

Source: Internet
Author: User

Use SQL statements to operate MYSQL character encoding
-- 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;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.