Modification of database or table encoding

Modification of database or table encoding

During the development process, many people did not specify the encoding method when creating the database, resulting in an error in inserting Chinese into the table. At this time, you need to modify the database encoding or the encoding method of the table. The following is a specific command for how to modify it.

1
View the current database encoding format:

show variables like'character_set_database';

2 View the encoding format of the data table:

show create table <table name>;

3
Specify the character set of the database when creating the database:

create database <database name> character set utf8;

4
Specify the encoding format of the data table when creating the data table:

create table tb_books (

name varchar(45) not null,

price double,

bookCount int,

author varchar(45)) default charset = utf8);


5
Modify the encoding format of the database:

alter database <database name> character set utf8;

6 Convert the encoding format of the specific table to utf8:

alter table <table name> CONVERT TO CHARACTER SET utf8;


7
Modify the field encoding format:

alter table <table name> change <field name> <field name> <type> character set utf8;
For more Python learning materials, please pay attention to: itheimaGZ