I have this large database, and someone asked me to find out what tables has column name X. Is that possible?
You can query against all_tab_columns like this:
SELECT table_name
FROM all_tab_columns
WHERE column_name LIKE '%your_search_column_name%'
Have a look at the All_tab_columns system table.
http://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm
SELECT
TABLE_NAME, COLUMN_NAME
FROM
ALL_TAB_COLUMNS
WHERE
COLUMN_NAME = ...
Related
I want to select for all tables with columns that contain the word deposit from a list of all tables when the owner is like bi. How can I implement this?
For example, I can select all tables and owner:
SELECT * FROM ALL_TABLES WHERE OWNER LIKE '%BI';
But I want to then select all tables from table_name and run a query to find those columns that are type character and contain the string deposit. This I cannot understand how to do.
UPDATE:
The following query worked:
SELECT *
FROM ALL_TAB_COLUMNS
WHERE OWNER LIKE '%BI'
AND DATA_TYPE
LIKE '%NUMBER'
AND COLUMN_NAME
LIKE '%DEPOSIT%';
Just change the data_type to varchar% (to catch varchar and varchar2):
SELECT DISTINCT TABLE_NAME
FROM DBA_TAB_COLUMNS TBLS
WHERE UPPER(TBLS.OWNER) LIKE '%BI'
AND UPPER(COLUMN_NAME) LIKE '%DEPOSIT%'
AND UPPER(DATA_TYPE) LIKE 'VARCHAR%';
I need to list tables, columns and their data type; I used this code:
select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from dba_tab_columns
order by TABLE_NAME;
The problem is that the database is too large and I only need 2000 specific table/column/datatype. Knowing that a table with the same name and column can be present in multiple SCHEMA
For example (I have 2000 lines like that table names are very random and have nothing in common so is the case for the column names):
TABLE Column SCHEMA
------------------------------------------
DMT_AAAAAAA C1111 ANT_A1
DMT_AAAAAAA C1111 ANT_A2
BBBBBBBB A4444 ANT_A3
JHD6365 H5525 ZUGRU
WRK679 C3020 MUSTSU
TDG5378 C66739 SHGUY
I tried to filter by schema using this query :
select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from dba_tab_columns
where OWNER in ('ANT_A1', 'ANT_A2', 'ANT_A1', 'ZUGRU', 'MUSTSU', 'SHGUY')
order by TABLE_NAME;
The issue now is that it lists all columns even the ones I don't need.
Is there a way to filter out only the lines needed?
Expected output :
|table name | column | data type|
DMT_AAAAAAA C1111 NUMBER
DMT_AAAAAAA C1111 VARCHAR
BBBBBBBB A4444 NUMBER
JHD6365 H5525 VARCHAR
WRK679 C3020 VARCHAR
TDG5378 C66739 VARCHAR
Thank you.
If you want to show the information about certain columns across schemas and tables, specify them in an IN clause using tuples:
select owner, table_name, column_name, data_type
from dba_tab_columns
where (owner, table_name, column_name) in
(
('DMT_AAAAAAA', 'C1111', 'ANT_A1'),
('DMT_AAAAAAA', 'C1111', 'ANT_A2'),
('BBBBBBBB', 'A4444', 'ANT_A3'),
('JHD6365', 'H5525', 'ZUGRU'),
('WRK679', 'C3020', 'MUSTSU'),
('TDG5378', 'C66739', 'SHGUY')
)
order by owner, table_name, column_name;
Check whether the following is enough. (I'm considering your output only)
select Distinct TABLE_NAME, COLUMN_NAME, DATA_TYPE from dba_tab_columns
If you don't want the tables, 'BBBBBBBB' and 'WRK679'
Put those in to a table (say tblExclude) and you can use the following
select Distinct TABLE_NAME, COLUMN_NAME, DATA_TYPE from dba_tab_columns a
Left Join tblExclude b on a.TABLE_NAME = b.TABLE_NAME
Where b.TABLE_NAME is null
I'm trying to find a table in a schema with a specific column name. So I used the following script, but it doesn't return anything:
select a.table_name, column_name,DATA_TYPE,DATA_LENGTH
from all_tab_columns a,USER_ALL_TABLES u
where a.TABLE_NAME=u.TABLE_NAME
and column_name like '%LATLONG%'
order by DATA_LENGTH desc;
On the other hand, a SELECT of table LATLONG_DETAIL will display a column called LATLONG_TYPE.
So why isn't the query displaying this table in its result?
All these queries are being run in the schema where table LATLONG_DETAIL resides.
Thanks.
You say you own the table LATLONG_DETAIL. The only other thing I can think of why your query isn't returning anything is that the column name is not in upper case. Does this query return anything?
SELECT a.table_name, column_name,DATA_TYPE,DATA_LENGTH
FROM all_tables u JOIN all_tab_columns a
ON u.table_name = a.table_name
AND u.owner = a.owner
WHERE UPPER(column_name) LIKE '%LATLONG%';
I want to find the tables that contain both of the two columns together in one table. I tried this:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey');
This query generates all the tables that has either one of the CurrencyName or CurrencyKey column.
But I want the table that has both of these columns together.
Please shoot some ideas.
Thanks!
You are close. You want to use group by and then validate that you have two matches using a having clause:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 2;
Note: To be sure you have the right table, you should use TABLE_SCHEMA as well in the query.
Can I write an sql query that can find the names of all tables in a database that have column name like %COL_FAX_ID%. If so, how?
Database used is oracle or db2.
For Oracle, you could try:
SELECT owner,
table_name,
column_name
FROM all_tab_cols
WHERE column_name LIKE '%COL_FAX_ID%'
ORDER BY owner,
table_name;
For a full list of the Oracle data dictionary views etc. see here.
Hope it helps...
I don't have an oracle install lying around to test this with but you should be able to do something like:
SELECT TABLE_NAME
FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME LIKE '%COL_FAX_ID%'
Dublicate of possible How to find all the tables in MySQL with specific column names in them?
Answer:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('columnA','ColumnB')
AND TABLE_SCHEMA='YourDatabase';
Edit:
Sorry I Miss saw the sql for beeing mysql tag...
But this may work aswell? Dunno. Gl & Hf
select distinct table_name from all_tab_columns where column_name like %'COL_FAX_ID%'
For oracle:
SELECT table_name,column_name from all_tab_columns
where column_name like '%COL_FAX_ID%'
For DB2, you will want to use the SYSCAT.COLUMNS catalog view.
SELECT *
FROM SYSCAT.COLUMNS
WHERE COLNAME LIKE '%COL_FAX_ID%'