Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "VARCHAR BINARY" and "VARBINARY" in MySQL?

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  
like image 299
Clarence Avatar asked Mar 27 '15 02:03

Clarence


People also ask

Is VARBINARY same as binary?

The ANSI SQL synonym for varbinary is binary varying.

What is the difference between varchar and VARBINARY?

The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings.

What is binary and VARBINARY in SQL?

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.

What does VARBINARY mean in SQL?

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.


2 Answers

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.
  • Case conversion in SQL (ie. the LOWER / UPPER functions) has no effect on VARBINARY (bytes have no case).
  • Trailing spaces will be usually ignored in VARCHAR BINARY comparisons (that is, 'x ' = 'x' will be true).
like image 149
Tgr Avatar answered Nov 15 '22 15:11

Tgr


Here are the difference I was able to find reading the documentation :

VARCHAR BINARY

  • The BINARY attribute cause the binary collation for the column character set to be used, and the column itself contains nonbinary character strings rather than binary byte strings.
  • When BINARY values are stored, they are right-padded with the pad value to the specified length.
  • You should consider the preceding padding and stripping characteristics carefully if you plan to use the BINARY data type for storing binary data and you require that the value retrieved be exactly the same as the value stored.

VARBINARY

  • If strict SQL mode is not enabled and you try to assign a value that exceeds the column's maximum length, the value is truncated to fit and a warning is generated.
  • There is no padding on insert, and no bytes are stripped on select. All bytes are significant in comparisons.
  • Utilisation is preferable when the value retrieved must be the same as the value specified for storage with no padding.
like image 21
Jean-François Savard Avatar answered Nov 15 '22 15:11

Jean-François Savard