1)dba_tab_columns/dba_tab_cols
- SELECT *
- FROM Dba_Tab_Cols
- WHERE Table_Name = Upper('test_table_01');
- SELECT *
- FROM Dba_Tab_Columns
- WHERE Table_Name = Upper('test_table_01');
查询这两个视图需要dba权限,dba_tab_columns基于dba_tab_cols,比如隐藏列,后者更全面:
- create or replace view sys.dba_tab_columns as
- select OWNER, 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
- from DBA_TAB_COLS
- where HIDDEN_COLUMN = 'NO';
- create or replace view sys.dba_tab_cols as
- select u.name, o.name,
- c.name,
- decode(c.type#, 1, decode(c.charsetform, 2, 'NVARCHAR2', 'VARCHAR2'),
- 2, decode(c.scale, null,
- decode(c.precision#, null, 'NUMBER', 'FLOAT'),
- 'NUMBER'),
- 8, 'LONG',
- 9, decode(c.charsetform, 2, 'NCHAR VARYING', 'VARCHAR'),
- 12, 'DATE', 23, 'RAW', 24, 'LONG RAW',
- 58, nvl2(ac.synobj#, (select o.name from obj$ o
- where o.obj#=ac.synobj#), ot.name),
- 69, 'ROWID',
- 96, decode(c.charsetform, 2, 'NCHAR', 'CHAR'),
- 105, 'MLSLABEL',
- 106, 'MLSLABEL',
- 111, nvl2(ac.synobj#, (select o.name from obj$ o
- where o.obj#=ac.synobj#), ot.name),
- 112, decode(c.charsetform, 2, 'NCLOB', 'CLOB'),
- 113, 'BLOB', 114, 'BFILE', 115, 'CFILE',
- 121, nvl2(ac.synobj#, (select o.name from obj$ o
- where o.obj#=ac.synobj#), ot.name),
- 122, nvl2(ac.synobj#, (select o.name from obj$ o
- where o.obj#=ac.synobj#), ot.name),
- 123, nvl2(ac.synobj#, (select o.name from obj$ o
- where o.obj#=ac.synobj#), ot.name),
- 178, 'TIME(' ||c.scale|| ')',
- 179, 'TIME(' ||c.scale|| ')' || ' WITH TIME ZONE',
- 180, 'TIMESTAMP(' ||c.scale|| ')',
- 181, 'TIMESTAMP(' ||c.scale|| ')' || ' WITH TIME ZONE',
- 231, 'TIMESTAMP(' ||c.scale|| ')' || ' WITH LOCAL TIME ZONE',
- 182, 'INTERVAL YEAR(' ||c.precision#||') TO MONTH',
- 183, 'INTERVAL DAY(' ||c.precision#||') TO SECOND(' ||
- c.scale || ')',
- 208, 'UROWID',
- 'UNDEFINED'),
- decode(c.type#, 111, 'REF'),
- nvl2(ac.synobj#, (select u.name from user$ u, obj$ o
- where o.owner#=u.user# and o.obj#=ac.synobj#), ut.name),
- c.length, c.precision#, c.scale,
- decode(sign(c.null$),-1,'D', 0, 'Y', 'N'),
- decode(c.col#, 0, to_number(null), c.col#), c.deflength,
- c.default$, h.distcnt, h.lowval, h.hival, h.density, h.null_cnt,
- decode(h.row_cnt, 0, 1, 1, 1, h.row_cnt-1), h.timestamp#, h.sample_size,
- decode(c.charsetform, 1, 'CHAR_CS',
- 2, 'NCHAR_CS',
- 3, NLS_CHARSET_NAME(c.charsetid),
- 4, 'ARG:'||c.charsetid),
- decode(c.charsetid, 0, to_number(NULL),
- nls_charset_decl_len(c.length, c.charsetid)),
- decode(bitand(h.spare2, 2), 2, 'YES', 'NO'),
- decode(bitand(h.spare2, 1), 1, 'YES', 'NO'),
- h.avgcln,
- c.spare3,
- decode(c.type#, 1, decode(bitand(c.property, 8388608), 0, 'B', 'C'),
- 96, decode(bitand(c.property, 8388608), 0, 'B', 'C'),
- null),
- decode(bitand(ac.flags, 128), 128, 'YES', 'NO'),
- decode(o.status, 1, decode(bitand(ac.flags, 256), 256, 'NO', 'YES'),
- decode(bitand(ac.flags, 2), 2, 'NO',
- decode(bitand(ac.flags, 4), 4, 'NO',
- decode(bitand(ac.flags, 8), 8, 'NO',
- 'N/A')))),
- decode(c.property, 0, 'NO', decode(bitand(c.property, 32), 32, 'YES',
- 'NO')),
- decode(c.property, 0, 'NO', decode(bitand(c.property, 8), 8, 'YES',
- 'NO')),
- decode(c.segcol#, 0, to_number(null), c.segcol#), c.intcol#
- from sys.col$ c, sys.obj$ o, sys.hist_head$ h, sys.user$ u,
- sys.coltype$ ac, sys.obj$ ot, sys.user$ ut
- where o.obj# = c.obj#
- and o.owner# = u.user#
- and c.obj# = h.obj#(+) and c.intcol# = h.intcol#(+)
- and c.obj# = ac.obj#(+) and c.intcol# = ac.intcol#(+)
- and ac.toid = ot.oid$(+)
- and ot.type#(+) = 13
- and ot.owner# = ut.user#(+)
- and (o.type# in (3, 4) /* cluster, view */
- or
- (o.type# = 2 /* tables, excluding iot - overflow and nested tables */
- and
- not exists (select null
- from sys.tab$ t
- where t.obj# = o.obj#
- and (bitand(t.property, 512) = 512 or
- bitand(t.property, 8192) = 8192))));
2)user_tab_columns/user_tab_cols
和dba视图类似,但只能查当前用户的表:
- SELECT *
- FROM User_Tab_Cols
- WHERE Table_Name = Upper('test_table_01');
- SELECT *
- FROM User_Tab_Columns
- WHERE Table_Name = Upper('test_table_01');
3)all_tab_columns/all_tab_cols
类似,这个能查询所有用户的表,不过个人感觉检索速度慢得多:
- SELECT *
- FROM All_Tab_Cols
- WHERE Table_Name = Upper('test_table_01');
- SELECT *
- FROM All_Tab_Columns
- WHERE Table_Name = Upper('test_table_01');
另:
xxx_tab_comments 可以查询表注释;
xxx_tab_statistics 可以查询列统计信息;
xxx_tab_privs 和 xxx_tab_privs_recd 可以查询该表授权信息;