MySQL 为什么 varbinary 而不是 varchar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13393718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:27:48  来源:igfitidea点击:

why varbinary instead of varchar

mysqlsqlsql-serverdatabasedatabase-design

提问by user1411084

Possible Duplicate:
What is the advantage of using varbinary over varchar here?

可能的重复:
在这里使用 varbinary 比使用 varchar 有什么优势?

Please take a look at this table :

请看一下这张表:

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

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

As you can see wikipedia use varbinary instead of varchar :

如您所见,维基百科使用 varbinary 而不是 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 .

他们对所有表都这样做。

采纳答案by Sjoerd

Mediawiki changed from varchar to varbinary in early 2011:

Mediawiki 在2011 年初从 varchar 更改为 varbinary :

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.

对 varchar 的War。将所有出现的 varchar(N) 和 varchar(N) 二进制更改为 varbinary(N)。varchars 在具有某些配置的 MySQL 数据库上导致问题(“无效的排序规则混合”错误),最显着的是默认的 MySQL 配置。

回答by András Ottó

In MSSQL:

在 MSSQL 中:

I think the big difference is only between nvarcharand varbinary.

我认为最大的区别仅在于nvarchar和之间varbinary

Because nvarcharstores 2 bytes for each character instead of 1 byte.

因为nvarchar每个字符存储 2 个字节而不是 1 个字节。

varchardoes the same as varbinary: from MSDN:

varchar与以下内容相同varbinary:来自 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.

存储大小是输入数据的实际长度 + 2 个字节”。

这里的区别是通过 varbinary输入的数据长度可以是 0 个字节。

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 DATALENGTHfunction:

您可以在这里使用的是DATALENGTH函数:

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

The result is 19 - 19 and 7 - 7

结果是 19 - 19 和 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.

所以在大小上它们是相同的,但还有另一个区别。如果您检查列规范,您会发现 varbinary(当然)没有排序规则和字符集,因此它可以帮助轻松使用来自不同类型编码和字符集的值。

SELECT 
  *
FROM   
  INFORMATION_SCHEMA.COLUMNS 
WHERE   
  TABLE_NAME = 'Test' 
ORDER BY 
  ORDINAL_POSITION ASC;