1025

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
  • 1
    Refer this: http://winashwin.wordpress.com/2012/08/28/mysql-search/ –  Sep 05 '12 at 06:44

11 Answers11

1660

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';
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Ken
  • 77,016
  • 30
  • 84
  • 101
  • 8
    @Echo - you can always play with mysqldump with --skip-extended-insert and then grep through the file... dirty but strangely satifying :) – Ken Oct 20 '11 at 17:35
  • 1
    @Echo, for versions prior to 5 dump the structure to file using mysqldump, see my answer. – Radu Maris Aug 14 '12 at 13:03
  • @kaii I would like to add more question on how will search including its type as PK. e.g. statement that goes: where COLUMN_NAME ='' and – Raf Mar 03 '13 at 17:04
  • @raf Try DESCRIBE INFORMATION_SCHEMA.COLUMNS; COLUMN_KEY = "PRI" should do the trick – Ken Mar 04 '13 at 12:09
  • 11
    You can also use `DATABASE()` instead of a string to search in the currently selected database. – Sherlock Mar 12 '13 at 16:01
  • 1
    @Ken : is there a way to add one more filter to your query ? Indeed, selecting the tables where de columnA has a specific value. – Fred FLECHE Oct 08 '14 at 12:15
  • 2
    @FredFLECHE I think at that stage I would just loop through them in a script and keep it simple – Ken Oct 08 '14 at 13:26
  • @Ken. Actually while waiting for your input I used a php script to do the simple and effcient solution you just suggested. Thanks. – Fred FLECHE Oct 09 '14 at 12:58
  • Thanks, especially useful when your coworker adds column manually instead of doing it in the migration script – Blkc May 14 '19 at 14:13
  • If you are looking for tables that must have ALL columns, this is relevant: https://stackoverflow.com/questions/23752116/mysql-select-all-tables-with-multiple-specific-columns – Herbert Van-Vliet Dec 10 '19 at 09:15
  • What it would be the approach for ColumnA AND ColumnB (not or) ?. So which tables contains those two columns, not at least one of those. – love2code Dec 17 '19 at 21:57
  • @Sherlock, I can't get your tip work with MySQL 5.6. – Éric Dec 14 '20 at 08:17
  • I'm using MySQL 8.0.25. I met a problem that this query also returns VIEW names. And I want to filter out them. I used a subquery `... WHERE ... AND TABLE_NAME NOT IN (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA='YourDatabase'))` Any ideas to improve this? – Hegwin Jun 09 '21 at 13:45
  • @Ken Can you help me in my question please: https://stackoverflow.com/questions/69005703/join-two-tables-and-extract-the-lowest-price-with-multiple-conditions-multiple – GePraxa Sep 02 '21 at 05:04
  • Kindly let me know if this is also correct? SELECT t.table_name,c.column_name FROM information_schema.tables t join information_schema.columns c on t.table_name=c.table_name Where t.TABLE_SCHEMA IN ('bayut_main') and c.column_name like 'userid%' – Abdullah Sohail Yaqoob Nov 18 '21 at 10:55
  • 1
    @Hegwin Another way to filter out views is to join on `information_schema.tables` and filter on `table_type = 'BASE TABLE'` – akenney Nov 22 '22 at 00:13
258
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 20
    If there are multiple databases, add a WHERE TABLE_SCHEMA="" line also. – John Millikin Oct 11 '08 at 07:08
  • 1
    Thanks John, how will the query if I need to get Table names which has columnA and ColumnB present? – Jobi Joy Oct 11 '08 at 07:19
  • 2
    @JohnMillikin: or conversely, if you don't know the database but only the field name, leave it out and add `TABLE_SCHEMA` to the fields of the return set to see all databases + tables that contain that column name. – Abel May 17 '13 at 15:02
  • Shouldn't that read either: like '%wild%' or = 'wild' instead of like 'wild'? – Uncle Iroh Sep 23 '16 at 21:16
  • 1
    I just tried this but noticed all the results I got where for objects that don't show up with the query `show tables;`. Can anyone explain why this might be the case? – wdkrnls Jan 06 '17 at 16:14
  • @wdkrnls you missed the first comment below this solution. Your results are from other dbs, while show tables shows you only the currently selected db's columns. – Ecuador Feb 06 '19 at 16:58
  • I would just add TABLE_SCHEMA and I'd be happy with it. I'm voting this because it is the closest match to how I modified Ken's answer. – louigi600 Sep 08 '21 at 07:38
61

More simply done in one line of SQL:

SELECT * FROM information_schema.columns WHERE column_name = 'column_name';
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
Xman Classical
  • 5,179
  • 1
  • 26
  • 26
42
SELECT DISTINCT TABLE_NAME, COLUMN_NAME  
FROM INFORMATION_SCHEMA.COLUMNS  
WHERE column_name LIKE 'employee%'  
AND TABLE_SCHEMA='YourDatabase'
Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
baycaysoi
  • 429
  • 4
  • 3
21

In older MySQL versions or some MySQL NDB Cluster versions that do not have information_schema, you can dump the table structure and search the column manually.

mysqldump -h$host -u$user -p$pass --compact --no-data --all-databases > some_file.sql

Now search the column name in some_file.sql using your preferred text editor, or use some nifty AWK scripts.


And a simple sed script to find the column. Just replace COLUMN_NAME with yours:

sed -n '/^USE/{h};/^CREATE/{H;x;s/\nCREATE.*\n/\n/;x};/COLUMN_NAME/{x;p};' <some_file.sql
USE `DATABASE_NAME`;
CREATE TABLE `TABLE_NAME` (
  `COLUMN_NAME` varchar(10) NOT NULL,

You can pipe the dump directly in sed, but that's trivial.

Radu Maris
  • 5,648
  • 4
  • 39
  • 54
  • 1
    Versions of what? MySQL? What is an "ndb"? [InnoDB](https://en.wikipedia.org/wiki/InnoDB)? Or something else? As much as possible, please respond by [editing (changing) your answer](https://stackoverflow.com/posts/11952827/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). For example, by linking to what a "ndb" is. – Peter Mortensen Aug 16 '21 at 15:42
  • @PeterMortensen I appreciate your intentions, but as it's a 10y old answer can't remember exact versions or so, I rephrease it and use MySQL NDB Cluster instead of simply ndb, hope it's cleared now. – Radu Maris Aug 23 '21 at 07:20
17

For those searching for the inverse of this, i.e. looking for tables that do not contain a certain column name, here is the query...

SELECT DISTINCT TABLE_NAME FROM information_schema.columns WHERE 
TABLE_SCHEMA = 'your_db_name' AND TABLE_NAME NOT IN (SELECT DISTINCT 
TABLE_NAME FROM information_schema.columns WHERE column_name = 
'column_name' AND TABLE_SCHEMA = 'your_db_name');

This came in really handy when we began to slowly implement use of InnoDB's special ai_col column and needed to figure out which of our 200 tables had yet to be upgraded.

oucil
  • 4,211
  • 2
  • 37
  • 53
13

Use this one line query. Replace desired_column_name by your column name.

SELECT TABLE_NAME FROM information_schema.columns WHERE column_name = 'desired_column_name';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
11

If you want to "get all tables only", then use this query:

SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME like '%'
and TABLE_SCHEMA = 'tresbu_lk'

If you want "to get all tables with columns", then use this query:

SELECT DISTINCT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE '%'
AND TABLE_SCHEMA='tresbu_lk'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
9
select distinct table_name 
from information_schema.columns 
where column_name in ('ColumnA') 
    and table_schema='YourDatabase';
    and table_name in 
    (
        select distinct table_name 
        from information_schema.columns 
        where column_name in ('ColumnB')
              and table_schema='YourDatabase';
    );

That ^^ will get the tables with ColumnA and ColumnB instead of ColumnA or ColumnB like the accepted answer

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
love2code
  • 441
  • 6
  • 9
  • 1
    Does that actually work? What about the first semicolon? In any case, an explanation would be in order. E.g., what is the gist of it? What version of MySQL did you test it on? Etc. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/59382822/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 16 '21 at 15:51
5
SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%city_id%' AND TABLE_SCHEMA='database'
nageen nayak
  • 1,262
  • 2
  • 18
  • 28
3

The problem with information_schema is that it can be terribly slow. It is faster to use the SHOW commands.

After you select the database you first send the query SHOW TABLES. And then you do SHOW COLUMNS for each of the tables.

In PHP that would look something like


    $res = mysqli_query("SHOW TABLES");
    while($row = mysqli_fetch_array($res))
    {   $rs2 = mysqli_query("SHOW COLUMNS FROM ".$row[0]);
        while($rw2 = mysqli_fetch_array($rs2))
        {   if($rw2[0] == $target)
               ....
        }
    }

user2587656
  • 309
  • 3
  • 5
  • Can you quantify *"terribly slow"*? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/54826287/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 16 '21 at 15:48