0% found this document useful (0 votes)
13 views2 pages

mysql - Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '=' - Stack Overflow

The document discusses an error in MySQL related to an illegal mix of collations (utf8_unicode_ci and utf8_general_ci) during an operation. It provides a stored procedure example that triggers this error and suggests various solutions, including altering the collation of input variables or the database fields. The document emphasizes the importance of consistent collation across tables to avoid such issues.

Uploaded by

Alex Nevsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

mysql - Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '=' - Stack Overflow

The document discusses an error in MySQL related to an illegal mix of collations (utf8_unicode_ci and utf8_general_ci) during an operation. It provides a stored procedure example that triggers this error and suggests various solutions, including altering the collation of input variables or the database fields. The document emphasizes the importance of consistent collation across tables to avoid such issues.

Uploaded by

Alex Nevsky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for

operation '='
Asked 12 years, 8 months ago Modified 2 months ago Viewed 383k times

219 Error message on MySql:

Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and


(utf8_general_ci,IMPLICIT) for operation '='

I have gone through several other posts and was not able to solve this problem. The part affected is something similar to this:

CREATE TABLE users (


userID INT UNSIGNED NOT NULL AUTO_INCREMENT,
firstName VARCHAR(24) NOT NULL,
lastName VARCHAR(24) NOT NULL,
username VARCHAR(24) NOT NULL,
password VARCHAR(40) NOT NULL,
PRIMARY KEY (userid)
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

CREATE TABLE products (


productID INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(104) NOT NULL,
picturePath VARCHAR(104) NULL,
pictureThumb VARCHAR(104) NULL,
creationDate DATE NOT NULL,
closeDate DATE NULL,
deleteDate DATE NULL,
varPath VARCHAR(104) NULL,
isPublic TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
PRIMARY KEY (productID)
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

CREATE TABLE productUsers (


productID INT UNSIGNED NOT NULL,
userID INT UNSIGNED NOT NULL,
permission VARCHAR(16) NOT NULL,
PRIMARY KEY (productID,userID),
FOREIGN KEY (productID) REFERENCES products (productID) ON DELETE RESTRICT
ON UPDATE NO ACTION,
FOREIGN KEY (userID) REFERENCES users (userID) ON DELETE RESTRICT ON UPDATE
NO ACTION
) ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_unicode_ci;

The stored procedure I'm using is this:

CREATE PROCEDURE updateProductUsers (IN rUsername VARCHAR(24),IN rProductID INT


UNSIGNED,IN rPerm VARCHAR(16))
BEGIN
UPDATE productUsers
INNER JOIN users
ON productUsers.userID = users.userID
SET productUsers.permission = rPerm
WHERE users.username = rUsername
AND productUsers.productID = rProductID;
END

I was testing with php, but the same error is given with SQLyog. I have also tested recreating the entire DB but to no good.

Any help will be much appreciated.

mysql stored-procedures

Share Improve this question Follow edited Feb 21, 2019 at 13:16 asked Aug 2, 2012 at 2:05
agold Manatax
6,286 9 40 54 4,233 6 31 41

1 I have faced the same issue when try to join records of 3 tables. When I checked my database, I found that, one of the table has utf8_general but other are have
utf8_unicode_ci. So I changed utf8_general to utf8_unicode_ci and my problem solved. – rahim.nagori Oct 28, 2020 at 10:28

12 Answers Sorted by:


292 The default collation for stored procedure parameters is utf8_general_ci and you can't mix collations, so you have four options:

Option 1: add COLLATE to your input variable:

SET @rUsername = ‘aname’ COLLATE utf8_unicode_ci; -- COLLATE added


CALL updateProductUsers(@rUsername, @rProductID, @rPerm);

Option 2: add COLLATE to the WHERE clause:

CREATE PROCEDURE updateProductUsers(


IN rUsername VARCHAR(24),
IN rProductID INT UNSIGNED,
IN rPerm VARCHAR(16))
BEGIN
UPDATE productUsers
INNER JOIN users
ON productUsers.userID = users.userID
SET productUsers.permission = rPerm
WHERE users.username = rUsername COLLATE utf8_unicode_ci -- COLLATE
added
AND productUsers.productID = rProductID;
END

Option 3: add it to the IN parameter definition (pre-MySQL 5.7):

CREATE PROCEDURE updateProductUsers(


IN rUsername VARCHAR(24) COLLATE utf8_unicode_ci, -- COLLATE added
IN rProductID INT UNSIGNED,
IN rPerm VARCHAR(16))
BEGIN
UPDATE productUsers
INNER JOIN users
ON productUsers.userID = users.userID
SET productUsers.permission = rPerm
WHERE users.username = rUsername
AND productUsers.productID = rProductID;
END

Option 4: alter the field itself:

ALTER TABLE users CHARACTER SET utf8 COLLATE utf8_general_ci;

Unless you need to sort data in Unicode order, I would suggest altering all your tables to use utf8_general_ci collation, as it requires no code
changes, and will speed sorts up slightly.

UPDATE: utf8mb4/utf8mb4_unicode_ci is now the preferred character set/collation method. utf8_general_ci is advised against, as the performance
improvement is negligible. See https://stackoverflow.com/a/766996/1432614

Share Improve this answer Follow edited Jun 15, 2021 at 14:41 answered Aug 2, 2012 at 2:25
Ross Smith II
12.2k 1 40 45

2 It is also possible to add COLLATE utf8_unicode_ci to string constants: SET @EMAIL = '[email protected]' COLLATE utf8_unicode_ci; . It is especially useful if you
are running a script from a console, where the console default encoding applies to your string constants' collation. – gaborsch May 5, 2016 at 10:35

Or drop database and create new with utf8_general_ci; collation. – Oleksii Kyslytsyn Apr 1, 2017 at 22:05

4 For future reference, don't change all your tables to utf8_general_ci unless you understand the differences between the two collations. – Manatax Jul 31, 2017 at 19:10

2 @GaborSch Adding collate to string variables was the solution for me, I wrote a detailed answer about it before I noticed your comment. – nkatsar Oct 10, 2017 at 16:49

im getting the same error, except (utf8mb4_unicode_ci, IMPLICIT) instead of (utf8_unicode_ci, IMPLICIT) . i am scraping data off the web using python, then
creating an CSV file with the scraped data, which i then process with a PHP file on my server that uploads the data to my database. all my MySQL tables/columns are
collated as utf8mb4_unicode_ci . might the issue be arising because i encode the data as utf8 in python/csv? – oldboy Nov 7, 2018 at 8:29

It seems option 4 won't work, you have to specify collation for each column, not only for the table (at least when the table already exists). – Skippy le Grand Gourou Aug
23, 2019 at 8:17

unfortunately, collate can't be added to the IN parameters in mysql 5.7 – veritas Jun 15, 2021 at 8:48

I got this 'Error Code: 1253. COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'utf8mb4' ' – Saige Zhang Dec 4, 2023 at 22:52

You might also like