[转]【纠误】NUMBER定义的长度与USER_TAB_COLUMNS视图中DATA_LENGTH内容不符之缘由

原文:http://space.itpub.net/519536/viewspace-623468

1.疑问出处
1)创建测试表T
sec@ora10g> create table t (x number(38));

Table created.

2)查看表T的描述
sec@ora10g> desc t;
 Name                                  Null?    Type
 ------------------------------------- -------- --------------------------
 X                                              NUMBER(38)

3)得到user_tab_columns视图得到DATA_LENGTH字段大小
sec@ora10g> select table_name,column_name,data_length from user_tab_columns where table_name = 'T';

TABLE_NAME COLUMN_NAME DATA_LENGTH
---------- ----------- -----------
T          X                    22

4)问题
有朋友问到为什么明明X列定义的是number(38),这里DATA_LENGTH字段怎么显示的只有22?您是否也会有过这样的疑问呢?
其实这是一个概念问题,只要细心地查看一下user_tab_columns视图的DATA_LENGTH列定义便可知道答案。

2.疑问缘由
这是一个概念问题,其实只要简单比较一下Oracle官方文档中有关NUMBER类型定义和user_tab_columns视图DATA_LENGTH列定义便可自行答疑解惑!
1)有关NUMBER的官方文档说明
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/sql_elements001.htm#sthref80
NUMBER(p,s)
where:
      p is the precision, or the total number of significant decimal digits, where the most significant digit is the left-most nonzero digit, and the least significant digit is the right-most known digit. Oracle guarantees the portability of numbers with precision of up to 20 base-100 digits, which is equivalent to 39 or 40 decimal digits depending on the position of the decimal point.

2)有关user_tab_columns的DATA_LENGTH字段的官方文档说明(同ALL_TAB_COLUMNS视图)
http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/statviews_2094.htm#REFRN20277
ALL_TAB_COLUMNS
... ...
Column        Datatype  NULL      Description
DATA_LENGTH   NUMBER    NOT NULL  Length of the column (in bytes)
... ...

3)解惑
这里DATA_LENGTH表示列的长度,但请注意单位是byte,而NUMBER(p,s)中的p表示数字定义的“精度”。

3.明晰
继续使用user_tab_columns视图,再添加两个字段便可一览无遗。
sec@ora10g> select table_name,column_name,data_length,data_precision,data_scale from user_tab_columns where table_name = 'T';

TABLE_NAME COLUMN_NAME DATA_LENGTH DATA_PRECISION DATA_SCALE
---------- ----------- ----------- -------------- ----------
T          X                    22             38          0

4.小结
表T定义的number(38)类型中38指的是它的DATA_PRECISION是38,DATA_SCALE是0,它的DATA_LENGTH之所以是22,是因为在数据库里完全可以最多使用22个字节进行存储。
头脑要清醒,概念要清晰,要沉着,要冷静!

Good luck.

secooler
09.12.23

-- The End --


posted @ 2011-03-14 17:24  NewSea  阅读(2471)  评论(0编辑  收藏  举报