Potential risks of modifying the default character set of a table in mysql

Source: Internet
Author: User

Potential risks arising from modifying the default character set of a mysql TABLE if you need to modify the default character set of the TABLE after creating the TABLE, you can use alter table to modify the default character set of the TABLE. alter table tablename default character set utf8; but there is a problem, only the default encoding defined in the table is changed, and the existing fields in each column are still encoded in the past. The existing table data is not converted into encoding. Mysql> create table mybig5 (id int not null auto_increment primary key,-> subject varchar (100) engine = innodb default charset big5; Query OK, 0 rows affected (0.81 sec) mysql> show create table mybig5; + -------- + tables ------------------------------------- + | Table | Create Table | + -------- + Response ------------------------------- + | mybig5 | create table 'mybig5' ('id' int (11) not null AUTO_INCREMENT, 'subobject' varchar (100) default null, primary key ('id') ENGINE = InnoDB default charset = big5 | + -------- + ---------------------------------------------------- Rows + 1 row in set (0.00 sec) mysql> alter table mybig5 default charset utf8; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 then we INSERT a multi-byte string record to the table mysql> insert into mybig5 VALUES (NULL, UNHEX ('e7bb8fe79086 '); 01:08:19 [INSERT-0 row (s ), 0.000 secs] [Error Code: 1366, S QL State: HY000] Incorrect string value: '\ xE7 \ xBB \ x8F \ xE7 \ x90 \ x86' for column 'subobject' at row 1... 1 statement (s) executed, 0 row (s) affected, exec/fetch time: 0.000/0.000 sec [0 successful, 0 warnings, 1 errors] mysql> show create table mybig5; + -------- + certificate ----------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------ + | Table | Create Table | + -------- + partition ratio + | mybig5 | create table 'mybig5' ('id' int (11) not null AUTO_INCREMENT, 'subobject' varchar (100) character set big5 default null, primary key ('Id ') ENGINE = InnoDB default charset = utf8 | + -------- + bytes ---------------------------------------------------- + 1 row in set (0.00 sec). We can see whether the column subject is big5 encoded, but not utf8. We can use 2. alter table tablename convert to character set utf8; TO modify the column encoding mysql> show create table mybig5; mysql> + -------- + response ------------------------------- +-> | Table | Create Table |-> + -------- + ignore tables +-> | mybig5 | create table 'mybig5' (-> 'id' int (11) not null AUTO_INCREMENT,-> 'subobject' varchar (100) default null,-> primary key ('id')->) ENGINE = InnoDB default charset = utf8 |-> + -------- + bytes ------------------------------------- +-> 1 row in set (0.00 sec), but pay attention to the conversion of existing content encoding, will not become garbled
 

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.