In MySQL, can binary strings (BINARY, VARBINARY and BLOB) have a CHARSET so bytes used per character are different for each CHARSET as in non-binary strings?
1 Answer
No. That'd defeat the purpose of the binary data types. They're meant for data that cannot ever be charset-translated. E.g. you save .jpg images in your DB. If the raw binary data was charset-translated, the jpg would be totally corrupted.
If you want charsets, then use varchar, text, etc...
9 Comments
vicaba
Then, if I store the string "añäà" (or with chinese symbols) in a binary data type, how is it treated? I mean, what is the relation between the character I type and the binary representation? Because ALL existing characters can't be encoded in a single byte. Thanks.
Marc B
so? that's why it's binary data. you're telling mysql that whatever bytes go into the field are just that - a sequence of bytes. if your charset actually is multi-byte, that's therefore irrelevant to mysql. two bytes representing one UTF-16 char are just two bytes.
vicaba
Yep, I understand that, but what happens if I inset the string "abc" in a binary(3) field from my command line and my mysql connection has a default charset that takes 3 bytes per character? Will the data be truncated?
Marc B
nothing. it's binary data. mysql won't do ANY translations on it. you put in 3bytes, and 3 bytes will come out. 3 bytes will also get sent "over the wire" back to the client. how those 3 bytes are treated on the client is not mysql's concern.
vicaba
I'm sorry but there is something I don't catch... Imagine that I have a keyboard with 300 keys, if I write the key number 300 into the string, how is this handled? Because a byte ranges from 0 to 255...
|