Oracle中的Number类型

number数据类型

number类型的语法很简单:number(p,s):
p:精度位,precision,是总有效数据位数,取值范围是38,默认是38,能够用字符*表示38。
s:小数位,scale,是小数点右边的位数,取值范围是-84~127,默认值取决于p,若是没有指定p,那么s是最大范围,若是指定了p,那么s=0。
p:is the precision,or the total number of digits. Oracle guarantees the portability of numbers with precision ranging from 1 to 38.
s:is the scale, or the number of digits to the right of the decimal point. The scale can range from -84 to 127.

number类型的p和s,与其底层存储彻底没有关系,根本不会影响数据在磁盘上如何存储,它只会影响容许哪些值以及数值如何舍入,你能够认为其是对数据的“编辑”。简单的说,精度位p表示数值最多能有多少个有效数字,而小数位s表示最多能有多少位小数。换句话说,p表示一共有多少位有效数字(即小数点左边最多有p-s位有效数字),s表示小数点右边有s位有效数字。如number(5,2)类型的数据,就表示小数点左边最多有3位有效数字,右边最多有2位有效数字,加起来就是最多有5位有效数字,超过这个范围的数字就不能正确的存储下来,注意这里说的是不能正确存储,但并非不能存储。

最高整数位数=p-s
s正数,小数点右边指定位置开始四舍五入
s负数,小数点左边指定位置开始四舍五入
s是0或者未指定,四舍五入到最近整数
当p小于s时候,表示数字是绝对值小于1的数字,且从小数点右边开始的前s-p位必须是0,保留s位小数。

p>0,对s分2种状况:
1. s>0
精确到小数点右边s位,并四舍五入。而后检验有效数位是否<=p;若是s>p,小数点右边至少有s-p个0填充。
2. s<0
精确到小数点左边s位,并四舍五入。而后检验有效数位是否<=p+|s|

具体数据可参考下表

Valuegit

Datatypethis

Stored Valuespa

123.2564orm

NUMBERci

123.2564it

1234.9876io

NUMBER(6,2)table

1234.99select

12345.12345数据类型

NUMBER(6,2)

Error

1234.9876

NUMBER(6)

1235

12345.345

NUMBER(5,-2)

12300

1234567

NUMBER(5,-2)

1234600

12345678

NUMBER(5,-2)

Error

123456789

NUMBER(5,-4)

123460000

1234567890

NUMBER(5,-4)

Error

12345.58

NUMBER(*, 1)

12345.6

0.1

NUMBER(4,5)

Error

0.01234567

NUMBER(4,5)

0.01235

0.09999

NUMBER(4,5)

0.09999

0.099996

NUMBER(4,5)

Error


里面发生错误的行有的是由于源数据超过了能够表示的范围,有的是由于进行小数四舍五入后超过了能够表示的范围。 如下是一些例子1. s>0 精确到小数点右边s位,并四舍五入。而后检验有效数位是否<=p; ZWF.YUDONG>create table t_n(id number(5,2)); Table created. ZWF.YUDONG>insert into t_n values(123.45); 1 row created. ZWF.YUDONG>insert into t_n values(123.455); 1 row created. ZWF.YUDONG>select * from t_n;         ID ----------     123.45     123.46 2 rows selected. ZWF.YUDONG>insert into t_n values(1.234); 1 row created. ZWF.YUDONG>select * from t_n;         ID ----------     123.45     123.46       1.23 3 rows selected. ZWF.YUDONG>insert into t_n values(.001); 1 row created. ZWF.YUDONG>select * from t_n;         ID ----------     123.45     123.46       1.23          0 4 rows selected. ZWF.YUDONG>insert into t_n values(1234.56); insert into t_n values(1234.56)                        * ERROR at line 1: ORA-01438: value larger than specified precision allowed for this column 若是s>p,小数点右边至少有s-p个0填充。 ZWF.YUDONG>create table t_n(id number(4,5)); Table created. ZWF.YUDONG>insert into t_n values(1); insert into t_n values(1)                        * ERROR at line 1: ORA-01438: value larger than specified precision allowed for this column ZWF.YUDONG>insert into t_n values(.1); insert into t_n values(.1)                        * ERROR at line 1: ORA-01438: value larger than specified precision allowed for this column ZWF.YUDONG>insert into t_n values(.01); 1 row created. ZWF.YUDONG>commit; Commit complete. ZWF.YUDONG>select * from t_n;         ID ----------        .01 1 row selected. ZWF.YUDONG>insert into t_n values(.001); 1 row created. ZWF.YUDONG>insert into t_n values(.0001); 1 row created. ZWF.YUDONG>insert into t_n values(.00001); 1 row created. ZWF.YUDONG>insert into t_n values(.000001);   --超过刻度存储0 1 row created. ZWF.YUDONG>select * from t_n;         ID ----------        .01       .001      .0001     .00001          0 10 rows selected. ZWF.YUDONG>col dp for a50 ZWF.YUDONG>select id,dump(id) dp,length(id),vsize(id) from t_n;  --vsize和dump的是字节数,length是数值实际位数(含小数点)         ID DP                                                 LENGTH(ID)  VSIZE(ID) ---------- -------------------------------------------------- ---------- ----------        .01 Typ=2 Len=2: 192,2                                          3          2       .001 Typ=2 Len=2: 191,11                                         4          2      .0001 Typ=2 Len=2: 191,2                                          5          2     .00001 Typ=2 Len=2: 190,11                                         6          2          0 Typ=2 Len=1: 128                                            1          1 5 rows selected. 2. s<0 精确到小数点左边s位,并四舍五入。而后检验有效数位是否<=p+|s| ZWF.YUDONG>create table t_n(id number(5,-2)); Table created. ZWF.YUDONG>insert into t_n values(12345); 1 row created. ZWF.YUDONG>select * from t_n;         ID ----------      12300 1 row selected. ZWF.YUDONG>insert into t_n values(123456); 1 row created. ZWF.YUDONG>insert into t_n values(1234567); 1 row created. ZWF.YUDONG>select * from t_n;         ID ----------      12300     123500    1234600 3 rows selected. ZWF.YUDONG>insert into t_n values(12345678); insert into t_n values(12345678)                        * ERROR at line 1: ORA-01438: value larger than specified precision allowed for this column