我们有一个包含许多表的大型Oracle数据库。有没有办法可以查询或搜索以查找是否有任何具有某些列名称的表?
IE向我显示所有包含列的表:id, fname, lname, address
细节我忘了添加:我需要能够搜索不同的模式。我必须使用连接的那个不拥有我需要搜索的表。
答案 0 :(得分:177)
查找具有特定列的所有表:
select owner, table_name from all_tab_columns where column_name = 'ID';
查找包含任意或全部4列的表格:
select owner, table_name, column_name
from all_tab_columns
where column_name in ('ID', 'FNAME', 'LNAME', 'ADDRESS');
要查找包含所有4列(没有丢失)的表:
select owner, table_name
from all_tab_columns
where column_name in ('ID', 'FNAME', 'LNAME', 'ADDRESS')
group by owner, table_name
having count(*) = 4;
答案 1 :(得分:10)
您想要的数据位于“cols”元数据表中:
SELECT * FROM COLS WHERE COLUMN_NAME = 'id'
这个将为您提供一个包含所有所需列的表的列表:
select distinct
C1.TABLE_NAME
from
cols c1
inner join
cols c2
on C1.TABLE_NAME = C2.TABLE_NAME
inner join
cols c3
on C2.TABLE_NAME = C3.TABLE_NAME
inner join
cols c4
on C3.TABLE_NAME = C4.TABLE_NAME
inner join
tab t
on T.TNAME = C1.TABLE_NAME
where T.TABTYPE = 'TABLE' --could be 'VIEW' if you wanted
and upper(C1.COLUMN_NAME) like upper('%id%')
and upper(C2.COLUMN_NAME) like upper('%fname%')
and upper(C3.COLUMN_NAME) like upper('%lname%')
and upper(C4.COLUMN_NAME) like upper('%address%')
要在其他模式中执行此操作,只需在表格前指定模式,如
SELECT * FROM SCHEMA1.COLS WHERE COLUMN_NAME LIKE '%ID%';
如果要将多个模式的搜索组合成一个输出结果,则可以执行以下操作:
SELECT DISTINCT
'SCHEMA1' AS SCHEMA_NAME
,TABLE_NAME
FROM SCHEMA1.COLS
WHERE COLUMN_NAME LIKE '%ID%'
UNION
SELECT DISTINCT
'SCHEMA2' AS SCHEMA_NAME
,TABLE_NAME
FROM SCHEMA2.COLS
WHERE COLUMN_NAME LIKE '%ID%'
答案 2 :(得分:10)
如果您准确了解列名称,要搜索列名称,请使用以下查询:
select owner,table_name from all_tab_columns where upper(column_name) =upper('keyword');
如果您不知道下面的准确列使用,请搜索列名称:
select owner,table_name from all_tab_columns where upper(column_name) like upper('%keyword%');
答案 3 :(得分:0)
这是我们保存到findcol.sql的一个,所以我们可以在SQLPlus中轻松运行它
set verify off
clear break
accept colnam prompt 'Enter Column Name (or part of): '
set wrap off
select distinct table_name,
column_name,
data_type || ' (' ||
decode(data_type,'LONG',null,'LONG RAW',null,
'BLOB',null,'CLOB',null,'NUMBER',
decode(data_precision,null,to_char(data_length),
data_precision||','||data_scale
), data_length
) || ')' data_type
from all_tab_columns
where column_name like ('%' || upper('&colnam') || '%');
set verify on