为什么 SQL Server 不支持无符号数据类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4451967/
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-09-01 08:35:04  来源:igfitidea点击:

Why doesn't SQL Server support unsigned datatype?

sqlsql-serversqldatatypesunsigned-integer

提问by Romhein

I am specifically thinking about unsigned int.

我特别在考虑 unsigned int

Here is a practical example: what do you do when your identity column maxes out? It's possible to either go BigInt(8 bytes storage instead of 4) or to refactor the application to support negative integers, and even to create your own rules as indicated in this answer; neither of those options are optimal.

这是一个实际示例:当您的标识列最大化时,您会怎么做?可以使用BigInt(8 字节存储而不是 4 字节存储)或重构应用程序以支持负整数,甚至可以创建自己的规则,如本答案所示;这些选项都不是最佳选择。

UIntwould be an ideal solution, but SQL Server does not offer it (where MySQL does).

UInt将是一个理想的解决方案,但 SQL Server 不提供它(MySQL 提供)。

I understand that unsigned datatypes are not part of the SQL standard (SQL-2003) but still seems like a waste to me.

我知道无符号数据类型不是 SQL 标准 (SQL-2003) 的一部分,但对我来说仍然是一种浪费。

What is the reason of not including these (in SQL Server or in the standard)?

不包括这些(在 SQL Server 或标准中)的原因是什么?

采纳答案by Jeff Hornby

If I had to guess, I would say that they are trying to avoid a proliferation of types. Generally speaking there isn't anything that an unsigned integer can do that a signed integer can't do. As for the case when you need a number between 2147483648 and 4294967296 you probably should go to an 8 byte integer since the number will also eventually exceed 4294967296.

如果我不得不猜测,我会说他们正在努力避免类型的扩散。一般来说,没有什么是无符号整数可以做而有符号整数不能做的。至于您需要 2147483648 和 4294967296 之间的数字的情况,您可能应该使用 8 字节整数,因为该数字最终也会超过 4294967296。

回答by CFreitas

For that purpose you could use -2,147,483,648 as the seed value.

为此,您可以使用 -2,147,483,648 作为种子值。

Identity(-2147483648, 1)

回答by Anthony K

I found a similar question on Microsoft Office Dev Center.

在 Microsoft Office Dev Center 上发现了一个类似的问题

The reply from Jim Hogg (Program Manager) has some pro's and con's for adding unsigned int's. The major con is the rules to implement implicit type conversions become a nightmare to get right.

Jim Hogg(程序经理)的回复对添加无符号整数有一些优点和缺点。主要的缺点是实现隐式类型转换的规则变成了正确的噩梦。

The request was closed as "Won't Fix".

该请求被关闭为“不会修复”。

回答by Federico Razzoli

They don't support the SIGNED and UNSIGNED keyword because they're not standard. In SQL standard, all numeric types are signed.

它们不支持 SIGNED 和 UNSIGNED 关键字,因为它们不是标准的。在 SQL 标准中,所有数字类型都是有符号的。

UNSIGNED (and SIGNED, which is the default) are MySQL extensions that can be useful to store higher unsigned numbers in the same amount of bytes, and disallow negative numbers.

UNSIGNED(和 SIGNED,这是默认值)是 MySQL 扩展,可用于在相同数量的字节中存储更高的无符号数,并禁止负数。