Checksum() 为负
Checksum() for minus
为什么 checksum() 函数 return 0 为负数?
select checksum('-') /* 0 */
select checksum('---') /* 0 */
select checksum('-+-') /* 67 */
select checksum('+') /* 67 */
来自维基百科,哈希函数 (http://en.wikipedia.org/wiki/Hash_function):
A hash function is any function that can be used to map digital data of arbitrary size to digital data of fixed size. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes.
在这个定义下,你可以有一个函数 returns 任何输入都为零,但这将是最糟糕的散列。所以,如果你问他们为什么不选择更好的算法,我不确定你问对了人。你可能想问微软。我们可以从 MSDN 上的评论推断,至少在 Microsoft 中有人知道 CHECKSUM 不是理想的哈希函数。在这里,如果你需要一个特别好的哈希,他们推荐 HASHBYTES。
来自 MSDN,CHECKSUM(Transact-SQL):(https://msdn.microsoft.com/en-us/library/ms189788.aspx)
CHECKSUM satisfies the properties of a hash function: CHECKSUM applied over any two lists of expressions returns the same value if the corresponding elements of the two lists have the same type and are equal when compared using the equals (=) operator. For this definition, null values of a specified type are considered to compare as equal. If one of the values in the expression list changes, the CHECKSUM of the list also generally changes. However, there is a small chance that the CHECKSUM will not change. For this reason, we do not recommend using CHECKSUM to detect whether values have changed, unless your application can tolerate occasionally missing a change. Consider using HashBytes instead. When an MD5 hash algorithm is specified, the probability of HashBytes returning the same result for two different inputs is much lower than that of CHECKSUM.
为什么 checksum() 函数 return 0 为负数?
select checksum('-') /* 0 */
select checksum('---') /* 0 */
select checksum('-+-') /* 67 */
select checksum('+') /* 67 */
来自维基百科,哈希函数 (http://en.wikipedia.org/wiki/Hash_function):
A hash function is any function that can be used to map digital data of arbitrary size to digital data of fixed size. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes.
在这个定义下,你可以有一个函数 returns 任何输入都为零,但这将是最糟糕的散列。所以,如果你问他们为什么不选择更好的算法,我不确定你问对了人。你可能想问微软。我们可以从 MSDN 上的评论推断,至少在 Microsoft 中有人知道 CHECKSUM 不是理想的哈希函数。在这里,如果你需要一个特别好的哈希,他们推荐 HASHBYTES。
来自 MSDN,CHECKSUM(Transact-SQL):(https://msdn.microsoft.com/en-us/library/ms189788.aspx)
CHECKSUM satisfies the properties of a hash function: CHECKSUM applied over any two lists of expressions returns the same value if the corresponding elements of the two lists have the same type and are equal when compared using the equals (=) operator. For this definition, null values of a specified type are considered to compare as equal. If one of the values in the expression list changes, the CHECKSUM of the list also generally changes. However, there is a small chance that the CHECKSUM will not change. For this reason, we do not recommend using CHECKSUM to detect whether values have changed, unless your application can tolerate occasionally missing a change. Consider using HashBytes instead. When an MD5 hash algorithm is specified, the probability of HashBytes returning the same result for two different inputs is much lower than that of CHECKSUM.