CONVERT to CHAR cause index on VARCHAR column doesn't work - mysql

SELECT * FROM table1 WHERE a = '1';
SELECT * FROM table1 WHERE a = CONVERT(1, CHAR);
Column a is VARCHAR type, and I have already created an index on it. The first one uses index but the second one doesn't. Any Clue on this?
I suspect that maybe MySQL takes CHAR and VARCHAR as two different types, so I changed column a to CHAR, and the index doesn't work either.

Sounds like a CHARACTER SET or COLLATION problem. Please provide:
SHOW VARIABLES LIKE 'char%';
how you are connecting to the database
SHOW CREATE TABLE table1;
Probably the answer involves changing the CONVERT:
CONVERT(1, CHAR charset utf8)
What is the real query; there may be some other approach that is better for your situation. Using CONVERT is a kludge; let's go to the need for it.
To see the conversion(s), do
EXPLAIN FORMAT=JSON SELECT ...

Related

Convert VARCHAR to BINARY in MySQL

I have a table with VARCHAR(191) that actually uses a unique id which is constructed of 5 chars from these options: a-z, A-Z, 0-9 (example: 7hxYy)
I am performing a simple lookup with select * from table where token = 7hxYy yet I want the token to be case sensitive since I can actually query for 7hxyy and it will work.
How can I convert this column to binary without affecting the SQL select statements?
Try to modified the data type like:
alter table modify column token varchar(191) binary;
In MySQL, char vs char binary may affect the value case sensitive.
Or you can change your query sql like:
select * from table where BINARY `token`=‘7hxYy’;

Equal to operation not working on a field with updated collation

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"?

MySQL trying to apply UTF-8 encoding when trying to insert bytes to BLOB field

I'm using MySQL 8.0.4 (rc4) I need MySQL 8 because it's the only version of MySQL that supports CTEs.
My database is created thus:
CREATE DATABASE IF NOT EXISTS TestDB
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_general_ci;
USE TestDB;
SET sql_mode = 'STRICT_TRANS_TABLES';
CREATE TABLE IF NOT EXISTS MyTable (
(...)
Body LONGBLOB NOT NULL,
(...)
);
When I try to insert raw byte data to this description field, I receive this error:
Error 1366: Incorrect string value: '\x8B\x08\x00\x00\x00\x00...' for column 'Body' at row 1.
This is the insert statement I'm using.
REPLACE INTO MyTable
SELECT Candidate.* FROM
(SELECT :Id AS Id,
(...)
:Body AS Body,
(...)
) AS Candidate
LEFT JOIN MyTable ON Candidate.Id = MyTable.Id
WHERE (
(...)
);
How could there be an incorrect string value for BLOB? Doesn't BLOB mean I can insert quite literally anything?
What's the : stuff? Why have the nested query? May we see actual SQL? What language are you using? It sounds like the "binding" tried to apply character set rules, when it should not. May we see the code that did the substitution of the : stuff?
BLOBs have not character set. As long as you can get the bytes past the parser, there should be no problem.
However, I find this to be a better way to do it...
In the app language, generate a hex string, then use that in
INSERT INTO ... VALUES (..., UNHEX(the-hex-string), ...)

Changing Column in MySQL from int to double?

Basically, I currently have a column in a MySQL table, which is an int.
I'd like to change that to double. I've searched the web, but all it came up with was conversion upon getting the values from the column (like converting some date to Date), but that's not what I mean.
I'm guessing it's something with Alter Table, and I looked that up on the MySQL dev page, but could not find what I was looking for.
Here's the real syntax. Make sure to set the nullability appropriately too:
ALTER TABLE your_table
MODIFY COLUMN your_column DOUBLE NULL;
or
ALTER TABLE your_table
MODIFY COLUMN your_column DOUBLE NOT NULL;
You're right that you need to use ALTER TABLE. The command will look something like this:
ALTER TABLE tablename MODIFY COLUMN columnname DOUBLE;
My SQL is Non-case sensitive*
You can use :
ALTER or alter, TABLE or table, COLUMN or column, DOUBLE or double
Right now , you follow this format
ALTER TABLE table_name MODIFY COLUMN column_name DOUBLE;
I hope,
it will be helpful for you.

mysql querying a utf charset table for c returns ç

I managed to insert special characters into a table by setting the charset with
CHARSET=utf8;
Thing is, when I run the following query on the table
SELECT * FROM table WHERE word = 'francais';
it returns both "francais" and "français"!
This is not quite desirable for my situation.. I have no idea why it does this because they're just different...
Can anyone tell me how to avoid this? Would be much appreciated.
lordstyx
Try using collation, e.g.,
select *
from table
where word = 'francais' collate utf8_bin;