I've created the following test table:
CREATE TABLE t (
a VARCHAR(32) BINARY,
b VARBINARY(32)
);
INSERT INTO t (a, b) VALUES ( 'test ', 'test ');
INSERT INTO t (a, b) VALUES ( 'test \0', 'test \0');
But this query indicated no difference between the two types:
SELECT a, LENGTH(a), HEX(a), b, LENGTH(b), HEX(b) FROM t;
a LENGTH(a) HEX(a) b LENGTH(b) HEX(b)
--------- --------- ------------------ --------- --------- --------------------
test 8 7465737420202020 test 8 7465737420202020
test 9 746573742020202000 test 9 746573742020202000
The ANSI SQL synonym for varbinary is binary varying.
The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings.
The BINARY and BINARY VARYING (VARBINARY) data types are collectively referred to as binary string types and the values of binary string types are referred to as binary strings. A binary string is a sequence of octets or bytes. BYTEA and RAW are synonyms for VARBINARY.
SQL VarBinaryMax. The VARBINARY data type holds variable-length binary data. Use this type when the data is expected to vary in size. The maximum size for VARBINARY is 8,000 bytes. As an aside, the word VARBINARY stands for varying binary.
As the MySQL manual page on String Data Type Syntax explains, VARBINARY
is equivalent to VARCHAR CHARACTER SET binary
, while VARCHAR BINARY
is equivalent to VARCHAR CHARACTER SET latin1 COLLATE latin1_bin
(or some other non-binary character set with the corresponding binary collation; it depends on table settings):
Specifying the CHARACTER SET binary attribute for a character string data type causes the column to be created as the corresponding binary string data type: CHAR becomes BINARY, VARCHAR becomes VARBINARY, and TEXT becomes BLOB.
The BINARY attribute is a nonstandard MySQL extension that is shorthand for specifying the binary (_bin) collation of the column character set (or of the table default character set if no column character set is specified).
So, VARBINARY
stores bytes; VARCHAR BINARY
stores character codes but sorts them like bytes (almost - see below).
What this means in practice is explained on the manual page The binary Collation Compared to _bin Collations:
VARBINARY
sorts by comparing byte by byte; VARCHAR BINARY
compares the byte groups that correspond to characters (not much of a difference for most encodings)VARCHAR BINARY
performs a character set conversion when assigning value from another column with a different encoding, or when the value is inserted/updated by a client with a different encoding; VARBINARY
just takes the value as a raw byte string.LOWER
/ UPPER
functions) has no effect on VARBINARY
(bytes have no case).VARCHAR BINARY
comparisons (that is, 'x ' = 'x'
will be true).Here are the difference I was able to find reading the documentation :
VARCHAR BINARY
VARBINARY
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With