Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find all the tables in MySQL with specific column names in them?

I have 2-3 different column names that I want to look up in the entire database and list out all tables which have those columns. Is there any easy script?

like image 939
Jobi Joy Avatar asked Oct 11 '08 07:10

Jobi Joy


People also ask

How do I find which tables have a particular column in MySQL?

How to list all tables that contain a specific column name in MySQL? You want to look for tables using the name of columns in them. SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA. COLUMNS WHERE COLUMN_NAME IN('column1', 'column2') AND TABLE_SCHEMA = 'schema_name';

How do I find a particular column name in all tables in Oracle?

select table_name from all_tab_columns where column_name = 'PICK_COLUMN'; If you've got DBA privileges, you can try this command instead: select table_name from dba_tab_columns where column_name = 'PICK_COLUMN'; Now if you're like me, you may not even know what the column you're searching for is really named.

How do I see all the tables in a MySQL query?

In MySQL, there are two ways to find the names of all tables, either by using the "show" keyword or by query INFORMATION_SCHEMA. In the case of SQL Server or MSSQL, You can either use sys. tables or INFORMATION_SCHEMA to get all table names for a database.


2 Answers

To get all tables with columns columnA or ColumnB in the database YourDatabase:

SELECT DISTINCT TABLE_NAME      FROM INFORMATION_SCHEMA.COLUMNS     WHERE COLUMN_NAME IN ('columnA','ColumnB')         AND TABLE_SCHEMA='YourDatabase'; 
like image 92
Ken Avatar answered Sep 19 '22 15:09

Ken


SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%wild%'; 
like image 28
GSerg Avatar answered Sep 20 '22 15:09

GSerg