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%'
Related
I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.
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.
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 = ...
Is there any select statement to return the list of columns in the table?
The INFORMATION_SCHEMA.COLUMNS view will provide the column names for a particular table name.
SELECT Column_Name + ', '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Table_Name'
There are several other views as well as the one above which you may find useful. These INFORMATION_SCHEMA views provide information on the schema of your database.
Select top 10 * from Information_Schema.tables
Select top 10 * from Information_Schema.views
Select top 10 * from Information_Schema.routines
Select top 10 * from Information_Schema.parameters
Paul's answer is right for mysql. ON EDIT: and sql server too, apparently. Arrgh. Sorry Paul.
For sql server, you want sys.syscolumns, very similarly to this answer:
How do I look at column metadata in Sybase?
sp_help TableName
Will give you all columns, plus lots of other information.
You can also get column data in SqlServer 2005 using
SELECT column_name 'Column Name',
data_type 'Data Type'
FROM information_schema.columns
WHERE table_name = 'table name'
Srinivas Dontula.
sdonthula#live.com