ITPub博客

首页 > 数据库 > Oracle > [20160828]number类型.txt

[20160828]number类型.txt

原创 Oracle 作者:lfree 时间:2016-08-29 10:09:57 0 删除 编辑

[20160828]number类型.txt

--昨天看了一个链接http://www.cnblogs.com/kerrycode/p/4427352.html,感觉有点不对,上班测试看看。

1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

create table tx ( a number,b number(38,2) ,c number(38));
insert into tx values (1/3,1/3,1/3);
commit ;


SCOTT@book> select * from tx;
         A          B          C
---------- ---------- ----------
.333333333        .33          0

2.测试:
set serveroutput on

DECLARE 
CURSOR c_test IS SELECT a,b,c FROM Tx;
c_row c_test%rowtype;
begin
   for c_row in c_test loop
        dbms_output.put_line('the result is a=' || c_row.a);
        dbms_output.put_line('the result is b=' || c_row.b);
        dbms_output.put_line('the result is c=' || c_row.c);
   end loop;
end;
/

the result is a=.3333333333333333333333333333333333333333
the result is b=.33
the result is c=0
PL/SQL procedure successfully completed.

SCOTT@book> @ &r/desc tx
           Name Null?    Type
           ---- -------- ----------------------------
    1      A             NUMBER
    2      B             NUMBER(38,2)
    3      C             NUMBER(38)

--很明显我并没有测试出作者的情况,实际上定义number,并不意味者精度是0.估计作者使用pl/sql的原因。

SCOTT@book> select dump(a,10) c80 ,dump(b,10) c30 ,dump(c,10) c20 from tx;
C80                                                                              C30                            C20
-------------------------------------------------------------------------------- ------------------------------ --------------------
Typ=2 Len=21: 192,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34    Typ=2 Len=2: 192,34            Typ=2 Len=1: 128

--你可以发现字段a,长度是21,也就是在生产系统最好不要简单的定义类型number,要加入整数部分长度的限制,避免一些运算导致字
--段占用空间很大,最佳的方式准确的设置scale。

--另外我曾经在使用copy命令(sqlplus)时出现异常,可以参考链接:
http://blog.itpub.net/267265/viewspace-1257036/

--使用copy后丢失了小数点后面的信息,数据类型number变成了number(38).

--总之最好不要简单的定义类型是number,要写成number(10) ,如果有精度需求加入number(10,3).

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2124132/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4014
  • 访问量
    7991438