MySQL illegal mix of collationsy exception causes and workarounds

Source: Internet
Author: User

Original December 25, 2008 11:54:00
    • Label:
    • MySQL/
    • Collation/
    • Character/
    • Variables/
    • Database/
    • Server

This exception was found today when using a cursor for a database temp table.
After finding the information, the final result. Here to share with you. Character set problem or must unify is the simplest.

Create temporary table TEMP2 (MC1 varchar) default ', MC2 varchar default ', MC3 varchar (default '), MC4 varchar (2 0) Default ', MC5 varchar (default ", ParentID varchar), levelnum int); --Generate temporary tables

Declare mycur67 CURSOR for select concat (replace (Space (levelnum-1), Space (1), '------'), MC1,MC2,MC3,MC4,MC5) as MC, ParentID as location from Temp2;

The exception information is:
Illegal mix of collations for operation ' Concat '

Remove the Concat code from the following section, and run the result as
Declare mycur67 CURSOR for select concat (replace (Space (levelnum-1), Space (1), '------'), MC1) as mc,parentid as location From Temp2;

Error: Illegal mix of collations (utf8_general_ci,coercible) and (gb2312_chinese_ci,implicit) for operation ' Concat '

visible, is caused by inconsistencies in the encoding type of the 2 fields.

When my database was created, the gb2312 encoding was selected, and the field Operation defaults to UTF8 encoding. Absolute uniform use of UTF-8

Create temporary table TEMP2 (MC1 varchar) default ', MC2 varchar default ', MC3 varchar (default '), MC4 varchar (2 0) Default ", MC5 varchar (default), ParentID varchar (+), Levelnum int) default Charset=utf8;

The running result is normal.

after finding the information, the following is very clear.
Mysql> Show variables like "%character%"; Show variables like "%collation%"; +--------------------------+----------------------------+
| variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | Latin1 |
| character_set_connection | Latin1 |
| Character_set_database | Latin1 |
| Character_set_results | Latin1 |
| Character_set_server | Latin1 |
| Character_set_system | UTF8 |
| Character_sets_dir | /usr/share/mysql/charsets/|
+--------------------------+----------------------------+
7 Rows in Set (0.00 sec)

+----------------------+-------------------+
| variable_name | Value |
+----------------------+-------------------+
| collation_connection | Latin1_swedish_ci |
| Collation_database | Latin1_swedish_ci |
| Collation_server | Latin1_swedish_ci |
+----------------------+-------------------+
3 Rows in Set (0.00 sec)

Fix It with
Set collation_database=utf8_general_ci;
Set collation_connection=utf8_general_ci;
Set collation_server=utf8_general_ci;

MySQL illegal mix of collationsy exception causes and workarounds

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.