Mysql character encoding method

Source: Internet
Author: User

Setting character encoding in Mysql can solve some common problems. For example, if an error occurs when using different character sets, the following describes how to set character encoding in Mysql to solve this problem.

The error is that there are two character sets in your result set.

For example, if you perform a joint query on two tables, the character set of one table is latin1 and the other is utf8,
In this way, there are two character sets in your result set. mysql will report an error.

It is also true that different fields in a table use different character sets.

Use show create table table_name; to see the specific character set settings.

I checked the help manual, saying that the user's character set is not set. The default is utf8, And it is converted to the latin1 or gb2312 character set.

Solution:

Convert different character sets into a unified character set. The following describes how to set character encoding for Mysql.

 
 
  1. After   an   upgrade   to   MySQL   4.1,   the   statement   fails:       
  2.  
  3. mysql>   SELECT   SUBSTRING_INDEX(USER(),'@',1);     
  4.  
  5. ERROR   1267   (HY000):   Illegal   mix   of   collations     
  6.  
  7. (utf8_general_ci,IMPLICIT)   and   (latin1_swedish_ci,COERCIBLE)     
  8.  
  9. for   operation   'substr_index'     
  10.  
  11. The   reason   this   occurs   is   that   usernames   are   stored   using   UTF8   (see   section   11.6   UTF8   for   Metadata).   As   a   result,   the   USER()   function   and   the   literal   string   '@'   have   different   character   sets   (and   thus   different   collations):       
  12.  
  13. mysql>   SELECT   COLLATION(USER()),   COLLATION('@');     
  14.  
  15. +-------------------+-------------------+     
  16.  
  17. |   COLLATION(USER())   |   COLLATION('@')         |     
  18.  
  19. +-------------------+-------------------+     
  20.  
  21. |   utf8_general_ci       |   latin1_swedish_ci   |     
  22.  
  23. +-------------------+-------------------+     
  24.  
  25. One   way   to   deal   with   this   is   to   tell   MySQL   to   interpret   the   literal   string   as   utf8:       
  26.  
  27. mysql>   SELECT   SUBSTRING_INDEX(USER(),_utf8'@',1);     
  28.  
  29. +------------------------------------+     
  30.  
  31. |   SUBSTRING_INDEX(USER(),_utf8'@',1)   |     
  32.  
  33. +------------------------------------+     
  34.  
  35. |   root                                                               |     
  36.  
  37. +------------------------------------+     
  38.  
  39. Another   way   is   to   change   the   connection   character   set   and   collation   to   utf8.   You   can   do   that   with   SET   NAMES   'utf8'   or   by   setting   the   character_set_connection   and   collation_connection   system   variables   directly.       

You can use MySQL Version> 4.12 for table encoding and conversion)

 
 
  1. ALTER   TABLE   tbl_name   CONVERT   TO   CHARACTER   SET   charset_name;    

Earlier versions can be used:

 
 
  1. ALTER   TABLE   tbl_name   CHARACTER   SET   charset_name; 

Statement for modifying fields in mysql

How to delete multiple tables in MySQL

How MySQL connects to remote SQL

MySQL cluster introduction and Configuration

Special notes for MySQL 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.