您的位置:首页 > 数据库 > Oracle

Oracle的number的整数位最大长度是38(小数位最大长度是127位),varchar2的最大长度是4000...

2016-04-24 10:46 686 查看
SQL> alter table can_do modify id number(39);
alter table can_do modify id number(39)
*
ERROR at line 1:
ORA-01727: numeric precision specifier is out of range (1 to 38)


SQL> alter table can_do modify name varchar2(4001);
alter table can_do modify name varchar2(4001)
*
ERROR at line 1:
ORA-00910: specified length too long for its datatype


SQL>

说明:number的默认值是38,number的范围是(1~38)。varchar2没有默认值,必须指定长度。(1~4000)

Tip:关于number类型在Oracle中的定义,number(p[,s]),p为:precision,s为:scale

范围: 1 <= p <=38, -84 <= s <= 127
保存数据范围:-1.0e-130 <= number value < 1.0e+126
保存在机器内部的范围: 1 ~ 22 bytes

number(p,s)含义:小数点右边不超过s位,小数点左边和右边总位数不超过p位。

如果定义为:id number的话,则precision和scale没有指定,即相当于number(38,7)。

小数点右边最多7位,小数点左边和右边总位数不超过38位数字

如果定义为:id number(5)的话,则precision为5,scale为0,小数点右边没有位数,小数点左边最多5位数字,如果输入的数字是带小数位的如:12345.71的话,Oracle会自动进行四舍五入,变为:

12346,如果输入的是12345.48的话,会自动四舍五入为:12345。

如果定义为:salary number(6,2)的话,则precision为6,scale为2,即小数点右边边最多两位,小数点左边和右边总位数不超过6位(不包括小数点“.”)。

关于id number类型定义后的实验:

SQL> create table can_do_temp (id number primary key,name varchar2(100));

Table created

SQL> desc can_do_temp;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER
NAME VARCHAR2(100)

SQL> select * from can_do_temp;

ID NAME
---------- -----------------------
-
1.0000E+39 1
1.0000E+39 2
1.0000E+40 3
1.0000E+38 4
1.0000E+37 5
1.0000E+38 6

按正常的考虑,id可以吸收的最大值是38个9即:【99999999999999999999999999999999999999】

select length('99999999999999999999999999999999999999') from dual;

也就是说表can_do_temp的id最大值为1.0000E+38

那么1.0000E+39是怎么插入进去的呢?

如果你使用power函数的话,是可以插入的,如:

SQL> insert into can_do_temp b (id,name) values (power(10,40)-1,'1');

1 row created.

SQL> commit;

Commit complete.

但是如果你不使用power函数而是直接写39个9的话,会提示主键冲突,如下

SQL> insert into can_do_temp b (id,name) values (999999999999999999999999999999999999999,'1');
insert into can_do_temp b (id,name) values (999999999999999999999999999999999999999,'1')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C005211) violated

不知这个是Oracle的bugs还是Oracle本身就没有限制number的最大位数。

再试验个够狠的:

SQL> insert into can_do_temp b (id,name) values (power(10,100)-1,'9');

1 row created.

SQL> commit;

Commit complete.

SQL> alter table can_do_temp modify age number(38,128);
alter table can_do_temp modify age number(38,128)
*
ERROR at line 1:
ORA-01728: numeric scale specifier is out of range (-84 to 127)


SQL> alter table can_do_temp modify age number(39,7);
alter table can_do_temp modify age number(39,7)
*
ERROR at line 1:
ORA-01727: numeric precision specifier is out of range (1 to 38)

SQL> alter table can_do_temp modify age number(3,4);

Table altered.

SQL> desc can_do_temp;
Name Null? Type
----------------------------------------- -------- ------------------------
ID NOT NULL NUMBER
NAME VARCHAR2(100)
AGE NUMBER(3,4)

SQL> insert into can_do_temp values(1,'14',2.1234);
insert into can_do_temp values(1,'14',2.1234)
*
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column

建议在定义数据类型时不要写为:id number,

即不指定precision(scale)的格式是不赞成的。

而且一般的都是p>3的定义格式。

【编写于 2009-01-21】 阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: