[英]Why no “division by zero” error in PostgreSQL for such case?
Noticed the following behavior of PostgreSQL 9.5: 注意PostgreSQL 9.5的以下行为:
Division of something by zero - works as expected: 将某物除以零-按预期工作:
# select 1 / 0;
ERROR: division by zero
Dividing NULL
by zero: 将
NULL
除以零:
# select NULL / 0;
?column?
----------
(1 row)
I browsed the documentation, but didn't find any reference to the reasons for such a design decision. 我浏览了文档,但没有找到任何有关此类设计决定的原因的参考。 The result of the dividend can depend on data - can be something or
NULL
, so the result - NULL
or error - will depend on data. 红利的结果可能取决于数据-可以是某物或
NULL
,因此结果NULL
或错误-将取决于数据。 Wouldn't that be safer to throw error in all cases when divisor is known to be zero? 在已知除数为零的情况下,在所有情况下抛出错误是否更安全?
This is more about SQL Standard than anything: 这比SQL Standard更重要:
An arithmetic operation involving a NULL returns NULL.
涉及NULL的算术运算将返回NULL。 For example, NULL minus NULL yields NULL, not zero.
例如,NULL减去NULL会产生NULL,而不是零。
The decision in this case is just to follow SQL ANSI. 在这种情况下,决定只是遵循SQL ANSI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.