MySQL "binary" vs "char character set binary"
Asked Answered
H

1

15

What's the difference between binary(10) vs char(10)character set binary?

And varbinary(10) vs varchar(10)character set binary?

Are they synonymous in all MySQL engines?

Is there any gotcha to watch out for?

Horan answered 11/3, 2013 at 9:4 Comment(0)
V
16

There isn't a difference.

However, there is a caveat if you're storing a string.

If you only want to store a byte array or other binary data such as a stream or file then use the binary type as that is what they are meant for.

Quote from the MySQL manual:

The use of CHARACTER SET binary in the definition of a CHAR, VARCHAR, or TEXT column causes the column to be treated as a binary data type. For example, the following pairs of definitions are equivalent:

CHAR(10) CHARACTER SET binary
BINARY(10)

VARCHAR(10) CHARACTER SET binary
VARBINARY(10)

TEXT CHARACTER SET binary
BLOB

So, technically there is no difference.

However, when storing a string it must be converted from a string to byte values using a character set. The decision is to either do this yourself before the MySQL server or you leave it up to MySQL do to do for you. MySQL will perform with by casting a string to BINARY using the BIN character sets.

If you want to store the encoding in another format, lets say you have a business requirement that says you must use 4 bytes per character (MySQL doesn't do this by default) you could then use the CHARACTER SET BINARY to a textual column and perform the character set encoding yourself.

It is also worth reading The BINARY and VARBINARY Types from the MySQL manual as this details important information such as padding.

Summary: There is no technical difference as one is a synonym to the other. In my opinion it makes logical sense to store binary strings in data types that would normally hold a string using the CHARACTER SET BINARY and to store byte arrays / streams etc in BINARY fields that cannot be represented by transforming the data though a character set.

Vaudevillian answered 11/3, 2013 at 10:0 Comment(4)
I'm storing byte arrays. If both options are synonymous, why do you say "use the binary type as that is what its designed for"? Isn't char character set binary designed for byte arrays too?Horan
@Horan that really was bad wording on my part (I will make this clearer in my answer), charset set binary is a synonym for binary so technically there is no difference. IMO it is more understandable to store a binary stream (that cannot be represented by a character set and collation) makes sense in BINARY, if you're doing your own character encoding it makes sense to store that in a field with CHARACTER SET BINARYVaudevillian
@Ic, btw are you aware if this behavior is official, or are engines still allowed to do their own implementation?Horan
@Horan that's a great question, and to be honest I don't know. However I can point you in the right direction. MySQL creates the .frm file on a table create, this .frm is then passed to the storage engine thought the API - if there is no difference in the .frm file (when creating two tables with different field values) then the storage engine won't know any difference and will not be able to treat them differently. This 'could' change in future versions and shouldn't be relied upon.Vaudevillian

© 2022 - 2024 — McMap. All rights reserved.