USER_TAB_COLS和USER_TAB_COLUMNS两个视图有什么区别

USER_TAB_COLS和USER_TAB_COLUMNS两个视图有什么区别

 

看oracle的元数据,

--by yl [email protected],[email protected] 2008.9.12

create or replace view sys.user_tab_columns as
select TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_TYPE_MOD, DATA_TYPE_OWNER,
       DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE, COLUMN_ID,
       DEFAULT_LENGTH, DATA_DEFAULT, NUM_DISTINCT, LOW_VALUE, HIGH_VALUE,
       DENSITY, NUM_NULLS, NUM_BUCKETS, LAST_ANALYZED, SAMPLE_SIZE,
       CHARACTER_SET_NAME, CHAR_COL_DECL_LENGTH,
       GLOBAL_STATS, USER_STATS, AVG_COL_LEN, CHAR_LENGTH, CHAR_USED,
       V80_FMT_IMAGE, DATA_UPGRADED, HISTOGRAM
  from USER_TAB_COLS
 where HIDDEN_COLUMN = 'NO'

HIDDEN_COLUMN 字段来源

SQL> CREATE TABLE yl (id integer,name varchar2(32));

Table created

SQL> INSERT INTO yl SELECT  rownum,'yanleigis' || LPAD(ROWNUM,2,'0') FROM DUAL CONNECT BY ROWNUM<=11;

11 rows inserted

SQL> commit;

Commit complete

SQL> CREATE INDEX idx_Name_yl ON yl(SUBSTR(name,4));

Index created

SQL> DESC yl;
Name Type         Nullable Default Comments
---- ------------ -------- ------- --------
ID   INTEGER      Y                        
NAME VARCHAR2(32) Y                        

SQL> select * from yl;

                                     ID NAME
--------------------------------------- --------------------------------
                                      1 yanleigis01
                                      2 yanleigis02
                                      3 yanleigis03
                                      4 yanleigis04
                                      5 yanleigis05
                                      6 yanleigis06
                                      7 yanleigis07
                                      8 yanleigis08
                                      9 yanleigis09
                                     10 yanleigis10
                                     11 yanleigis11

11 rows selected

SQL> SELECT COLUMN_NAME,HIDDEN_COLUMN FROM user_TAB_COLS WHERE TABLE_NAME='YL';

COLUMN_NAME                    HIDDEN_COLUMN
------------------------------ -------------
ID                             NO
NAME                           NO
SYS_NC00003$                   YES

SQL>  SELECT SYS_NC00003$ ,id,name FROM yl;

SYS_NC00003$                                                                                    ID NAME
---------------------------------------------------------- --------------------------------------- --------------------------------
leigis01                                                                                         1 yanleigis01
leigis02                                                                                         2 yanleigis02
leigis03                                                                                         3 yanleigis03
leigis04                                                                                         4 yanleigis04
leigis05                                                                                         5 yanleigis05
leigis06                                                                                         6 yanleigis06
leigis07                                                                                         7 yanleigis07
leigis08                                                                                         8 yanleigis08
leigis09                                                                                         9 yanleigis09
leigis10                                                                                        10 yanleigis10
leigis11                                                                                        11 yanleigis11

11 rows selected

    由此可见,Oracle在创建函数索引的时候,实际上在表上添加了一个字段,用于保留该函数的计算结果,并基于此字段创建普通索引,
由于在底层数据字典中,将这个字段标记为 HIDDEN,因此用户在执行 DESC 的时候看不到,但可以用 SELECT 来查看。
在我们查找字段时,尽可能使用.user_tab_columns 而不是USER_TAB_COLS

因为USER_TAB_COLS可能加入oracle自己的隐含字段

于是,随之而来的问题就是Oracle是如何维护这个隐藏字段的呢?

SELECT COLUMN_NAME,COLUMN_ID,DATA_DEFAULT,HIDDEN_COLUMN,VIRTUAL_COLUMN,SEGMENT_COLUMN_ID,INTERNAL_COLUMN_ID
  FROM User_TAB_COLS
    WHERE TABLE_NAME='YL';
COLUMN_NAME                     COLUMN_ID DATA_DEFAULT                                                                     HIDDEN_COLUMN VIRTUAL_COLUMN SEGMENT_COLUMN_ID INTERNAL_COLUMN_ID
------------------------------ ---------- -------------------------------------------------------------------------------- ------------- -------------- ----------------- ------------------
ID                                      1                                                                                  NO            NO                             1                  1
NAME                                    2                                                                                  NO            NO                             2                  2
SYS_NC00003$                              SUBSTR("NAME",4)                                                                 YES           YES                                               3
可以看出,
1、普通的字段其 COLUMN_ID、SEGMENT_COLUMN_ID 是完全一致的,而Oracle自行添加的字段这两项却是空的,这个实际上是由于DBA_TAB_COLS的定义中做了转换:
decode(c.col#, 0, to_number(null), c.col#) as COLUMN_ID
decode(c.segcol#, 0, to_number(null), c.segcol#) as SEGMENT_COLUMN_ID(这里的c就是 sys.col$),也就是说,实际上Oracle自行添加的字段这两项也是一致的,只不过是0而已
2、普通字段当然不会是隐藏、虚拟的,而Oracle自行添加的其 HIDDEN_COLUMN 和VIRTUAL_COLUMN 都是YES
3、INTERNAL_COLUMN_ID 完全与字段加入表中的顺序一致
4、注意Oracle自行添加的那个字段,将函数的表达式作为默认值

参考:http://www.itpub.net/thread-575391-1-1.html



你可能感兴趣的:(oracle,user,table,Integer,character,Comments)