Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why varbinary instead of varchar [duplicate]

Please take a look at this table :

http://www.mediawiki.org/wiki/Manual:Logging_table

As you can see wikipedia use varbinary instead of varchar :

| log_type      | **varbinary**(32)       | NO   | MUL |                | | log_action    | **varbinary**(32)       | NO   |     |                | | log_timestamp | **binary**(14)          | NO   | MUL | 19700101000000 | | log_user      | int(10) unsigned        | NO   | MUL | 0              |   | log_user_text | **varbinary**(255)      |      |     |                | 

All of these information are text , so why they save them as binary ?

They do this for all tables .

like image 806
user1411084 Avatar asked Nov 15 '12 08:11

user1411084


People also ask

Why do we use VARBINARY?

It stores the maximum size of up to 2GB. A variable-length datatype, varbinary(max) can be used for media that require large capacity.

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. M represents the maximum column length in bytes. It contains no character set, and comparison and sorting are based on the numeric value of the bytes.

What is the use of VARBINARY in SQL?

Binary, Varbinary & Varbinary(max) are the binary string data types in SQL Server. These data types are used to store raw binary data up to a length of (32K – 1) bytes. The contents of image files (BMP, TIFF, GIF, or JPEG format files), word files, text files, etc. are examples of binary data.

What is the difference between binary and VARBINARY?

binary [ ( n ) ] Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. The storage size is n bytes. varbinary [ ( n | max) ] Variable-length binary data. n can be a value from 1 through 8,000.


2 Answers

Mediawiki changed from varchar to varbinary in early 2011:

War on varchar. Changed all occurrences of varchar(N) and varchar(N) binary to varbinary(N). varchars cause problems ("Invalid mix of collations" errors) on MySQL databases with certain configs, most notably the default MySQL config.

like image 147
Sjoerd Avatar answered Sep 20 '22 12:09

Sjoerd


In MSSQL:

I think the big difference is only between nvarchar and varbinary.

Because nvarchar stores 2 bytes for each character instead of 1 byte.

varchar does the same as varbinary: from MSDN:

The storage size is the actual length of the data entered + 2 bytes" for both.

The difference here is by varbinary The data that is entered can be 0 bytes in length.

Here is a small example:

CREATE TABLE Test (textData varchar(255), binaryData varbinary(255))  INSERT INTO Test  VALUES('This is an example.', CONVERT(varbinary(255),'This is an example.',0)) INSERT INTO Test  VALUES('ÜŰÚÁÉÍä', CONVERT(varbinary(255),'ÜŰÚÁÉÍä',0)) 

What you can use here is the DATALENGTH function:

SELECT datalength(TextData), datalength(binaryData) FROM test 

The result is 19 - 19 and 7 - 7

So in size they are the same, BUT there is an other difference. If you check the column specifications, you can see, that the varbinary (of course) has no collation and character set, so it could help use values from different type of encoding and character set easily.

SELECT    * FROM      INFORMATION_SCHEMA.COLUMNS  WHERE      TABLE_NAME = 'Test'  ORDER BY    ORDINAL_POSITION ASC;  
like image 30
András Ottó Avatar answered Sep 17 '22 12:09

András Ottó