MySQL modifies the character set of the database

Source: Internet
Author: User

One of the principles----Modifying the database character set is that the new character set contains the font to >= the original character set font, otherwise the existing data may become garbled.

If there is no data in the database for the time being, we can execute the command directly. If a table is present in the database, the table's character set is not changed as the character set of the database changes.

ALTER DATABASE dbname Character Set Charset_name

If there is data in the database, you can actually execute the command, but it will leave a problem, that is, the character set of the table of the previously existing data, but only for the newly added table or record to take effect.

In this case, we can modify the character set by another method.

Simulate the environment, simulate changing the character set of database A from latin1 to GBK (the parameters in the following statement are the specific meanings of your own)

1.export the existing table structure in a database (table A is present in a database)

Mysqldump-uroot-p--default-character-set=gbk-d A >table_create.sql

2. Manually set the table structure in Table_create.sql to GBK

The specific is:

CREATE TABLE ' A ' (  ' id ' int (one) default NULL) Engine=innodb default CHARSET=GBK;

3. Verify that the application is no longer modifying data for all tables in a database, and then export all data

Mysqldump-uroot-p--quick--no-create-info--extended-insert--default-character-set =latin1 A>A_data.sql

4. Open A_data.sql and modify the set NAMES latin1 to set NAMES GBK.

/*!40101 SET NAMES GBK */;

5. Delete the previous database

6. Create a database with the same name using the new character set

Create DATABASE A default CharSet GBK;

7. Use Table_create.sql to create a table

Mysql-uroot-p A <table_create.sql

8.import data, using A_data.sql

Mysql-uroot-p A <a_data.sql

So far, we have completely modified the character set of a database from Latin1 to GBK

MySQL modifies the character set of the database

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.