numeric(m,n)所能插入数值的范围

来源:互联网 发布:天谕 萝莉脸数据 编辑:程序博客网 时间:2024/05/21 11:25

最近看书的时候看到这么一句话:

对于声明了精度的数值,如果insert插入的数值大于声明的精度范围,则会报错。

那么这里的精度范围是什么?精度代表的数值就是位数吗?即3是不是意味着最大值就是999?

测试了一堆数据发现如下:

如果精度和标度都没有声明,那么插入的数值将保持原样,没有范围约束。(当然不可以超出系统内可以实现的精度和标度)。

 

testdb=# create table numeric(n1numeric(3,0),n2 numeric(3,0),n3 numeric(3,2),n4 numeric);

CREATE TABLE

testdb=# insert into numericvalues(3.1,3.5,3.12,3.123);

INSERT 0 1

testdb=# select * from numeric;

 n1 |n2 |  n3 |  n4  

----+----+------+-------

  3|  4 | 3.12 | 3.123

(1 row)

可以发现,超出标度的数值确实是按照四舍五入的方式插入的。

 

testdb=# insert into numeric values(999.4999,5,9.991,13.123);

INSERT 0 1

testdb=# insert into numericvalues(999.5,5,9.994,13.123);

ERROR: numeric field overflow

DETAIL: A field with precision 3, scale 0 must round to an absolute value lessthan 10^3.

STATEMENT: insert into numeric values(999.5,5,9.994,13.123);

ERROR: numeric field overflow

DETAIL: A field with precision 3, scale 0 must round to an absolute value lessthan 10^3.

这两组数据就意味着numeric(3,0)所能插入的最大值为999.4999(9循环),即总是小于999.5。

 

这个时候我想,那numeric(3,2)是不是就意味着必须小于999.49呢?继续插入数值,找到了临界值:

testdb=# insert into numericvalues(999.4999,5,9.994999999999999999,13.123);

INSERT 0 1

testdb=# insert into numericvalues(1,2,9.995,22.123);

ERROR: numeric field overflow

DETAIL: A field with precision 3, scale 2 must round to an absolute value lessthan 10^1.

STATEMENT: insert into numeric values(1,2,9.995,22.123);

ERROR: numeric field overflow

DETAIL: A field with precision 3, scale 2 must round to an absolute value lessthan 10^1.

这两组数据证明了numeric(3,2)最大值是9.99499999(9循环),即总是小于9.995。这也就是说明所谓的精度范围是要和标度结合的一个范围,精度值表示的位数是要把标度(小数点后几位)算进去的。

而没有精度和标度的数值范围(系统所能承受的精度范围)我并没有测试出来,这个意义不大。

testdb=# insert into numeric

values(999.4999999,999.49999999999,9.99499999999999999,99999999999999.99999999);

INSERT 0 1

testdb=# select * from numeric;

 n1  |n2  | n3  |           n4           

-----+-----+------+-------------------------

   3|   4 | 3.12 |                   3.123

   5|   4 | 3.12 |                   3.123

   5|   4 | 5.12 |                   3.123

  13|   4 | 5.12 |                   3.123

  13|   4 | 8.12 |                  13.123

 999|   4 | 9.12 |                  13.123

 999|   5 | 9.99 |                  13.123

 999|   5 | 9.99 |                  13.123

 999|   5 | 9.99 |                  13.123

 999|   5 | 9.99 |                  13.123

 999| 999 | 9.99 | 99999999999999.99999999

(11 rows)


0 0
原创粉丝点击