在Oracle数据库中search具有特定列名的表?
我们有一个有很多表的大型Oracle数据库。 有没有一种方法,我可以查询或search,以查找是否有任何具有某些列名称的表?
IE浏览器显示所有具有列: id, fname, lname, address
细节我忘了补充:我需要能够search不同的模式。 我必须用来连接的一个并不拥有我需要search的表格。
要查找具有特定列的所有表:
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;
你想要的数据在“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%';
如果你想将许多模式的search合并成一个输出结果,那么你可以这样做:
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%'
如果您知道列名称,请使用以下查询search列名称:
select owner,table_name from all_tab_columns where upper(column_name) =upper('keyword');
要search列名称,如果您不知道以下准确的列使用情况:
select owner,table_name from all_tab_columns where upper(column_name) like upper('%keyword%');
从dba_tab_columns中selecttable_name,其中column_name ='THE_COLUMN_YOU_LOOK_FOR';
这里是我们保存的查找col.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