Why does this query produce an "Duplicate entry" error?
TRUNCATE parim_firm_tag_names;
INSERT INTO parim_firm_tag_names (firm_tag_name_value)
SELECT DISTINCT sona
FROM parim_marksona;
Error message:
SQL Error (1062): Duplicate entry '1-??????? ??????' for key
'firm_tag_name_value'
As you can see, firm_tag_name_value has an unique index, I use DISTINCT select and I'm truncating all existing data from tag_names.
What could produce this error?
This could be happening because of different collations defined on both tables parim_firm_tag_names and parim_marksona as string comparisons using distinct may results in different values on case sensitive and case insensitive collation values.
You can check collation of columns using this query:
SHOW FULL COLUMNS FROM parim_marksona;
SHOW FULL COLUMNS FROM parim_firm_tag_names;
To avoid this error, you can convert collation of column sona to the collation of column firm_tag_name_value using COLLATE, while selecting the distinct values from table parim_marksona.
Assuming collation of column firm_tag_name_value as latin1_swedish_cs:
TRUNCATE parim_firm_tag_names;
INSERT INTO parim_firm_tag_names (firm_tag_name_value)
SELECT DISTINCT sona COLLATE latin1_swedish_cs
FROM parim_marksona;
This should work without errors.
For more details refer manual Column Character Set and Collation.
Different character sets between the two tables, perhaps?
Related
I was trying to convert an existing varchar column with a unique index on it to a case sensitive column. So to do this, I updated the collation of the particular column.
Previous value: utf8mb4_unicode_ci
Current value: utf8mb4_bin
Now I have a row in my table TEST_TABLE with test_column value is abcd.
When I try to run a simple query like SELECT * FROM TEST_TABLE WHERE test_column = 'abcd'; it returns no result.
However when I try SELECT * FROM TEST_TABLE WHERE test_column LIKE 'abcd'; it returns the data correctly.
Also when I try SELECT * FROM TEST_TABLE WHERE BINARY test_column = 'abcd'; it returns the data correctly.
One more thing I tried was creating a duplicate of the table with column collation set as utf8mb4_bin while creating itself and then copy all data from original table. Then the query SELECT * FROM TEST_TABLE WHERE test_column = 'abcd'; is working alright.
So this seems to be a problem with BINARY conversion. Is there any solution to this or Am I doing something wrong ?
This seems to be an issue with MySQL. The steps I followed to resolve this is as follows:
dropped the unique index on the column
change the collation of the column
created the unique index again
Now it is working as expected. It seems MySQL didn't rebuild unique index when collation was changed. However the above steps solved my issue.
How did you change the collation? There are about 4 ways that you might think to do it. Most do something different.
Probably ALTER TABLE ... CONVERT TO COLLATION utf8mb4_bin was what you needed.
Why "bin"? You want to match case and accents? That is "abcd" != "Abcd"?
I have a table that looks like this:
CREATE TABLE t1 (
id BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
col1 VARCHAR(256),
UNIQUE INDEX t1_col1_index (col1)
)
I'm trying to modify the col1 type using the following query:
ALTER TABLE t1 MODIFY COLUMN col1 varchar(191) COLLATE utf8mb4_unicode_ci;
However, I run into this duplication error:
error: ("1062", "QMYSQL3: Unable to execute statement", "Duplicate entry '+123456789' for key 't1_col1_index'")
I initially thought it could be because two or more rows might 'contain' similar value for col1 and on changing varchar length the data gets truncated but then I found out that data truncation wouldn't even allow the query to go through. Any pointers on what could be causing this?
EDIT (Resolved): Truncation does happen when ##sql_mode is not set with STRICT_TRANS_TABLES. This was causing the error.
You are reducing the length of a varchar column that is controlled by a UNIQUE constraint.
This is risky business. Oversize data will be silently trimed (unless you have the ##sql_mode set to STRICT_TRANS_TABLES in which case an error will be raised). This probably generates duplicates, which cause the error to be raised by your UNIQUE constraint.
You can check the max length of the values in your column with :
SELECT MAX(CHAR_LENGTH(col1)) FROM t1:
I am not sure if this is work.
Try to check the table t1.
select count(1) from t1 where col1 = 123456789
Now if count is greater than one then try to remove the other one and leave only one record.
Then try to run your statement again.
Reminder:
Do back up first before removing.
i have to compare two strings in a query like following:
SELECT *
FROM MY_TABLE
WHERE column LIKE '%keyword%';
But i want to compare unaccented values of both column and keyword. Is there an unaccent() function or other way to achieve this in MySQL?
AFAIK, No there is no unaccent() function present in MySQL. To ignore the accent you will have to set the proper collation for the column you are trying to compare. Example: How to remove accents in MySQL?
I'm attempting to insert a datetime('2013-08-30 19:05:00') value into a SQL server database table column(smalldatetime) and the value stays "NULL" after the insert.
I'm doing this to 6 other columns that are the exact same type. What is this only occuring on one column? I've triple checked that the names of the columns are correct. Any ideas?
Assuming the situation is as you describe
CREATE TABLE T
(
S SMALLDATETIME NULL
)
INSERT INTO T
VALUES('2013-08-30 19:05:00')
SELECT *
FROM T /*Returns NULL*/
There are only two ways I can think of that this can happen.
1) That is an ambiguous datetime format. Under the wrong session options this won't cast correctly and if you have some additional options OFF it will return NULL rather than raise an error (e.g.)
SET LANGUAGE Italian;
SET ansi_warnings OFF;
SET arithabort OFF;
INSERT INTO T
VALUES('2013-08-30 19:05:00')
SELECT *
FROM T /*NULL inserted*/
2) You may have missed the column out in an INSTEAD OF trigger, or have an AFTER trigger that actually sets the value back to NULL.
I have created a MYSQL stored procedure that searches all columns of a table (parameter 1) to find if any of them contains a string (parameter 2). The problem is that when I compare a datetime column with a string that contains greek characters I get the following error:
SELECT * FROM my_table WHERE my_datetime LIKE '%ΕΛΛΗΝΙΚΑ%';
Illegal mix of collations for operation 'like'
The execution of the query is successful when I use latin characters. I know how to avoid checking columns based on their datatype, however I really want to be able to search for datetime strings as well. I use MySQL 5.5.24. The collation of the database is utf8_general_ci and the collation of the server is latin1_swedish_ci. I have tried the command SET NAMES utf8 before the query, with no luck though. Any ideas? Thanks.
I had a problem with similar query:
SELECT * FROM some_table WHERE some_date LIKE '%2012-01%';
The result was an "Illegal mix of collations" error.
Using simply DATE_FORMAT function helped:
SELECT * FROM some_table WHERE DATE_FORMAT(some_date, '%Y-%m-%d') LIKE '%2012-01%';